Horje
How to use Escape Characters in PHP ?

Escape characters in PHP are special characters that allow you to include non-printable characters, represent special characters, or format strings in a way that might be challenging with regular characters alone. They are preceded by a backslash \. In this article, we’ll explore how to use escape characters in PHP, covering various scenarios and providing detailed descriptions with examples.

Method 1: Escaping Special Characters

Escape characters are often used to include special characters within strings without disrupting the code. For example, include a double quote within a double-quoted string.

PHP
<?php 

$str = "This is a \"quote\" inside a string."; 
echo $str; 

?>

Output
This is a "quote" inside a string.

Here, the escape character \” allows the double quote to be part of the string without ending it.

Method 2: Newline and Tab Characters

Escape characters are handy for introducing newlines (\n) and tabs (\t) within strings.

PHP
<?php 

$str = "Line 1\nLine 2\nLine 3\n"; 
echo $str; 

$str2 = "Name\tAge\tCity"; 
echo $str2; 

?>

Output
Line 1
Line 2
Line 3
Name    Age    City

Method 3: Backslash Itself

If you need to include a backslash itself within a string, you can use \\.

PHP
<?php 

$str = "This is a backslash: \\"; 
echo $str; 

?>

Output
This is a backslash: \

Method 4: Unicode Characters

Escape characters also support Unicode characters using the \u prefix followed by the hexadecimal representation of the Unicode code point.

PHP
<?php 

$str = "This is a smiley face: \u{1F604}"; 
echo $str; 

?>

Output
This is a smiley face: ????

Method 5: Variable Variables

Escape characters can be used with variable variables to dynamically create variable names.

PHP
<?php 

$baseVar = "Hello"; 
$dynamicVarName = "baseVar"; 
echo $$dynamicVarName; 

?>

Output
Hello

Method 6: Single Quoted Strings

Escape characters behave differently in single-quoted strings compared to double-quoted strings. In single-quoted strings, only the backslash itself (\\) and the single quote (\’) need to be escaped.

PHP
<?php 

$str = 'This is a single quote: \''; 
echo $str; 

?>

Output
This is a single quote: '

Method 7: Heredoc and Nowdoc Syntax

PHP offers Heredoc and Nowdoc syntaxes for working with multi-line strings, which can include escape characters for various purposes. Heredoc syntax behaves similarly to double-quoted strings, while Nowdoc syntax behaves similarly to single-quoted strings.

Example:

1. Heredoc Syntax:

Heredoc syntax allows the inclusion of escape characters and variable interpolation within a multi-line string.

PHP
<?php
$variable = "example";
$heredocString = <<<EOD
This is an "example" of a Heredoc string.
It can include newlines \n and tabs \t.
Variable interpolation: $variable
EOD;

echo $heredocString;

?>

Output
This is an "example" of a Heredoc string.
It can include newlines 
 and tabs     .
Variable interpolation: example

2. Nowdoc Syntax:

Nowdoc syntax is similar to Heredoc but behaves like single-quoted strings, meaning it does not parse escape characters or variable interpolation.

PHP
<?php
$nowdocString = <<<'EOD'
This is an 'example' of a Nowdoc string.
It treats newlines \n and tabs \t as literal text.
Variable interpolation: $variable
EOD;

echo $nowdocString;

?>

Output
This is an 'example' of a Nowdoc string.
It treats newlines \n and tabs \t as literal text.
Variable interpolation: $variable





Reffered: https://www.geeksforgeeks.org


Geeks Premier League

Related
PHP Program to Print Pascal&#039;s Triangle PHP Program to Print Pascal&#039;s Triangle
Complex Numbers in PHP Complex Numbers in PHP
PHP Program to Convert Enum to String PHP Program to Convert Enum to String
How to Convert Int Number to Double Number in PHP ? How to Convert Int Number to Double Number in PHP ?
What are HTML Tags ? What are HTML Tags ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13