PHP Strings

PHP Strings

Hope this article find you well.

Today we will learn about the PHP Strings.

So read the article till the end and i will make sure you know everything about PHP Strings.

Before i proceed, i would request you to do SUBSCRIBE to so that you can get regular updates.

So lets start…..

What is a String?

A string is a sequence of characters, like “Hello ShoutMe360!“.

Below are some valid examples of string:

$string_1 = “This is a double quoted string”;
$string_2 = ‘This is a little longer, singly quoted string’;
$string_39 = “This string has thirty-nine characters”;
$string_0 = “”; // a string with zero characters

Single Quoted Strings :

Single quoted strings are the simplest way to specify a string. Single quote is generally faster, and everything quoted inside treated as plain string.

This method is used when we want the string to be written exactly as it is.

When string is specified in single quotes PHP will not evaluate it or interpret escape characters except single quote with backslash (‘) and backslash(\) which has to be escaped.

echo ‘Hello \’ShoutMe360\’!!’;
//Output: Hello ‘ShoutMe360’!!

Double Quoted Strings:

Doubly quoted strings replace variables with their values as well as specially interpreting certain character sequences.

It is litter bit slower than Single Quoted string but does not affect performance.

$count = 360;
echo “Hello ShoutMe$count”;
//Output: Hello ShoutMe360

Actually there are no defined limits on string length.

Some character sequences beginning with backslash (\) are replaced with special characters

Variable names (starting with $) are replaced with string representations of their values.

The escape-sequence replacements are :

\n is replaced by the newline character
\r is replaced by the carriage-return character
\t is replaced by the tab character
\$ is replaced by the dollar sign itself ($)
\” is replaced by a single double-quote (“)
\\ is replaced by a single backslash (\)

String Concatenation Operator

To concatenate two string variables together, use the dot (.) operator in PHP


echo $string1 . ” ” . $string2;

This will produce the following result:

ShoutMe 360

Above in the code, you can see that we have used the concatenation operator two times. This is because we had to insert a third string.

Between the two string variables we added a string with a single character, an empty space, to separate the two variables.

Get The Length of a String

The PHP strlen() function returns the length of a string.

The example below returns the length of the string “ShoutMe360”:


echo strlen(“ShoutMe360”); // outputs 10
The output of the code above will be: 10.

Count The Number of Words in a String

The PHP str_word_count() function counts the number of words in a string:


echo str_word_count(“ShoutMe360 !!!”); // outputs 2
The output of the code above will be: 2.

Reverse a String

The PHP strrev() function reverses a string:


echo strrev(“ShoutMe360”); // outputs 063eMtuohS
The output of the code above will be: 063eMtuohS.

Find Word in a String

The strpos() function is used to search for a string or character within a string.


echo strpos(“Shout Me 360″,”Me”);

The output of the code above will be: 6

Hope this explains you enough about the PHP Strings.

This is Lalit signing off for today.

If you like this post please give a thumbs up and keep it sharing.

Hey don’t forget to SUBSCRIBE please so that you can get notification on new posts.


Leave a Reply