TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-22-2008, 01:17 PM   #1 (permalink)
The Contributor
 
Join Date: May 2008
Location: Iran
Posts: 53
Thanks: 33
Yoosha is on a distinguished road
Smile 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.
Send a message via Yahoo to Yoosha
Yoosha is offline  
Reply With Quote
Old 06-22-2008, 02:44 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
Yoosha (06-23-2008)
Old 06-22-2008, 04:46 PM   #3 (permalink)
The Wanderer
 
Join Date: May 2008
Posts: 17
Thanks: 2
Alex is on a distinguished road
Default

Quote:
Originally Posted by Yoosha View Post
1) OR V.S. ||
Personal preference, they are identical.
Quote:
Originally Posted by Yoosha View Post
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 View Post
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 View Post
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 View Post
5) if($X == $Y) V.S. if($X === $Y)
Read Wildhoney's post
Quote:
Originally Posted by Yoosha View Post
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 View Post
7) die(''); V.S. exit();
I'm pretty sure they are identical.
Alex is offline  
Reply With Quote
The Following User Says Thank You to Alex For This Useful Post:
Yoosha (06-23-2008)
Old 06-22-2008, 06:10 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-23-2008, 06:53 AM   #5 (permalink)
The Contributor
 
Join Date: May 2008
Location: Iran
Posts: 53
Thanks: 33
Yoosha is on a distinguished road
Default

Thanks both ;). Thats resolved!
Send a message via Yahoo to Yoosha
Yoosha is offline  
Reply With Quote
Old 06-23-2008, 10:16 AM   #6 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)

Last edited by sketchMedia : 06-23-2008 at 01:50 PM.
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
Alex (06-23-2008)
Old 06-23-2008, 02:51 PM   #7 (permalink)
The Wanderer
 
Join Date: May 2008
Posts: 17
Thanks: 2
Alex is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
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.
Alex is offline  
Reply With Quote
Old 06-23-2008, 02:52 PM   #8 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:44 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design