PHP String

A string variable is used to store and manipulate a piece of text.
Working with Strings
String variables are used for values that contains character strings. In this tutorial we are going to look at some of the most common functions and operators used to manipulate strings in PHP. After we create a string we can manipulate it.
A string can be used directly in a function or it can be stored in a variable. Below, the PHP script assigns the string “Hello World!” to a string variable called $text_str:
	$text_str= “Hello World!”;
	echo “$text_str”	;
Type the above code and save it as string.php and open the page in browser to view the output:
PHP – String Creation Heredoc
The two methods above are the traditional way to create strings in most programming languages. PHPintroduces a more robust string creation tool called heredoc that lets the programmer create multi-line strings without using quotations. However, creating a string using heredoc is more difficult and can lead to problems if you do not properly code your string! Here’s how to do it:
PHP Code:
$my_string = <<<STR
<a href="" target="_self"></a><br />
PHP Tutorial<br />
Feel the power of PHP
echo $my_string;
Type the above code and save it as string-heredoc.php and open the script in browser to view the output.
There are a few very important things to remember when using heredoc.
• Use <<< and some identifier that you choose to begin the heredoc. In this example we chose STR as our identifier.

• Repeat the identifier followed by a semicolon to end the heredoc string creation. In this example that was STR;

• The closing sequence STR; must occur on a line by itself and cannot be indented!

Another thing to note is that when you output this multi-line string to a web page, it will not span multiple lines because we did not have any <br /> tags contained inside our string! Here is the output made from the code above.
PHP String Manipulation
Simple PHP string operation
Using the strlen() function
The strlen() function is used to find the length of a string.
int strlen ( string str )
Let’s find the length of our string “Hello world!”:
    echo strlen("eBIZ education team.");
The output of the code above will be:
The length of a string is often used in loops or other functions, when it is important to know when the string ends. (i.e. in a loop, we would want to stop the loop after the last character in the string)
Using the strpos() function
The strpos() function is used to search for a string or character within a string.
int strpos ( string haystack, string needle [, int offset] )
If a match is found in the string, this function will return the position of the first match. If no match is found, it will return FALSE.
Let’s see if we can find the string “world” in our string:
echo strpos("eBIZ education team.", “team”);
The output of the code above will be:
As you see the position of the string “team” in our string is position 15. The reason that it is 15, and not 16, is that the first position in the string is 0, and not 1.
<h3>strlen function</h3>
$txt="eBIZ education team.";

echo "Length of $txt=". strlen($txt);
<h3>strpos function</h3>
$txt="eBIZ education team.";

echo "Position of team in  $txt=". strpos($txt,"team");
Type the above code and save it as str-function.php and open the script in browser, to view the output.
The Concatenation Operator
There is only one string operator in PHP. The concatenation operator (.) is used to put two string values together.
$var=$var1. $var2;
To concatenate two variables together, use the dot (.) operator:
$txt1="Hello World";
$txt2="eBIZ Education Team";
echo "String 1= ".$txt1."<br >String =" . 
$txt2."<br />String 1 . String 2 =".$txt1." ".$txt2;
The output of the code above will be:
Hello World eBIZ Education Team
If we look at the code above you see that we used the concatenation operator two times. This is because we had to insert a third string. Between the two string variables we added a string with a single character, an empty space, to separate the two variables.
Scroll to Top