We now have a youtube channel. Subscribe!

Strings in PHP

String in PHP


Hello folks! welcome back to a new section of our tutorial on PHP. In this section of our PHP tutorial, we will be studying about PHP Strings.

Strings are sequences of characters, like "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


Strings with single quote are treated almost literally, whereas strings with double quote replace variables with their values and also specially interpreting some character order.

Example

The following below is a simple example -

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

Output

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

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


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

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

  • Certain character sequences starting with backslash (\) are replaced with special characters.
  • Variable names (that starts with $) are replaced with string representation 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 (\)


PHP String Concate Operator

String Concatenation Operator

To concatenate two string variables, make use of the dot (.) operator -

<?php
   $string1="Hello World";
   $string2="123456";
   
   echo $string1 . " " . $string2;
?>

Output

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

  Hello World 123456

If we look at the above code you notice that we made use of 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 one character, an empty space, to separate the two variables.


Using the strlen() function

The strlen() function finds and then returns the length of a string.

Example

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

<?php
   echo strlen("Hello world!");
?>

Output

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

  12

The length of a string is often used in loops or other PHP functions, when it is important to know when 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, it will return the position of the first match. If no match is found, it will return False.

Example

Let us see if we can find the string "world" in our string -

<?php
   echo strpos("Hello world!","world");
?>

Output

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

  6

As you see the position of the string "world" in our string is 6. The reason that it is 6, and not 7, is 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 we 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.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain