TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Differents... (http://www.talkphp.com/absolute-beginners/2989-differents.html)

Yoosha 06-22-2008 01:17 PM

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.

Wildhoney 06-22-2008 02:44 PM

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:
$bValue = 'TalkPHP.com';

if (!$bValue)
{
    die('Variable was inverted to true');
}

die('Variable was inverted to false');

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:
$bValue = false;
var_dump(!!!$bValue);

Inverted the variable 3 times, from its initial state of false:
  • True
  • False
  • True (Final state)

I hope this answers your question thoroughly!

Alex 06-22-2008 04:46 PM

Quote:

Originally Posted by Yoosha (Post 15874)
1) OR V.S. ||

Personal preference, they are identical.
Quote:

Originally Posted by Yoosha (Post 15874)
2) $_X V.S. $X

The _ just changes the variables name, there's nothing special about it.
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:

Originally Posted by Yoosha (Post 15874)
3) @session_start(); V.S. session_start();

The @ suppresses any errors that the session_start() function might cause. Take a look at PHP: Error Control Operators - Manual for more info.
Quote:

Originally Posted by Yoosha (Post 15874)
4) if(!$X == Y) V.S. if($X != $Y)

I think Wildhoney's covered this pretty well, so I won't say too much.
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:

Originally Posted by Yoosha (Post 15874)
5) if($X == $Y) V.S. if($X === $Y)

Read Wildhoney's post
Quote:

Originally Posted by Yoosha (Post 15874)
6) if($X == '') V.S. if($X == null)

First checks if $X evaluates to an empty string. The second checks to see if $X evaluates to Null. Also read wildhoney's post
Quote:

Originally Posted by Yoosha (Post 15874)
7) die(''); V.S. exit();

I'm pretty sure they are identical.

Wildhoney 06-22-2008 06:10 PM

I know of at least one instance where the #6 differs. Take the following as an example:

php Code:
$aValue = array();

if ($aValue == null)
{
    die('Ended within here');
}

die('Ended over here');

Juxtaposed with:

php Code:
$aValue = array();

if ($aValue == '')
{
    die('Ended within here');
}

die('Ended over here');

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.

Yoosha 06-23-2008 06:53 AM

Thanks both ;). Thats resolved!

sketchMedia 06-23-2008 10:16 AM

Quote:

Quote:
Originally Posted by Yoosha
1) OR V.S. ||

Personal preference, they are identical.
Wrong, they operate under different precedence rules.

PHP: Logical Operators - Manual

PHP logical operator precedence:
PHP Code:

$e false || true;
$f false or true

On the face of it, it seems like both operate the same thus both evaluate to 'true' ('or' and '||' gives us 'true' if either the expression result to the left or right is true) however
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:

$f false or true;
//php interprets as
($f false) or true

So there you can see, PHP creates $f and assigns it a boolean 'false' before the 'or' statement is reached, and because assigning 'false' to $f didn't produce 'false' i.e. it didn't fail
it exits the expression there, leaving $f equal to 'false'.

Looking at the other example:
PHP Code:

$e false || true

'||' is higher than 'or' and more importantly '=', therefore the expression functions like expected:
PHP Code:

$e = (false || true); 

Which gives us 'true' because the '||' expression is evaluated before '=' assigns which gives us 'true' as expected.

Hopefully that explains it correctly.

Alex 06-23-2008 02:51 PM

Quote:

Originally Posted by sketchMedia (Post 15905)
Wrong, they operate under different precedence rules.

PHP: Logical Operators - Manual

PHP logical operator precedence:
PHP Code:

$e false || true;
$f false or true

On the face of it, it seems like both operate the same thus both evaluate to 'true' ('or' and '||' gives us 'true' if either the expression result to the left or right is true) however
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:

$f false or true;
//php interprets as
($f false) or true

So there you can see, PHP creates $f and assigns it a boolean 'false' before the 'or' statement is reached, and because assigning 'false' to $f didn't produce 'false' i.e. it didn't fail
it exits the expression there, leaving $f equal to 'false'.

Looking at the other example:
PHP Code:

$e false || true

'||' is higher than 'or' and more importantly '=', therefore the expression functions like expected:
PHP Code:

$e = (false || true); 

Which gives us 'true' because the '||' expression is evaluated before '=' assigns which gives us 'true' as expected.

Hopefully that explains it correctly.

Thanks for explaining that, I didn't realise that realise that operators other than the numerical ones had an order of precedence.

sketchMedia 06-23-2008 02:52 PM

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