Introduction

The string is one of the primitive types of PHP with a ton of built-in functions in PHP 8. In this article, you will learn PHP String data types and their functions in-depth.

What is a String & How to use 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 so, you don’t need to install any library.

$string = "Hello PHP Developers!";

PHP String functions

In this section, you will learn about used PHP String functions that a PHP developer may need in daily routine.

String Length

Strleng() function returns the length of string passed as a paramtere into this function. Following example shows, how to get the length of string.

<?php
echo strlen("Hello world"); // outputs 11
?>

Keep in mind that that strlen() function consider the space as one character so, if you have spaces between the characters, they will be counted in the length of the string.

String word count

str_word_count() function returns the number of words in a string. This function mostly used to iterate the string or to split the string. Don’t worry, we will learn how to split the string in PHP.

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

Unlike strlen() function, str_word_count() does not count spaces as the word. Each word is determined by space. If you don’t give space between the characters, its length will be one.

<?php
echo str_word_count("Guess_the_number_of_in_this_string"); // outputs 1
?>

Reverse a String

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

<?php
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.

<?php
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.

<?php
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.

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

<?php
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 " "

<?php
$str = "Hello world. It's a cool day.";
print_r (explode(" ",$str));
?>

<?php
$str = 'three,four,five,six';

// zero limit
print_r(explode(',',$str,0));

// positive limit
print_r(explode(',',$str,2));

// negative limit
print_r(explode(',',$str,-1));
?>

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.

<?php
$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!
Hi+World!+Cool+Day!
Hi-World!-Cool-Day!
HiXWorld!XCoolXDay!

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