echo "This example shows \"escape sequences\" in php string, like quotes etc";
echo 'This example shows \'escape sequences\' in php string, like quotes etc';
You can be confused if you think, what to do to put a slash in php string (\), because
it has some special meaning. But I have solution for you: this character also have some custom
escape sequence, and this sequence is: "\\".
PHP Coding tips & tricks. Function & error explanation, coding style, how tos etc. Short notes posted directly from developer work environment.
PHP Coding Tips
Thursday, March 21, 2013
How to quote in echo or print in PHP?
If you are creating applications in PHP, even if you are beginner, you definatelly see echo function already.
Echo function just outputs it's argument to a user browser or to a console/terminal if PHP script is running in
the CLI mode. The trouble rises, when you have to put ' or " sign in a echo, because you have to use one
of this characters just to begin and terminate echo literal. To embed such characters in displayer string,
you need to use escape sequence. It is a special characters combination, that makes some special sense in
string literal. So, if you want to send quote sign to user, not to terminate echo string, use \" escape sequence.
If your sequence is terminated by single quotes ('), then you can also include this character in string
by escape sequence \'. See examples below:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment