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.
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:
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 6 years ago.
Afternoon guys!
For some reason Complex (Curly) Syntax in my PHP code doesn't interpreter as variable
function view($name, $data = [])
{
require __DIR__ . '/../app/views/{$name}.view.php';
}
Here is the code I am calling view('index'); the problem is I get this error:
Warning: require(./core/../app/views/{$name}.view.php): failed to open stream
By the way my PHP version before anyone asks is 7.0.13
Thanks for your help!
Your string has to be placed in double quotes (") instead of single quotes ('). Otherwise variables won't be replaced.
Look here:
https://secure.php.net/manual/en/language.types.string.php#language.types.string.syntax.single
Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.
This question already has answers here:
Is there a better way to write HTML strings in PHP?
(8 answers)
Closed 8 years ago.
My PHP code begins with the ?php tag and ends with the ? tag and my file extension is php. If I want to write part of my PHP code inside HTML, what is the general way of doing it?
For instance:
echo '<strong>' .$element .'</strong><br>';
Do I need to always use single quotes for the HTML tags used inside of a PHP file? Or double quotes or no quotes?
The first thing you need to learn is that PHP doesn't care about HTML.
PHP doesn't understand that it's working with HTML, it just sees text and dutifully sends it to the browser. The browser then makes sense of it as HTML.
So, since <strong> is a string, it must be treated as a string. In other words, as "<strong>", '<strong>' or even a heredoc:
<<<HEREDOC
<strong>
HEREDOC;
(Probably not appropriate in this case :p)
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.