String in PHP

Admin   PHP   175  2020-10-16 08:17:40

Strings are sequences of characters, like "PHP is available".

Note - You will find available string handlers in PHP at String Functions in PHP

string in php

String in php

Below are examples of valid strings in PHP.

$string_1 = "Include a string in PHP";
$string_2 = "Learning PHP can be done";
$string_0 = ""; // for example a string with no characters

When using the string with single quotes, it will be treated as constants, and print the string itself. While used with double quotes, it replaces variables with their values ​​as well as interprets specific sequences of characters.

<?php
   $string = "name";
   $string_line = 'The string $string_line  will not be printed! \ n';
   
   print ($string_line);
   print "<br />";
   
   $string_line = "The string $string_line will be printed! \ n";
   
   print($string_line);
?>


Save the above program in a file called test.php in htdocs , then open a browser and type http://localhost/test.php will produce the following result:

Strings in PHP

There is no limit to the length of the string, which can be arbitrarily long if the memory allows.

Strings delimited by double quotes are handled by PHP in two ways:

Character strings beginning with (\) are replaced with a special character

Variables (starting with $) are replaced with a string representation of their value.

Alternate rules:

\ n is replaced with the newline character.
\ r is replaced with the carriage-return character, which is interpreted to put the cursor at the beginning of the line but not with a carriage return.
\ t is replaced by a tab character
$ is replaced with a $ sign
\ "is replaced with a quotation mark"
\\ is replaced with a single quote \

String concatenation operator in PHP

To concatenate two strings, you use the operator. (period) in PHP is as follows:

<?php
   $string1 = "Blog";
   $string2 = " dev";
   
   echo $string1 . " " . $string2;
?>


Save the above program in a file called test.php in htdocs , then open a browser and type http://localhost/test.php will produce the following result:

String concatenation

If you pay attention to the above code, you will see that we used the concatenation operator twice. This is because we have inserted a third string.

Between two string variables, we add a string with a single character, a space, to distinguish the two.

Find string length in PHP - strlen() function in PHP
The strlen() function is used to find the length of a string.

The following example will find the length of the string "Blogdev":

<?php
   echo strlen("Blogdev");
?>


Save the above program in a file called test.php in htdocs , then open a browser and type http://localhost/test.php will produce the following result:

Find string length in PHP

The length of a string is often used in a loop or other functions, when it is important to know when the string ends (e.g. in a loop we want to stop the loop after the last character of Chain).

Find a character or string in a string - The strpos() function in PHP

The strops() function is used to find a string or character within a string.

If a match is found, the function returns the position of the first match. If no match is found, it returns FALSE.

Now we find the string "eam" in the string "Blogdev Team!":

<?php
   echo strpos("Blogdev Team!","eam");
?>

Save the above program in a file called test.php in htdocs, then open a browser and type http://localhost/test.php will produce the following result:

Find strings in PHP

As you can see, the position of the string "eam" in the above string is 10. The reason is 10 but not 11, because the first position of the string is 0, not 1.