PHP

PHP Strings

PHP Strings

In the PHP variable type section, you must have got some idea about string. The difference between an array and a string is that string has no limit. Below are some escape sequence replacements.

  • \n   - used to replace by the newline character 
  • \r  - used to replace by the carriage-return character
  • \t  - used to replace by the tab character
  • \$- used to replace by the dollar sign ($)
  • \”  - used to replace by a single double-quote(“)
  • \\ - used to replace by a single backslash(\)

String concatenation operator 

the dot (.) operator is used to concatenate two string variables. In a case, if we want to insert a third-string we use this concatenation operator two times.

To separate the two variables we add a string with a single character and an empty space between the two string variables.

Strlen() function 

This function strlen() is used to find the length of a string. You will understand it with the help of the universal example "Hello world!"

INPUT-

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

OUTPUT- 12

The length of the string is used in loops and other functions where it is necessary to know when the string ends. For example, if we want the loop to stop after the last character of the string. 

Strops() function

This function strops() is used to search for a string or a character inside the string. So when the match is found in the string the position of the first match is returned. In a case, if the match is not found the result is returned as false.

Let us try if we can find string “world!”in the string.

Input-

 <?php

   echo strpos("Hello world!","world");

?>

Output-  6

The position of the string "world" in the string is 6. As the first position in the string is 0, not 1.