String (String) in PHP

String is a sequence of characters, you will find available string handling functions in PHP at PHP String (String) handler

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 1String (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:

  1. Character strings beginning with a forward slash () are replaced with special characters
  2. Variable names (starting with $) are replaced by the string representation of its value.

    Replacement rules:
  3. n is replaced with newline character (new line)
  4. 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.
  5. t is replaced by tab characters
  6. $ is replaced with a $ sign
  7. "replaced with a quotation mark"
  8. 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

4.5 ★ | 2 Vote