(1)Declarations of Strings in PHP
Strings are a sequence of character that are treated as one unit.Strings in PHP are declared in two
ways:
Single quoted
Double quoted
Single Quoted String:
Here the statement present within the single quotes will be displayed as it is without any changes.
Example:
<?php
$string_variable = "name";
$literally = 'My $string_variable is Happy!\n';
print($literally);
?>
Output:
My $string_variable is Happy!\n
Double Quoted String:
Here the statement present within the double quotes will be interpreted and the output of the
program will be displayed.
Example:
<?php
$string_variable = "name";
$literally = “My $string_variable is Happy!\n”;
print($literally);
?>
Output:
My name is Happy!
PHP provides a number of built-in string functions which help in performing several
operations while dealing with string data.
Basic PHP String Functions
In this section, we will discuss a few basic string functions which are most commonly used
in PHP scripts.
1. Getting length of a String
PHP has a predefined function to get the length of a string. Strlen() displays the length of
any string. It is more commonly used in validating input fields where the user is limited to
enter a fixed length of characters.
Syntax
Strlen(string);
Example
<?php
echo strlen(“Welcome to Cloudways”);//will return the length of given string
?>
Output
20
2. Counting of the number of words in a String
Another function which enables display of the number of words in any specific string is
str_word_count(). This function is also useful in validation of input fields.
Syntax
Str_word_count(string)
Example
<?php
echo str_word_count(“Welcome to Cloudways”);//will return the number of words in a string
?>
Output
3
3. Reversing a String
Strrev() is used for reversing a string. You can use this function to get the reverse version
of any string.
Syntax
Strev(string)
Example
<?php
echo strrev(“Welcome to Cloudways”);// will return the string starting from the end
?>
Output
syawduolC ot emocleW
4. Finding Text Within a String
Strpos() enables searching particular text within a string. It works simply by matching the
specific text in a string. If found, then it returns the specific position. If not found at all, then
it will return “False”. Strops() is most commonly used in validating input fields like email.
Syntax
Strpos(string,text);
Example
<?php
echo strpos(“Welcome to Cloudways”,”Cloudways”);
?>
Output
11
5. Replacing text within a string
Str_replace() is a built-in function, basically used for replacing specific text within a string.
Syntax
Str_replace(string to be replaced,text,string)
Example
<?php
echo str_replace(“cloudways”, “the programming world”, “Welcome to cloudways”);
?>
Output
Welcome to the programming world
6. Converting lowercase into Title Case
Ucwords() is used to convert first alphabet of every word into uppercase.
Syntax
Ucwords(string)
Example
<?php
echo ucwords(“welcome to the php world”);
?>
Output
Welcome To The Php World
7. Converting a whole string into UPPERCASE
Strtoupper() is used to convert a whole string into uppercase.
Syntax
Strtoupper(string);
Example
<?php
echo strtoupper(“welcome to cloudways”);// It will convert all letters of string into uppercase
?>
Output
WELCOME TO CLOUDWAYS
8. Converting whole String to lowercase
Strtolower() is used to convert a string into lowercase.
Syntax
Strtolower(string)
Example
<?php
echo strtolower(“WELCOME TO CLOUDWAYS”);
?>
Output
welcome to cloudways
9. Repeating a String
PHP provides a built-in function for repeating a string a specific number of times.
Syntax
Str_repeat(string,repeat)
Example
<?php
echo str_repeat(“=”,13);
?>
Output
=============
10. Comparing Strings
You can compare two strings by using strcmp(). It returns output either greater than zero,
less than zero or equal to zero. If string 1 is greater than string 2 then it returns greater
than zero. If string 1 is less than string 2 then it returns less than zero. It returns zero, if the
strings are equal.
Syntax
Strcmp(string1,string2)
Example
<?php
echo strcmp(“Cloudways”,”CLOUDWAYS”);
echo “<br>”;
echo strcmp(“cloudways”,”cloudways”);//Both the strings are equal
echo “<br>”;
echo strcmp(“Cloudways”,”Hosting”);
echo “<br>”;
echo strcmp(“a”,”b”);//compares alphabetically
echo “<br>”;
echo strcmp(“abb baa”,”abb baa caa”);//compares both strings and returns the result in
terms of number of characters.
?>
Output
-1
-1
-4
11. Displaying part of String
Through substr() function you can display or extract a string from a particular position.
Syntax
substr(string,start,length)
Example
<?php
echo substr(“Welcome to Cloudways”,6).”<br>”;
echo substr(“Welcome to Cloudways”,0,10).”<br>”;
?>
Output
e to Cloudways
Welcome to
s
12. Removing white spaces from a String
Trim() is dedicated to remove white spaces and predefined characters from a both the
sides of a string.
Syntax
trim(string,charlist)
Example
<?php
$str = “Wordpess Hosting”;
echo $str . “<br>”;
echo trim(“$str”,”Wording”);
?>
Output
Wordpess Hosting
pess Host
(2) php - operators
In all programming languages, operators are used to manipulate or perform operations on variables and
values. You have already seen the string concatenation operator "." in the Echo Lesson and the assignment
operator "=" in pretty much every PHP example so far.
Advertise on Tizag.com
There are many operators used in PHP, so we have separated them into the following categories to make
it easier to learn them all.
Assignment Operators
Arithmetic Operators
Comparison Operators
String Operators
Combination Arithmetic & Assignment Operators
assignment operators
Assignment operators are used to set a variable equal to a value or set a variable to another variable's
value. Such an assignment of value is done with the "=", or equal character. Example:
$my_var = 4;
$another_var = $my_var;
Now both $my_var and $another_var contain the value 4. Assignments can also be used in conjunction
with arithmetic operators.
arithmetic operators
Operator English Example
+ Addition 2+4
- Subtraction 6-2
* Multiplication 5 * 3
/ Division 15 / 3
% Modulus 43 % 10
PHP Code:
$addition = 2 + 4;
$subtraction = 6 - 2;
$multiplication = 5 * 3;
$division = 15 / 3;
$modulus = 5 % 2;
echo "Perform addition: 2 + 4 = ".$addition."<br />";
echo "Perform subtraction: 6 - 2 = ".$subtraction."<br />";
echo "Perform multiplication: 5 * 3 = ".$multiplication."<br />";
echo "Perform division: 15 / 3 = ".$division."<br />";
echo "Perform modulus: 5 % 2 = " . $modulus
. ". Modulus is the remainder after the division operation has been performed.
In this case it was 5 / 2, which has a remainder of 1.";
Display:
Perform addition: 2 + 4 = 6
Perform subtraction: 6 - 2 = 4
Perform multiplication: 5 * 3 = 15
Perform division: 15 / 3 = 5
Perform modulus: 5 % 2 = 1. Modulus is the remainder after the division operation has been
performed. In this case it was 5 / 2, which has a remainder of 1.
comparison operators
Comparisons are used to check the relationship between variables and/or values. If you would like to see a
simple example of a comparison operator in action, check out our If Statement Lesson. Comparison operators
are used inside conditional statements and evaluate to either true or false. Here are the most important
comparison operators of PHP.
Assume: $x = 4 and $y = 5;
Operator English Example Result
== Equal To $x == $y false
!= Not Equal To $x != $y true
< Less Than $x < $y true
> Greater Than $x > $y false
<= Less Than or Equal To $x <= $y true
>= Greater Than or Equal To $x >= $y false
string operators
As we have already seen in the Echo Lesson, the period "." is used to add two strings together, or more
technically, the period is the concatenation operator for strings.
PHP Code:
$a_string = "Hello";
$another_string = " Billy";
$new_string = $a_string . $another_string;
echo $new_string . "!";
Display:
Hello Billy!
combination arithmetic & assignment operators
In programming it is a very common task to have to increment a variable by some fixed amount. The most
common example of this is a counter. Say you want to increment a counter by 1, you would have:
$counter = $counter + 1;
However, there is a shorthand for doing this.
$counter += 1;
This combination assignment/arithmetic operator would accomplish the same task. The downside to this
combination operator is that it reduces code readability to those programmers who are not used to such an
operator. Here are some examples of other common shorthand operators. In general, "+=" and "-=" are the
most widely used combination operators.
Operator English Example Equivalent Operation
+= Plus Equals $x += 2; $x = $x + 2;
-= Minus Equals $x -= 4; $x = $x - 4;
*= Multiply Equals $x *= 3; $x = $x * 3;
/= Divide Equals $x /= 2; $x = $x / 2;
%= Modulo Equals $x %= 5; $x = $x % 5;
.= Concatenate Equals $my_str.="hello"; $my_str = $my_str . "hello";
pre/post-increment & pre/post-decrement
This may seem a bit absurd, but there is even a shorter shorthand for the common task of adding 1 or
subtracting 1 from a variable. To add one to a variable or "increment" use the "++" operator:
$x++; Which is equivalent to $x += 1; or $x = $x + 1;
To subtract 1 from a variable, or "decrement" use the "--" operator:
$x--; Which is equivalent to $x -= 1; or $x = $x - 1;
In addition to this "shorterhand" technique, you can specify whether you want to increment before the line
of code is being executed or after the line has executed. Our PHP code below will display the difference.
PHP Code:
$x = 4;
echo "The value of x with post-plusplus = " . $x++;
echo "<br /> The value of x after the post-plusplus is " . $x;
$x = 4;
echo "<br />The value of x with with pre-plusplus = " . ++$x;
echo "<br /> The value of x after the pre-plusplus is " . $x;
Display:
The value of x with post-plusplus = 4
The value of x after the post-plusplus is = 5
The value of x with with pre-plusplus = 5
The value of x after the pre-plusplus is = 5
As you can see the value of $x++ is not reflected in the echoed text because the variable is not
incremented until after the line of code is executed. However, with the pre-increment "++$x" the variable does
reflect the addition immediately.