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.
Related
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.
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).
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.
This question already has answers here:
PHP String Length Without strlen()
(3 answers)
Closed 3 years ago.
PHP coding standards say:
... PHP holds the length property of each string, and that it
shouldn't be calculated with strlen(). Write your functions in a such
a way so that they'll take advantage of the length property, both
for efficiency and in order for them to be binary-safe. ...
How can I access this length property? Or do I misunderstand it?
As Ignacio mentioned in another post:
They're talking about the C function, not the PHP function. The C
function will stop counting after the first \0, but PHP strings can
contain \0 elsewhere other than the end.
That document is most likely about how to extend PHP's engine, rather than programming in PHP itself.
These coding standards are for not intended for web sites developpers using PHP, but for the developpers of the PHP engine itself.
PHP is developped using the C language, and the strlen function to avoid is the C one, not the PHP one.
It's not a "property" in the sense that it would be in other languages (like C#).
You cannot do:
myString.Length;
Instead you would need to do:
strlen(myString);
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Why PHP variables start with a $ sign symbol?
I have looked at other programming languages and it seems that most of them do not have any symbol to show that something is a variable. Is there some reason why a PHP interpreter needs such a sign, when interpreters/compilers for other languages are capable of figuring out what is a variable without such a symbol?
Does it make it faster for the interpreter? Does it make it easier for engineers to create an interpreter? Is it to make the code easier to read? Or some other reason?
Bonus question: And if there is a good reason to have a symbol connoting a variable, why don't all programming languages have it?
This is the closest question I could find, although the question seems unclear and the answers range from "just because" to "here's why it's a $ and not some other symbol." That thread did not seem to address the actual purpose of the dollar sign.
EDIT: My question must have been horribly articulated, judging from the confusion in the comments. To clarify, my question is not "Why is the symbol in front of a variable a $ as opposed to some other symbol?", a question that was asked and got four good answers in the page I linked to. My question is "Why is there any symbol at all in front of a variable in PHP? What purpose does it serve to have a symbol in front of a variable?"
Having a symbol to denote variables makes string interpolation simple and clear. Shell, Perl and PHP grew out of the need for quick and easy string manipulation, including interpolation, so I imagine using a variable prefix seemed like a good idea.
I.e. in PHP:
$var = 'val';
$strVar = "The var is $var";
Compare to typical string formatting:
var = 'val'
strVal = 'The var is %s' %(var)
I think it's just from it's origins.
unix shell and Perl were examples.
if you watch PHP closer you will see very much in common with shell.
Thus, you'd better address your question there :)
In php you don't have to set the variables prior to using it, in other languages you have to declare variables before using it like var MyVar = 'my value' or defining what kind of content is going to hold.
I'm not sure but I think the purpose of adding a symbol was to not have to declare variables and let apache know that this is a variable.