Wednesday, September 23, 2020

Strings in PHP



Hello dear readers! welcome back to another section of my 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.

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

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

Output

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.

Example

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

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

Output

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

  12

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.

Example
Let's now 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

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 my next tutorial post, 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.
Share:

0 comments:

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.