![]() |
Differents...
Hi,
What is different between: 1) OR V.S. || 2) $_X V.S. $X 3) @session_start(); V.S. session_start(); 4) if(!$X == Y) V.S. if($X != $Y) 5) if($X == $Y) V.S. if($X === $Y) 6) if($X == '') V.S. if($X == null) 7) die(''); V.S. exit(); ? Regards. |
Each of these require a little more than one line to adequately explain, so I will cherry pick #4.
As you know, a conditional statement, such as the if block, will only be entered if the value is true. So by prepending an exclamation mark to a variable, you are inverting its value. However, many values cannot be successfully inverted. A string, for instance, cannot be inverted -- only reversed. To an invert a word, I would assume, you would have to find its antonym. In computer languages, such as PHP, this does not apply, as everything is reduced to either true or false, 1 or 0, yes or no.Thus, if I inverted a string it would naturally be inverted to false. Conversely, if I were to invert a false value, it would become true. Therefore once you place that in a conditional if statement, any variable that is true, will become false, and thus not enter that if statement.For instance: php Code:
To enter the if statement block, the initial value would have had to have been false to begin with, or any value which denotes a false value -- such as null.Why did I write all that? Well, I personally think that's the best way to understand your #4 question. Both are somewhat similar, but in the first example you're inverting the value, and in the second you're merely looking for a non-matching value. Both, insofar as I can see, will produce the same results, but the latter should be used, at least in my opinion, in such an instance. I reserve the inverting of values for if statements, as seen above.In addendum, you can invert values as much as you like. Consider the following: php Code:
Inverted the variable 3 times, from its initial state of false:
I hope this answers your question thoroughly! |
Quote:
Quote:
It's like comparing $a to $b . However, some superglobal variables (i.e. $_SESSION, $_GET, $_POST) have an underscore in them and I presume it is a safegaurd so that users don't accidentally overwrite / change variables. Quote:
Quote:
I'm pretty sure that the first statement means - If the inverse of $X is equal to $Y , and the second one means if $X is not equal to $Y Quote:
Quote:
Quote:
|
I know of at least one instance where the #6 differs. Take the following as an example:
php Code:
Juxtaposed with: php Code:
I surmise that's something to do with the fact that the array is null. It is not null to the variable because it holds a skeleton of an array, but the array, per se, is null.When you attempt to validate it by specifying a string value, I.E: '', then you are attempting to classify it as a string, and as aforementioned, the variable is not null in that sense because it holds an array -- an empty, or null, array.Am I right, or am I way off? Somebody please help. |
Thanks both ;). Thats resolved!
|
Wrong, they operate under different precedence rules.
PHP: Logical Operators - Manual PHP logical operator precedence: PHP Code:
this wont behave as expected, and $f will be assigned 'false', why? Well if you look at the precedence chart on php.net, you see that 'or' is lower down the list than '||' this means that it has lower precedence, this isn't the issue however. The issue arises when you use it in conjunction with another operator, for example '=' this is higher in the list than 'or', so therefore any expression with '=' in it must be evaluated first, thus:( i will use brackets to show what PHP does first) PHP Code:
it exits the expression there, leaving $f equal to 'false'. Looking at the other example: PHP Code:
PHP Code:
Hopefully that explains it correctly. |
Quote:
|
np, it is an easy one to miss as on the surface they seem to operate identically and for the most part do so, plus the php documentation really should be clearer.
|
| All times are GMT. The time now is 12:53 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0