New way to learn PHP

PHP 5 String


String is nothing, it's just a Series of character or set of character, like pTutorial, PHP, java, language etc. When a number is placed into the single or double quote then its also a string so it can’t be used in arithmetic. You briefly looked at the concept of strings in Previous section.

PHP provides a lot of pre-define function for string manipulation like strlen, strrev, htmlspecialchars etc.

Some valid string are

"hello and welcome to php"

There are many numbers of functions that are directly concerned with manipulating strings

  • Search for text within a string
  • Calculate the string length
  • Break a string down into component parts
  • Formatting of strings

String example


$str =" And welcome to php string " ;
echo " $str "."
"; echo " Hello, $str! " . "
" ; echo " Hello, $str! " . "
"; echo " hi friends" . "
"; echo " length:-".strlen($str); ?>

In the above example, $str is a string variable and print the string with the help of echo construct. srtlen is a built-in function that is used to calculate the length of string.

Creating and Accessing Strings

As you learned in previous section, creating a string variable is as simple as assigning a literal string value to a new variable name.

  • Both single and double quote are used to create string.


$my_variable = " hello " ;

Illustrate with example


$myvariable = " Welcome to php string " ;

echo "$myvariable "."
"; echo 'Example of php'; ?>

Creating more complex string expression


$car = "Audi R8";

echo "My favourite car is $car"."
"; echo "My favourite car is ${car}"."
"; echo "My favourite car is ${car}"."
"; ?>

$car is a string variable that concatenate with another string "my favourite car" is Audi R8". "

Strings Functions

In this section we will learn some of string functions like strlen, strsrt, strpos etc.

Php strlen() function

You can find out the length of the string using strlen() function.

Syntax of strlen function

Strlen( $expression );

Strlen example


$element = strlen("PHP");

echo $element."
"; echo strlen("welcome"); ?>

You can calculate the length of the string with the help of srlen function by passing the string into the strlen function as shown above.

Searching Strings with strstr() function

Find out whether some text occurs within a string or not, using strstr() function ,if string match print all the string from where string matched. If word is not found strstr function return false.

Syntax of strstr() function

Strstr( $expression ,search text );

Strsrt example


$string1= "Hello world!";

echo strstr( $string1, "H" )."
"; echo ( strstr( $string1, "xyz" ) ? "Yes" : "No" ) ."
"; $string2= "Welcome to php string"; echo strstr( $string1, "java" ) ."
"; //nothing print echo ( strstr( $string2, "php" ) ? "Yes" : "No" ) ."
"; ?>

Accessing Characters within a String

You might be wondering how you can access the each characters of a string. PHP makes this easy for you. To access a character at a particular position.


$char = $str [ position ] ;

String example


$myStr = "Welcome to the php string";

echo $myStr[0] ."
"; // print "W" echo $myStr[6] ."
"; // print "e" $myStr[25] = '?';//Welcome to the php string? echo $myStr ."
"; ?>

You can easily print any character of the string and also add some or replace the the character with the help of index number.


Crafty men condemn studies, simple men admire them, and wise use them.