Skip to main content

Escape Sequence in PHP

Escape sequence in PHP means preventing PHP from ending strings early or making sure you have the correct string information returned.

For example:

<?php

    $string='Rita's new house';
    echo $string;
?>

Here, there is no any syntax error. We have just written a normal code. But while running this code it will give an error message.

Why?

Because there is three single quote marks in the above statement. PHP gets confused since it doesn't know what is the string.

To solve such problem, you can use double quotes on the outside. Let's write the above code again.

<?php
    $string="Rita's new house";
    echo $string;

?>

Well it will run perfectly but if you don't want to use double quote outside the single quote then you can escape the apostrophe. You can use excape it by a character by typing a "slash"(\) before it like below:

<?php

    $string='Rita\'s new house';
    echo $string;
?>

Now run this, it should run perfectly fine.

So now you should know the concept about escape sequence, it is used to print the output where PHP have special meaning of.

Below are some other use of backslash as escape sequence.

\" -> It prints the next character as a double quote, not a string closer
\' -> It prints the next character as a single quote, not a string closer
\n -> It prints a new line character
\t -> It prints a tab character
\$ -> It prints the next character as a dollar sign but not as a part of a variable

\\ -> It prints the next character as a backslash but not as a escape sequence character

Comments

Popular posts from this blog

MySQL Connection

Connection with MySQL Database Before accessing database, you must create a connection to the database Syntax: mysql_connect(servername,username,password); where, servername specifies the server to connect to. Default value is “localhost” username specifies the username to log in with. Default value is the name of the user that owns the server process. To connect offline we use username “root”. password specifies the password to log in with. Default is “” Code : Creating connection to the database and selecting the required database <?php $con = mysql_connect(“localhost”,”root”,”"); if (!$con) { die(‘Could not connect: ‘ . mysql_error()); } else{ mysql_select_db(“test”, $con) }; ?> Here, we have store connection in a variable called $con and trap error using die function. Closing connection The connection will be closed automatically when the script ends. To close the connection before, use the mysql_close() function: <?php $con = mysql_conne...

Join in MySQL

Join clause is used to combine rows from two or more tables depending upon common field between them. There are different kinds of Joins. The most common and useful Join is Inner Join. Inner Join: It will return all rows from multiple tables where the condition is met.

Type Juggling and Type Casting

Type Juggling: PHP does not require (or support) explicit type definition in variable declaration; a variable's type is determined by the context in which the variable is used. That is to say, if a string value is assigned to variable $var, $var becomes a string. If an integer value is then assigned to $var, it becomes an integer.