Jump to content

Php 'or' operator...or language construct? What is it ?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
totonex

totonex

    Learning Programmer

  • Members
  • PipPipPip
  • 82 posts
Hello,

While reading and writing PHP, i've seen a (cast_required)thing within PHP 'literature', which i also use, but i don't know what it is.

Par example:
mysql_connect($dbhost,$dbuser,$dbpass) or die(mysql_error());

Ok, the basics are easy: connect to mysql, and if anything fails, just kill the execution of the script and print the mysql_error() message to the user. This is a common occurrence in PHP all around.

Buuuut:
What is the 'or' thing, apart from its obvious meaning? It's not a function, not an operator, not a bullet, neither a train, nor Superman.
How does it do that which it does? I've never seen it in any context other than mysql error handling. Is it a primitive try/catch clause? Language construct? How else can it be used?
Thanks!

#2
PsychoCoder

PsychoCoder

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
or is a PHP logical operator

#3
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
As PsychoCoder has said or is a logical operator. Logically or and || do the same thing, but or and || are different since they have a different precedence. PHP: Operator Precedence - Manual

#4
totonex

totonex

    Learning Programmer

  • Members
  • PipPipPip
  • 82 posts
So...that's it ? Just it?
Well i expected a bit more, but oh.
Thanks.

But the question remains.
What happens in php at the " do_something or die() " thing?
I mean, it's a boolean statement.
It makes no sense for me.

In C, if you would write
do_function_1() || do_function_2()
C would evaluate both functions, decide their truth state, and the ( func1 ||func 2) expression would return itself a boolean (actually 0 or 1) value, but a redundant value.
I don't get it - why does a boolean expression, which itself returns a boolean value, influence the flow of execution in PHP?

#5
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Oh! That's actually a fantastic question. It's not often people actually are interested in the theory behind the language. In PHP the OR operator, as well as a few others, are called "short-circuit operators" meaning the second operand is evaluated only if and only if the first operand evaluates to false.

#6
PsychoCoder

PsychoCoder

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
Ok what happens with this statement, that's your real question?

mysql_connect($dbhost,$dbuser,$dbpass) or die(mysql_error());

die is equivalent to PHP's exit function so the statement above is saying Connect to this MySql database or exit (terminate the current script) and display the error

#7
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts

PsychoCoder said:

Ok what happens with this statement, that's your real question?

mysql_connect($dbhost,$dbuser,$dbpass) or die(mysql_error());

die is equivalent to PHP's exit function so the statement above is saying Connect to this MySql database or exit (terminate the current script) and display the error

Right, but normal logical operators will evaluate the left most operand mysql_connect($dbhost,$dbuser,$dbpass) to a boolean value then evaluate the right most operand die(mysql_error()) to a boolean value. Then based upon the logical values of the operands, return a boolean value. The code you provided does not do that. Hence the short circuit nature of the OR operator in PHP.

#8
totonex

totonex

    Learning Programmer

  • Members
  • PipPipPip
  • 82 posts
I GOT IT !!!
It came to me last night :))
C# also has short-circuited operators, so i'm familiar with them.

So the PHP "OR DIE" thing is just bluffing - tricking PHP into evaluating only the first operand!
Thanks guys.

#9
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
Well, not just bluffing, it do have a practical meaning, as if the command fails, it really performs the second command... like throwing an error or die with a error report...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#10
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
For a better understanding, most PHP functions *especially as of 5.0, are hardcoded to return FALSE on error, so if a function returns (null, false, 0) then it will short circuit to the die() statement.
echo (2 == 1) OR die("2 != 1");

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#11
totonex

totonex

    Learning Programmer

  • Members
  • PipPipPip
  • 82 posts
Well, technically nothing is bluffing, but for me, it looks more like of a bluff ^^

#12
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
It's syntactic sugar.