How to include text next to php variable in heredoc text [duplicate] - php

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

Related

How to write HTML code inside PHP [duplicate]

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)

What does echo <<<output do? [duplicate]

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.

Is it possible to merge PHP code inside EOF variable? [duplicate]

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.

PHP equivalent to python's triple-quotes - How to print bulk / lots of HTML within PHP without escaping [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php string escaping like python’s “”“ ”“”?
The triple-quotes in python escapes all quotes and newlines contained within. For example,
""" this
is all
just one string. I can even tell you "I like pi".
Notice that the single quotes are escaped - they don't end the string; and the newlines become part of the string
"""
Does anybody know if PHP has an equivalent to python's
""" <form><h1>PUT HTML HERE</h1> </form> """
enter code here
EDIT:
For those looking at this question in the future, I have answered it, here is an example:
$heading = "Heading Gettizburgz";
print <<< END
<p><h1>$heading</h1> "in quotes" 'in single'
Four score and seven years ago<br/>
our fathers set onto this continent<br/>
(and so on ...)<br/>
</p>
END;
prints:
Heading Gettizburgz
"in quotes" 'in single' Four score and seven years ago
our fathers set onto this continent
(and so on ...)
Note one important thing, you must make sure that the very last END is to the far left (fist column) of your code without ANY spaces before it.
source: http://alvinalexander.com/blog/post/php/php-here-document-heredoc-syntax-examples
You can use heredocs or nowdocs (see below heredocs).
Heredoc
$bar = <<<EOT
bar
EOT;
Nowdoc
$str = <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
EOD;

What does EOD acronym stand for? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the use of <<<EOD in PHP?
I know how to use it, I know it's used to mark Heredoc's syntax to easily print multiple lines of string, but what is full name of that EOD acronym (if it's acronym) ?
Only thing I heard is End Of Data, but I'm not sure if it's good.
EOD isn't part of the Heredoc syntax. It's just used in their example.
$foo = <<<JAVASCRIPT
alert('Hello!');
alert('World!');
JAVASCRIPT;
This example would echo the javascript back to the user (or in other words, until the Token JAVASCRIPT is reached).
The heredoc identifier can be chosen at will:
http://php.net/manual/en/language.types.string.php
[T]he identifier must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.
So it might just mean "End Of Data" as well as "Explosive Ordnance Disposal", but it doesn't really matter, it's jsut an identifier, and it could as well have been _Fo0b4R as any other (valid) identifier.

Categories