PHP escape characters

Question

Answers ( 1 )

    0
    2024-01-11T18:05:20+00:00

    Here are some common escape characters in PHP:

    1. \' - Single Quote: Used to insert a single quote in a string that is enclosed in single quotes.

      $str = 'It\'s a beautiful day.';
      
    2. \" - Double Quote: Used to insert a double quote in a string that is enclosed in double quotes.

      $str = "He said, \"Hello!\"";
      
    3. \\ - Backslash: Used to insert a backslash in a string.

      $path = "C:\\xampp\\htdocs";
      
    4. \n - New Line: Inserts a new line (line break).

      $str = "Line 1\nLine 2";
      
    5. \r - Carriage Return: Mainly used in Windows systems to signify the end of a line.

      $str = "Line 1\rLine 2";
      
    6. \t - Tab: Inserts a tab character.

      $str = "Column 1\tColumn 2";
      
    7. \xNN - Hexadecimal Representation: Allows the insertion of a character represented by its hexadecimal value.

      $str = "\x41"; // Represents 'A'
      
    8. \u{NNNN} - Unicode Characters: Allows the insertion of Unicode characters using their code points.

      $str = "\u{1F601}"; // Unicode character for a smiling face emoji
      

    It's important to note that escape characters are crucial when dealing with strings that include special characters which could otherwise break the syntax or change the intended output of the code.

Leave an answer