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

CSS List

CSS allows you to customize the lists that can be made with HTML  to the extent that even images can be used as bullet points for unordered lists. CSS List Style Type If you want to use something different from the default numbering of ordered lists, or the bullets/discs of unordered lists, then all you have to do is choose a different style for your lists. CSS allows you to select from a wide variety of different list item shapes. Unordered list styles: square, circle, disc (default), and none Ordered list styles: upper-alpha, lower-alpha, upper-roman, lower-roman, decimal (default), and none Code:

Accessible Forms

Forms aren't the easiest of things to use for people with disabilities. Navigating around a page with written content is one thing, hopping between form fields and inputting information is another. Because of this, it is a good idea to add a number of elements to the form. Labels Each form field should have its own label. The label tag sorts this out, with a for attribute that associates it to a form element:

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.