String (String) in PHP
The string is a sequence of characters, just like "Learn PHP with QTM technology".
Note - You will find available string processing functions in PHP at PHP String (String)
Below is an example of valid strings in PHP.
$ string_1 = "In PHP";
$ string_2 = "Learning PHP transfer technology in QTM";
$ string_0 = ""; // for example, a string with no characters
When using a string with single quotes, it will be treated as constants, and print the same string. While using with quotation marks, it replaces variables with their values as well as interprets specific character sequences.
";
$ bien_chuoi_hang = "The string $ bien_chuoi will be printed! n";
print ($ bien_chuoi_hang);
?>
Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:
String (String) in PHP Picture 1
There is no limit to the length of the string, it can be arbitrarily long if the memory allows.
Strings limited by double quotes are handled by PHP in two ways:
- Character strings beginning with a forward slash () are replaced with special characters
- Variable names (starting with $) are replaced by the string representation of its value.
Replacement rules: - n is replaced with newline character (new line)
- The r is replaced by the carriage-return character, which is meant to bring the cursor to the beginning of the line but not the line.
- t is replaced by tab characters
- $ is replaced with a $ sign
- "replaced with a quotation mark"
- is replaced with a backslash
String concatenation operator in PHP
To join two strings together, you use the operator. (dots) in PHP are as follows:
Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:
Web QTM
If you pay attention to the above code, you will see that we used the string concatenation operator twice. This is because we have inserted a third string.
Between two string variables, we add a string with a single character, which is a space, to distinguish the two variables.
Find the string length in PHP with strlen () in PHP
The strlen () function in PHP is used to find the length of a string.
The following example will find the length of the string "QTM Nhom!":
Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:
8
The length of a string is often used in loops or other functions, when it is important to know when the string ends (for example, in a loop, we want to stop the loop after the last character of Chain).
Find the character or string in the string - strpos () function in PHP
The strops () function is used to find a string or character inside a string.
If a match is found, this function returns the position of the first match. If no match is found, it returns FALSE.
Now we find the "QTM" string in the "Web QTM!" String:
Save the above program in a file named test.php in htdocs , then open the browser and type the address http:/// localhost: 8080 / test.php will result:
4
As you can see, the position of the string "QTM" in the above string is 4. The reason is 4 but not 5, because the first position of the string is 0, not 1.
Follow tutorialspoint
Previous post: Array (Array) in PHP
Next lesson: Insert file in PHP
You should read it
- String (String) in C #
- String (String) in C / C ++
- string.h in C
- String object in JavaScript
- Some ways to tie the pole or everyone should know
- String in C
- How to Manipulate Strings in Java
- Find, FindB functions in Excel - The function finds a text string in another document in Excel
- MID and MIDB functions to cut strings in Excel
- The function takes a string in Excel
- How to insert characters at the beginning or end of a string in Excel
- LEN function in SQL Server