PHP 7 Strings

String is an arrangement of characters. PHP has a good combination of built-in functions for string handling and for easily manipulation of strings.

Like by using php string built-in functions, we can -
  • a. Get length of string.
  • b. Reverse a string.
  • c. Convert the string in lowercase or uppercase.
  • d. Split a string.
  • e. Replace some part or character of a string.
  • f. Trim whitespaces from beginning and end of the string.
  • g. Convert the string in encoded form.
  • h. Get position of substring or a character in a string.

Lists of all built-in functions are given in PHP official website

Some of the uses of built-in functions are -


Echo function is used to output string.

    $str1 = "Hello John, How are you?";
    echo ($str1);


This function is used to get length of a string.

    $str = 'India';
    echo strlen($str); // Output 5


strcmp is used to compare two strings. It takes two strings as parameter.

    $str1 = "hello";
    $str2 = "Hello";
    $str3 = "";
    $str4 = "lo";

    echo strcmp($str1, $str2); 
    echo strcmp($str3, $str4); 
    echo strcmp($str1, $str4); 


This is used to find the first occurrence of a substring in a string.

    $str1 = "etutorialspoint";
    $str2 = "tutorial";
    $getpos = strpos($str1, $str2); 
    echo $getpos; // output 


convert all strings to lowercase.


convert all strings to uppercase

    $str1  = "Hello World";
    echo $strlowercase  = strtolower($str1 ); // Output hello world
    echo $struppercase = strtoupper($str1 ); // Output HELLO WORLD   

Practice Exercises