PHP string function

PHP String: Useful functions

PHP string is sequences of characters placed together. The amount of memory that one character occupies is one byte. In PHP, strings are actually an array of characters. In this article, we will introduce some of the most used functions related to strings in PHP.

Definition of PHP string

The easiest way to specify a string is to enclose it in single quotes (the ‘ character) or double quotes (the ” character). But single quotes are more useful.

$str = 'Honar Systems';
$str = "Honar Systems";
$str = 'Honar Systems is "Awesome"';
$str = 'Honar Systems\'s themes';
$str = 'Honar Systems\n';

If we define the string with the single quotation, we can use double quotation inside the string. If we want to have a single quote inside the string, we must use a backslash (\) before the single quote.

The expression “\n” is not the letter n, but a new line. If we put a backslash before some letters, their meaning changes completely and they are no longer used as letters. Like n, t, etc.

echo <<<END
      Honar 
     Systems
END;

This method is also one of the methods of string definition (Heredoc), but it is less useful.

If we have used double quotation or Heredoc to define the string, we can use the variable inside the string directly.

$val = "Systems";

$arr = array(1, 2, 3, 4, 5);

echo "Honar $val is #$arr[0] and the third letter from the second part is: $val[2]";
--------------------
Honar Systems is #1 and the third letter from the second part is: s

Note: Strings are arrays of characters.

To read more about arrays, visit our PHP array article.

String operators

In PHP, 2 operators are defined for strings.

echo 'Honar ' . 'Systems';
------------------------
Honar Systems

Operator (.) is used to concatenate strings. In this example, two strings are joined together.

$val = 'Honar';
$val .= 'Systems';
echo $val;
------------------
Honar Systems

The operator (.=) is also used to append the string, with the difference that it appends the second string to the end of the first string. This operator is actually shortened to the first operator.

PHP string functions

strlen() function

This function returns the length of the string.

echo strlen("Honar Systems");
--------------
13

str_word_count() function

This function declares the number of words in a string.

echo str_word_count("Honar Systems");
--------------------
2

strrev() function

This function reverses the desired string. That is, the string is displayed from the end to the first.

echo strrev("Honar Systems");
-----------------
smetsyS ranoH

strpos() function

It is used to search a string inside another string. If the function finds the desired string, it returns the first position (index) of the string, otherwise, it returns false.

echo strpos("Honar Systems","ar");
------------
3

Like arrays in PHP, the index of strings in PHP also starts from 0.

str_replace() function

This function replaces a string inside another string.

echo str_replace("Systems", "Software", "Honar Systems");
-------------------
Honar Software

The first parameter is the string that we want to replace with another string. The next option is the string we want to replace. The last parameter is our main string.

substr()

This function returns the specified part of the string.

echo substr("Honar Systems", 3, 4);
---------------------
ar S

It returns 4 characters from the third index of the string which is the fourth character.

strval() function

Convert number to string.

echo strval(4);
------------
4

654بسی6ب46یسشل4یب5بی5ب5ل6بی5ل6dfsgdf

Shopping Cart