echoing HTML with echo <<< HTML [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a reason to use Heredoc in PHP?
I'm new to PHP.
Is it good practice to use something like that?
echo <<< HTML
$item
HTML;
It does work, but I'm not sure if I'm using it correctly.
Thanks very much...

It is perfectly fine to use single-quotes, double-quotes, heredoc and nowdoc, depending on what you need (formatting, variables inside, etc.). See more info here.

Mixing HTML and PHP in the same file is never a good practice. Separate code and templates by putting them in different files and possibly using a template engine.
However, if you need to put blocks of HTML in a PHP file the heredoc syntax you used in your example is perfectly fine as it avoids the escaping hell you'd have when using regular quotes.
For the various ways of quoting strings have a look at the PHP documentation. It also explains how the various strings behave (e.g. regarding variable interpolation and escape sequences).

Related

What's the difference between ${var} and {$var} [duplicate]

This question already has answers here:
What does ${ } mean in PHP syntax?
(4 answers)
Closed 7 years ago.
In PHP, what is the difference between placing the dollar-sign in front or within the curly brackets:
1. $var = 'Hello World!';
2.
3. echo "${var}";
4. echo "{$var}";
I understand what it is (variable parsing within a string), however there is not a clear explanation between these two differences in the PHP Manual.
The way the variable is parsed on line 3 is supposed to be simple syntax, whilst line 4 is supposed to be complex syntax.
On the PHP Manual however, the syntax used on line 3 is only used as an example for complex syntax which is quite confusing?
Upon a few tests it seems that they both parse variables as complex syntax?
If possible could anyone provide an example of when to use each one?
If there is no difference, then which convention is preferred?
You are talking about Complex syntax. Looking into example shows that the meaning is same for both cases therefore it is up to you to decide which one to use based on your preferences.

What is the difference between "<<<SQL SQL;" and using quotes for a statement [duplicate]

This question already has answers here:
In PHP, what does "<<<" represent?
(8 answers)
Closed 9 years ago.
Could anyone provide some sort of documentation on the differences and or benefits of using
$sql = <<<SQL
SELECT COUNT(ParentGUID)
FROM siteobjects
SQL;
Instead of using just using.
$sql = "SELECT COUNT(ParentGUID)
FROM siteobjects";
Struggling to find any information on this due to searching for "<<
The first one uses HEREDOC syntax. It's useful when you're working with multi-line strings and to avoid quoting problems. To solve the search issue, you can use a programming search engine that doesn't ignore special characters (like SymbolHound).
There's no difference except for the fact that the HEREDOC would have white spaces in the start (because of tabulation).
That's PHP Heredocs syntax (http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc). I prefer heredocs over simple strings because almost any IDE recognize the syntax, and it's more readable, but both options are possible.

Standard encoding for urls for Sitemap [duplicate]

This question already has answers here:
Sitemap urls with special characters [closed]
(6 answers)
Closed 9 years ago.
I want to submit my sitemap to Google, but I don't want to mess anything up. I am also having trouble with the URLs to submit; some of them have special characters in them such as the ampersand (&) symbol and parenthesis (). I just want to know what is the correct way to handle them?
I am currently using PHP's urlencode(), which turns them in to %28, %29 and so on which doesn't really look too good and I am scared if I give Google those links and they go on to index them they will index them as
domain.com/blabla%28blabla.html
Rather than
domain.com/blabla&blabla.html
Are you generating the XML by hand? Please consider using something like the PHP DOM classes instead.
You'll actually want to encode ampersands as &, etc., but it's really best to let a library emit well-formed XML for you.
See Generating XML document in PHP (escape characters) for more discussion of this.
urlencode() is the right function to use. You definately don't want ampersands in your URL because they are a special character used to form a URL (for passing GET variables).

When should I use parenthesis in require/include statements? [duplicate]

This question already has answers here:
Is php's 'include' a function or a statement?
(9 answers)
Closed 8 years ago.
I've been handed a pile of code that includes a lot of require/include statments (mixed between require and require_once). Sometimes, the path has parenthesis around it, i.e. require_once (JPATH_COMPONENT.DS.'controller.php');, and other times there isn't: require_once $path;.
The php docs for include mention this, but aren't specific. Should I remove the parenthesis when I find them, or is it ok to leave them alone? When writing further require/include statements, are there specific cases where I should use them?
You are allowed to use parentheses in 'include/require' not because include allows it itself but because you can use parentheses around any string or number in PHP for grouping.
So for example, "dog" is equivalent to ("dog"), ("dog")."dog" is equivalent to "dog"."dog", etc.
Parentheses become useful when you use complex expressions involving calculations and string concatenations but in such a simple case, they are simply allowed and perform an unnecessary and harmless "grouping" of a single string value.
Both syntaxes are valid. So, it's up to you. :)
The documentation explains:
Because include is a special language construct, parentheses are not needed around its argument.
There is no issue with leaving them or taking them out, at the end of the day it is up to the comfort of the developer.
Personally, I leave them off. I think it looks a little cleaner, and the IDE syntax coloring works a bit better.

Value of omitting ?> in PHP only files [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP closing tag
I've read recently that the ?> should be omitted from files that contain ONLY PHP
In fact even the Zend Framework code standard strongly discourages using ?> in files containing only PHP because:
For files that contain only PHP code, the closing tag ("?>") is never
permitted. It is not required by PHP, and omitting it´ prevents the
accidental injection of trailing white space into the response.
Is the injection of trailing white space really that bad? And it is really a hideous crime to not omit the ?> from files containing only PHP? It simply seems unnatural for me to do so.
If you inject white space in an include and then try to use header() (or something else that depends on running before content is output) then you'll be entering debug hell. This is a quick and easy technique for avoiding that.
If accidental injection happens you cannot send header or start session but errors will show you that. If that happens on every file you have then you have some problems
As far as I know there are no code police running around chopping off people's hands for doing something like this. I say write good code and leave the ?> in.

Categories