Strings in PHP

File handling in PHP
Operators in PHP

Strings in PHP are words formed by characters. For example ‘P’ is a character
and ‘PHP is cool’ is a string. The string function in PHP has tons of built-in functions. In this article we will discover when and how to use strings and its functions.

How to use a string in PHP?

The string is a sequence of characters enclosed in single or double-quotes. The String is a primitive data type in PHP, which means that you don’t have to install any library.

$string = "Hello PHP Developers!";

PHP string functions

In this section we will discover the most important string functions PHP has to offer.

String Length

The strlen function returns the length of a string which is passed as a parameter into this function. The following example shows how to get the length of string.

echo strlen("Hello world"); // outputs 11

One thing you will want to keep in mind when using the strlen function is that it sees the space as a character. So this means that the character is also being counted.

String word count

The str_word_count function returns the number of words in a string. This function is mostly used to iterate the string or to split the string.

echo str_word_count("Guess the number of words in this string"); // outputs 8

Unlike the strlen function the str_word_count function doesn’t count any spaces. The function recognizes words by the spaces used in a string.

echo str_word_count("Guess_the_number_of_words_in_this_string"); // outputs 1
echo str_word_count("Guess the number of words in this string"); // outputs 8

Reverse a String

You can reverse the string using loop but its more conventient and effeicient way to use strrev() function.

echo strrev("Can you see this"); // outputs sith ees uoy nac

strrev() function actually performs two types of reverse process. It outputs the string starting from the last word to the first word. Also, each word within the string outputs starting from the last character to the first character of the word.

Get position of word in String

strpos() function returns the position of the string specified in the paramters. Look at the following example first.

echo strpos("Where PHP is located?", "PHP"); // outputs 6

In the above example, the function starts counting the position of word “PHP”, passed as second parameter and found it at 6th index. Keep in mind that the starting index is 0.

If the word passed in second parameter is not found in the string passed as first paramter, it will return false.

Replace string characters by other characters

str_replace() function replace the characters by the specified characters. Look at the example below.

echo str_replace("world", "PHP", "Hello world!"); // outputs Hello PHP!
  • The first parameter is the word to be replaced.
  • The Second parameter is the word to replace with.
  • The third parameter is the String to search the word specified in first parameter.

How to concatenate Strings in PHP

To join two or more strings in PHP, use the dot (.) operator. Look at the following example.

$str1 = "I am alone without";
$str2 = "my second string";

$final_str = $str1.$str2; // outputs I am alone without my second string

Convert the string to upper case or lower case in PHP

strtoupper() and strtolower function returns the string in upper case and lower case characters respectively. Look at the example below.

echo strtoupper("i like upper case."); // outputs I LIKE UPPERCASE

echo strtoupper("I DON't LIKE UPPER CASE."); // outputs i dont't like uppercase

If a character is already in uppercase in strtoupper() function, it will remain in upper case in the output. Similarly, if a character is already in lowercase in strtolower() function, it will remain in lower case.

An interesting usecase is empty string in this function. It will output the empty string in this case.

  • Icfirst() – converts the first character of a string to lowercase.
  • ucfirst() – converts the first character of a string to uppercase.
  • ucwords() – converts the first character of each word in a string to uppercase.

Interesting fact – String is array of characters

Like other languages, PHP string is also array of characters. You can parse or iterate through the string like you do with the arrays. Look at the example below in which we will output characters of String by treating it like array.

$str = "I am string array";

for ($int = 0; $i < strlen ($str); $++))
     echo $str[$i];

Note: Following two string functions are depriciated in PHP 8

  • convert_​cyr_​string
  • hebrevc

Split string based on specific separator

PHP explode function is also one of the most useful function for the developers in daily routine. explode() function returns array containing the substring. The array store the substrings on the indexes starting from index 0. Look at the following example of explode function in PHP.

// Separate the string based on space characters " "
$str = "Hello world. It's a cool day.";
print_r (explode(" ",$str));

$str = 'three,four,five,six';

// zero limit

// positive limit

// negative limit

Note: In PHP 8 explode() will return ValueError when separator parameter is an empty string (""). So, this parameter cannot be empty or null.

Form string from arrays of string – reverse of implode function

explode() function works as inverse of implode function. It joins the arrays of strings into one string. Look at the following example to have clear idea.

$arr = array('Hi','World!','Cool','Day!');
echo implode(" ",$arr)."<br>";
echo implode("+",$arr)."<br>";
echo implode("-",$arr)."<br>";
echo implode("X",$arr);

// outputs

Hi World! Cool Day!

The arrays containing the strings are joined together and the joining characters are passed as first parameter of the function.

Next we will learn about operators in PHP.

File handling in PHP
Operators in PHP

Tutor Network

Share this

Learn PHP from A to Z