Strings in PHP

S
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.

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

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

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

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

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

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

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

<?php
// Separate the string based on space characters " "
$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.

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