PHP – Strings,They are sequences of characters, like “PHP helps string operations”.
NOTE − Built-in string features is given in characteristic reference PHP String Functions
Following are legitimate examples of string
$string_1 = "This is a string in double quotes";
$string_2 = "This is a somewhat longer, singly quoted string";
$string_39 = "This string has thirty-nine characters";
$string_0 = ""; // a string with zero characters
Singly quoted strings are treated nearly actually, whereas doubly quoted strings replace variables with their values in addition to specifically interpreting certain person sequences.
<?php
$variable = "name";
$literally = 'My $variable will not print!\\n';
print($literally);
print "<br />";
$literally = "My $variable will print!\\n";
print($literally);
?>
This will produce the subsequent result −
My $variable will not print!\n
My name will print!\n
There are no artificial limits on string duration – within the bounds of to be had reminiscence, you ought in order to make arbitrarily lengthy strings.
Strings which might be delimited through double prices (as in “this”) are preprocessed in both the following two approaches by way of PHP −
- Certain individual sequences starting with backslash () are replaced with unique characters
- Variable names (starting with $) are replaced with string representations in their values.
The break out-collection 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 (\)
PHP – Strings,String Concatenation Operator
To concatenate string variables collectively, use the dot (.) operator −
<?php
$string1="Hello World";
$string2="1234";
echo $string1 . " " . $string2;
?>
This will produce the following result −
Hello World 1234
If we study the code above you see that we used the concatenation operator two instances. This is because we needed to insert a 3rd string.
Between the two string variables we added a string with a unmarried man or woman, an empty space, to split the 2 variables.
PHP – Strings,Using the strlen() function
The strlen() function is used to discover the length of a string.
Let’s locate the length of our string “Hello world!” −
<?php
echo strlen("Hello world!");
?>
This will produce the following result −
12
The duration of a string is frequently used in loops or other features, while it’s miles critical to know whilst the string ends. (i.E. In a loop, we would want to forestall the loop after the remaining person in the string)
PHP – Strings,Using the strpos() function
The strpos() function is used to search for a string or person inside a string.
If a in shape is determined within the string, this characteristic will return the placement of the primary match. If no suit is located, it’ll return FALSE.
Let’s see if we can discover the string “international” in our string −
Using the strpos() function
The strpos() function is used to search for a string or person within a string.
If a fit is located in the string, this characteristic will return the position of the first suit. If no fit is discovered, it will go back FALSE.
Let’s see if we are able to discover the string “world” in our string −
<?php
echo strpos("Hello world!","world");
?>
This will produce the following result −
6
As you see the location of the string “world” in our string is role 6. The cause that it is 6, and not 7, is that the first position in the string is 0, and not 1.