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

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.

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.

echoing HTML with echo <<< HTML [duplicate]

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).

PHP How To Know When To Put A Space Between A Word And Parentheses

I have been recommended to put a space between foreach and (, among other things. My understanding is that it helps to differentiate between functions and everything else.
I was just working with an array and noticed that you are supposed to unset($value). I looked up unset and it's a construct. foreach is also a construct. Then I looked and array is also a construct. No one seems to put a space with unset or array, though.
How do you know when to put a space in and not? I use Adobe Dreamweaver, so I just got into a habit of putting a space after every php word that was colored green (like include_once).
JJ
Edit...
It's possible that it has something to do with keywords http://php.net/manual/en/reserved.keywords.php. I noticed that foreach is a keyword, but array() is also a keyword.
JJ
There's many different coding conventions for php. The Zend standards seem to be very close to most code I see:
http://framework.zend.com/manual/en/coding-standard.html
Anyway, probably a space after control structure keywords
Do you as a more comfortable, but the only way. and yet php coding standart

The Namespace operator... what's the big deal?

Can someone explain to me what exactly is so bad about using the backslash as the namespace operator? I'v read a lot of scoffing remarks about it. One StackOverflower even said that he gave up PHP because of it.
Yes I know that backslash has special meaning as the escape character inside strings, but it's not really any worse than using ->, or the dot . like in many other languages.
It kind of reminds me of all the mocking of Nintendo when they announced the name of the Wii. Everyone makes a big fuss and then once its out and you're used to it, no one cares and they move on.
So, please enlighten me. What is so bad about it? What would have you suggested instead?
What's so bad about it: Can you spot the error in the following code?
if(class_exists("namespace1\namespace2\myClass"))
echo "This will never be true";
What would I have suggested: Unfortunately, '\' is the only single single character available. If PHP6 were mine to design, I would replace all the bitwise operators (^, &, |, ~) with keywords (seeing as they're used so little) and use '|' as the namespace separator. In fact I would suggest lots more simple syntax changes to make PHP easier to read and write, but it's easier to just use Python instead ...
The problem with it is that it's the escape character in almost every other context. This means that people will inadvertently mess it up, but it also makes it hard to read because your eyes are tuned to read a backslash as a meta-character, rather than just another symbol.
I would have preferred three colons, which was actually suggested at some point.
Moreover, if I wanted a language that reinvented syntax for no particularly good reason, I would use Ruby.
Well, there are other problem when using "\" as namespace.
It's the escape character. If you
have to use \ in "string" and \ in
'string'. I feel someone will mess
with it at somepoint. Soon or later.
the escap char will catch you.
the '\' is not very well located on every keyboard. On my keyboard I have to use a combination of key that aren't really close to each others and sometimes it's just a pain. While it's not as bad as '^'. In other words, it will not be possible to write fluently code that needs to access namespace on certain keymap.
I remember when they voted it and how absurd the results are. They chose it because it was simpler than the other char and needed less typing. To be honest, it all depends on your keyboard layout.
That's all the reason I can find why it might be a bad thing to use the escape character.
To be honest, I'm still waiting for the language that will create his own unicode symboles. So it would give much more flexibility on which operator you can override. Let say in c++ you could write something like.
bool operator ≤ (Dog dog);
//and then do this
if(myDog ≤ thisDog){
}
//Seems useful?
bool operator ≅ thisDog){
// this wouldn't check for equality
// but for something close to it
}
Being able to use our own arbitrary operator make much more sense than using + to group things..."∪" would make much more sense...And if you want to get intersection just you "∩" and then people might say.."what if we don't have those char in our font?" I can only answer with: "Find a font that has them!!!!"
The official RFC and backing documents can be found at
https://wiki.php.net/rfc/namespaceseparator and
https://wiki.php.net/rfc/backslashnamespaces
They include an IRC log about the decision process.
Quoting Section "Problems"
\ looks a lot like / and is easy to accidentally flip, especially for unix users
\ is used for escaping
inside a string a namespace name becomes \\like\\this or we can get weird characters. This could be confusing to users at first.
all existing namespaced code must be nearly rewritten, a simple search/replace of :: would not be possible.
the patch touches a lot of the engine, and will need rigorous battle-testing.
to many, \this\way will look weird at first.
Any of the scoff remarks you mentioned are likely due to that above or personal opinion for "reasons". I, for instance, find them quite ugly to read and cumbersome to write, especially in strings where I have to use double backslashes. But then again, I get used to it the more often I use them.
let's assume the following namespace: jp\nintendo\rvl\testing
do you notice the errors?
the actual (internal) namespace is most likely something like this:
jp
intendo
vl esting
The solution to this is to always use two backslashes as a namespace seperator similar to how we're using this in windows filenames.
using two backslashes is completely harmles, as it is a escape sequence itself, which expands into 1 single literal backslash, which is the actual namespace seperator.
now, if we use jp\\nintendo\\rvl\\testing as a namespace (using 2 backslashes as the seperator) the actual (internal) namespace becomes: jp\nintendo\rvl\testing
The real question is, why didn't they just put it as / ? I personally hate \ because it's an escape character... that screws everything up for me!

Categories