What does a \" do in php? [duplicate] - php

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
I am very new to PHP and HTML, and I came across some code that has a \" where a select name is defined.
<select name=\"table\" ...
My question is: what is that doing?
Thank you.

It's an escape character. It's used to ensure that the next character is not treated as code, but as part of the string.
In this case, it means that the " is part of the string, and doesn't signify the end of the string within the code.
For example, if I want to put this inside of a string...
He said "shut up"...
I'd write something like this...
$str = "He said \"shut up\"...";
The backslash can also be used for special "sequences", which insert special characters. For example, if I want to insert a newline into the string, I'd use the \n escape sequence. Or, if I want a full CRLF, I'd use \r\n to insert both the carriage return (\r) and the line feed (\n) together. There are many escape sequences, most of which are described here, in the PHP documentation.

It escapes the double quote. Consider this:
echo "Hello world, John said "Hello world!"";
This would not evaluate, because John's quotewould get processed by PHP as syntax, so you use the escape character \ to skip it:
echo "Hello world, John said \"Hello world!\"";
This is only applicable to double quotes, but would also be used for single quotes in the same way:
echo 'John said \'Hello!\'';
... but this would be fine:
echo "John said 'Hello'";

The \ is used as an escape character in many programming langauges, such as PHP, Java, and C#. The escape character allows the usage of special characters, such as " and various special "values" such as \n for a new line.
Here is a reference to Escape sequences in PHP for further reference

The \ is an escape character. In your example, you are outputting HTML, and need to include the quotes, so you use the \ to ignore the quotes and thus create an HTML statement that looks like <select="table"...

Related

Backslashes breaking string in PHP

I have an issue. I'm trying to write a string with ASCII text like this: '/\'. But whenever I do that the backslash screws up the code by canceling out the quote defining it a string therefore screwing it up. Is there anyway to cancel out the backslash so it doesn't cancel out the quote? Thanks guys!
The \ is special character, that says: 'The next character has special meaning'.
So if you want to dispaly \ you should write... \\ to get one \ in output
It would be very helpful to show what you have tried, but this will produce the exact output you requested (as shown by SO)
echo '\'/\\' . "'\n" ;
'/\'
It should also give you an idea of how backslash escaping works in different types of strings.
A great solution when writing stuff like that is HEREDOC. Inside a heredoc block you don't need to worry about escaping anything, it will just be text.
For example:
echo <<<TEXT
/|\/|\/|\/|\/|\/|\/|\/|\/|\/|\
TEXT;
There is one catch. PHP will break if you don't align the echo at the start of the line, or if the TEXT; is not aligned at the start of the line.
Heredoc can also be assigned to a variable, like so:
$var = <<<SOME_MORE_TEXT
/|\/|\/|\/|\/|\/|\/|\/|\/|\/|\
SOME_MORE_TEXT;
Finally, HEREDOC preserves tabs and spaces. Which also might come in handy when doing ASCII art.
Refer to: http://php.net/manual/en/language.types.string.php for more information.
You only need to escape the final one when using single quotes.
$var = 'backslash\backslash\backslash\\';
// output is:
// backslash\backslash\backslash\

How do I echo the original HTML in PHP?

I have some HTML. In my HTML, there is a form that uses a PHP script. When I submit this form, it sends me to a blank page. I have tried this, as suggested in a similar question, but it doesn't work because I have HTML events that call functions with arguments, so both " and ' are already used... I am new to PHP, so, is a there a solution to this that I do not know of?
Use the backslash character \ to escape your quotes like so \". The backslash causes PHP to treat the quote as a regular character. :]
Example:
<?php echo "Hello World"; ?>
outputs Hello World
<?php echo "\"Hello World\""; ?> outputs "Hello World"
Escape your quotes.
echo "<h3>First</h3>"
\" is an escaped quote. When a quote is escaped, the environment treats it as an ordinary character, not as a quote. \' Single quotes can also be escaped if need be.
(Not the best example since I'm not also using singles, but you get the point.)

What does "<<<ST" mean in PHP? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP <<<EOB
I saw this below piece of code in one php file , can some one explain what <<< st means.?
$status['caption']=<<<ST
ST;
P.s : I really cant google it , trust me :D
This is referred to as a heredoc string.
That is a way to store multiline strings. (Called Heredoc Syntax)
$string = <<<IDENTIFIER
IDENTIFIER;
All the lines in between are stored as string. Used for long walls of text.
It is described here.
It is called the Heredoc syntax:
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
It can be helpful for multiline strings and strings containing both double and single quotes.
As double quotes Heredoc interprets many escape sequences for special characters.
The <<< operator stands for the heredoc syntax. It's a way to write strings in a natural way.
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
That's a heredoc string ("text block").
Everything between <<<ST and ST; will be output as it is written. So you could put some HTML you want to output and save a bunch of print() statements or save your self the work of escaping characters like you would with a $variable = " textity text text text"; command.
From php website : Heredoc text behaves just like a double-quoted string, without the double quotes. This means that quotes in a heredoc do not need to be escaped, but the escape codes listed above can still be used. Variables are expanded, but the same care must be taken when expressing complex variables inside a heredoc as with strings.

php singlequote not working on sprintf properly

I use sprintf() on my program to output some tabs and newlines. I noticed a part of my program not working properly.
As I inspected the part that isn't working, I noticed that I used a single quote ' instead of a doublequote " and the program actually outputs a \t instead of a inivisible tab space.
I thought the two are similar and the reason for having two delimeters for php is for us to be able to insert single or doublequote in a string or echo them without inserting escape characters.
Would there be a difference in assigning variables aside from the one I discovered
$a = "qq";
$b = 'qq';
Would they be stored in the computer's memory in a different manner?
you can refer to the manual that specifies that single quotes in php consider most escape sequences as litterals, contrary ot double quotes:
http://php.net/manual/en/language.types.string.php
single quote is faster than double
double quote can parse php variable. i.e. $a=2; and if you use echo "a is: $a"; then it will print a is: 2 but single quote will print a is: $a
if you use single quotes for the format string (like you should do, since there
aren't any variable conversions to do as long as you don't need any special chars),
the given examples won't work because of the backslash before the $ (needs to be
escaped in double quoted strings - but not in single quoted!) http://php.net/manual/en/function.sprintf.php

Can't see new lines on textarea - what could the problem be?

I have a php string with a lot of information to be displayed inside a textarea html element.
I don't have access to that textarea nor to the script (if any) that generates it.
$somestring = 'first line \nSecond line \nThird line.';
$somestring as NOT been "worked" with trim or filter_var. Nothing.
On the textfield, I get the \n printed on the textarea hence, not interpreted.
What can I try in order to have those new lines applied?
Thanks in advance.
Try wrapping $somestring with " (double quotes) instead of ' (single quotes)
\n, \r and other backslash escape characters only works in double quotes and heredoc. In single quotes and nowdoc (the single quote version of heredoc), they are read as literal \n and \r.
Example:
<?php
echo "Hello\nWorld"; // Two lines: 'Hello' and 'World'
echo 'Hello\nWorld'; // One line: literally 'Hello\nWorld'
echo <<<HEREDOC
Hello\nWorld
HEREDOC; // Same as "Hello\nWorld"
echo <<<'NOWDOC'
Hello\nWorld
NOWDOC; // Same as 'Hello\nWorld' - only works in PHP 5.3.0+
Read more about this behaviour in the PHP manual
EDIT:
The reason single and double quotes behave differently is because they are both needed in different situations.
For instance, if you would have a string with a lot of new lines, you would use double quotes:
echo "This\nstring\nhas\na\nlot\nof\nlines\n";
But if you would use a string with a lot of backslashes, such as a file name (on Windows) or a regular expression, you would use single quotes to simplify it and avoid having unexpected problems by forgetting to escape a backslash:
echo "C:\this\will\not\work"; // Prints a tab instead of \t and a newline instead of \n
echo 'C:\this\would\work'; // Prints the expected string
echo '/regular expression/'; // Best way to write a regular expression
$somestring = "first line \nSecond line \nThird line.";
http://php.net/types.string <-- extremely useful reading
this article is a cornerstone of PHP knowledge and it's just impossible to use PHP without it.
unlike most of manual pages which are are just for quick reference, this very page is one which every developer should learn by heart.

Categories