Strings in PHP

Hello dear readers! welcome back to another section of our tutorial on PHP. In this section of our PHP tutorial, we will be studying about Strings in PHP.

Strings can simply be described as sequences of characters. E.g., "PHP supports string operations".

Following are valid examples of string -

$string_1 = "This is a string in double quotes";
$string_2 = "This is a somewhat longer, single quoted string";
$string_39 = "This string has thirty-nine characters";
$string_0 = ""; // a string with zero characters

RECOMMENDED: A Guide to PHP Arrays

Single quoted strings are treated almost literally, whereas double quoted strings replaces variables with their values and also specially interpreting some character order.


The following below is a simple example -

   $variable = "name";
   $literally = 'My $variable will not print!\\n';
   print "<br />";
   $literally = "My $variable will print!\\n";


When the above code is executed, it will produce the following result -

My $variable will not print!\n
My name will print!\n

RECOMMENDED: PHP Decision Making

There are no artificial limits on string length - within the bounds of available memory, you ought to be able to produce randomly long strings.

Strings are delimited by double quotes are preprocessed in both the following two ways by PHP -

  • Certain character order that begins with backslash(\) are replaced with some special characters.
  • Variable names (that starts with $) are replaced with the string presentation of their values.

The escape-order replacements are -

  • \n is replaced by the newline character
  • \r is replaced by the carriage return character
  • \t is been replaced by the tab character
  • \$ is replaced by the dollar sign itself ($)
  • \" is replaced by the single double quote (")
  • \\ is replaced by the single backslash (\)

String Concatenation Operator

In order to concatenate two string variables, use the dot (.) operator -

   $string1="Hello World";
   echo $string1 . " " . $string2;


When the above code is executed, it will produce the following result -

  Hello World 123456

If we look at the above code you will see that we made use of the concatenation operator twice. This is because we had to insert a third string.

In the middle of the two string variables we added a string with a single character, and an empty space used to separate the two variables.

Using the strlen() function

The strlen() function is used for finding the length of a string.


Let us now find the length of our string "Hello world!" -

   echo strlen("Hello world!");


When the above code is executed, it will produce the following result -


The length of a string is often used in loops or other functions, when it is important to know the string ends.

Using the strpos() function

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

If a match is found in the string, this function is going to return the position of the first match. If no match is found, it will return False.

Let's now see if we can find the string "world" in our string -

   echo strpos("Hello world!","world");

When the above code is executed, it will produce the following result -


From the output, the position of the string "world" in our string is 6, and not 7. Also the first string position is 0, and not 1.

Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP Web Concept.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.