Reputation: 59
I am in the process of learning PHP development as a beginner i would like to ask question regarding the use of if with isset. Here the two code:
Is there any difference between these two pieces of code:
if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) )
And:
if (isset($_GET['s']) && is_numeric($_GET['s']))
In the first part the isset()
and is_numeric()
are inside an additional set of parenthesis.
But in the second they reside inside the if()
's parenthesis.
Is there any difference between the first and second snippet?
Thanks in advance for any explanation and clarification.
Upvotes: 2
Views: 53
Reputation: 24699
No, there is no difference whatsoever.
The purpose of additional parenthesis ((...)
) is to specify a particular order of operations, like in math. As a general rule, nothing bad will happen if you use too many parenthesis, but it can make your code a bit less clear. The two code snippets you wrote do exactly the same thing. I think the second example is cleaner.
Here's where you need them:
if ((conditionA || conditionB) && (conditionC || condition D)) {
...
}
They can also add clarity around mathematical expressions:
$x = $y * 7 + 3 / 2;
$x = ($y * 7) + (3 / 2);
Both of these expressions do the exact same thing, but the parenthesis make it easier to see what's going on, without remembering the order of operations.
Sometimes, you want to use a different order of operations, and you need them:
$x = $y * (7 + 3) / 2;
Upvotes: 5