This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
I came across some code in a file I was working that basically has
echo <<<output
"some html here"
output;
What does this do? Normally I do something like
echo "some html here";
but I've run into some cases where I have to use both ' and " and then it breaks the statement to echo, I think the first method would be a way around this but I would like to know what it's actually doing.
Thanks in advance.
You are looking at heredoc notation.
A third way to delimit strings is the heredoc syntax: <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.
Related
This question already has answers here:
Mixing a PHP variable with a string literal
(5 answers)
Closed 4 months ago.
My actual use case involves filling in javascript variable names with partial id's from php, but to illustrate the issue here's a simple example with html:
$var="ello worl";
echo <<<HTML
H$var d
HTML;
I want the output to be "Hello world" however of course there is a space after the $var variable name so the output is "Hello worl d". If I remove the space, then it changes the variable name.
How do I place text next to the right side of the variable?
I've tried quotes and escaping etc. but to no avail.
You can enclose the variable in curly braces ({ and }) to ensure the PHP interpeter knows which characters are part of the variable name and which are static text.
$var="ello worl";
echo <<<HTML
H{$var}d
HTML;
Demo: https://3v4l.org/TYgEF
Documentation reference: https://www.php.net/manual/en/language.types.string.php#language.types.string.parsing
This question already has answers here:
How to use str_replace to replace single and double quotes
(7 answers)
Closed 4 years ago.
I'm having trouble replacing a single quote in a string, the purpose is to create part of an URL
For example : If I type in "Villeneuve d'ascq" I would want to have :
Villeneuve+d%27ascq", %27 being the ascii equivalent of (')
I tried using str_replace("'", ord("'"), string_name) but it doesn't seem to work
Any help would be appreciated and feel free to ask for any more details
Please try this :
echo 'test';
You can also check this at :
PHP MANUAL
This question already has answers here:
PHP quotes inside quotes
(3 answers)
Closed 5 years ago.
hi guys i just want to know if there is an alternative syntax for onclick="document.getElementById('id01').style.display='none'" on php. Beceause its not working even it is inside the echo.tnx
here is my code
First up: document.getElementById is a construct used in JavaScript and has nothing to do with PHP.
The problem you are running into is that you are not escaping the single quotes in your string:
echo '<button onclick="document.getElementById(' <- there is your problem
PHP thinks that the string ends here since you told it so with '.
But the string is in fact not at its end so you need to escape the single quote in order to make it work:
echo '<button onclick="document.getElementById(\'id0\')...';
This goes for all single quotes in your string of course.
This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 7 years ago.
What is better to use in PHP?
' or "? for something like this:
<?=$json["response"]["players"][0]["personaname"];?>
And this:
$steamid = "";
"Better" really depends on your use cases. In both your example, the single quote is better for the perfomance because you have no $variable interpolation needed.
Otherwise "better" may be a question of style (and a little of performance).
Read the documentation for more : http://php.net/manual/en/language.types.string.php
Better? Well, let's just say that an empty string with single quotes is the most basic string literal you can use. In your example shown, I would use single quoted strings for array elements and the empty string. PHP will know that the string literal is only a string literal, and that nothing else needs to be "interpolated." Better? Who knows.
One place to use "" is with escape sequences such as "\n" .
This question already has answers here:
PHP using Gettext inside <<<EOF string
(2 answers)
Closed 8 years ago.
Is it possible to put php code ( NOT ONLY {$SomeVariable} ) inside eof ?
bad syntax but just to get the idea...
$html = <<<EOF
<!docutype html>
<html>
<head>
<title>{ SOME PHP CODE }</title>
</head>
EOF;
No, heredoc strings are the same as double-quoted strings with the exception that the double-quote character does not need to be escaped. Variables are expanded in the same way (including complex variables as well as simple variables as in your example). You cannot run code other than this within the string.
I'd recommend reviewing the strings documentation:
http://www.php.net/manual/en/language.types.string.php
Also your example is invalid, the closing operator (EOF; in this example) cannot be indented.