Stupid if statment

Ok this is a really stupid question, but I cant remember it

I need to know the syntax for IF OR statments

I thought it was:-
<?php
if ((1 == 1) || (2 ==2))
{
echo "1 = 1 or 2 = 2";
}
?>

I also thought that the and would be:-
<?php
if ((1 == 1) && (2 ==2))
{
echo "1 = 1 and 2 = 2";
}
?>
tonelm54Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Robin HickmottConnect With a Mentor Software DeveloperCommented:
Traditionally in programming the idea in the Double Operator is to tell the interpreter or compiler to use Lazy Evaluation.

If you have for example

==============================

function mock () {
     for ($i=1 ; $i < 80000000; $i++ ) {
           // Do Nothing
     }
     return true;
}

if ((1=1) || mock()) {
     echo("True");
}

===============================

Because this is an OR statement if the first is true then we don't need to evaluate the second so the pointless function mock() is never run. If however you had used

if ((1=1) | mock()) {
     echo("True");
}

It will evaluate BOTH conditions even though it doesent need to useful if mock () contained statements you wanted to run anyway even if it was false.

For this reason It is normally faster to use || and NEVER slower.

Ditto with && if the first is false then the second is pointless and not run.

While its true you can use OR and AND directly but you would have to test it to find out which system it used and for this reason most people tend to stick with && and ||

0
 
gamebitsConnect With a Mentor Commented:
Hi tonelm54,
And what is the problem

Gamebits

Cheers!
0
 
Robin HickmottConnect With a Mentor Software DeveloperCommented:
Yes those statements are correct.

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
hernst42Connect With a Mentor Commented:
instead of || you can write "or" or for && you can also use "and" If you use the parantheis, but the if statements are akso correct
0
 
jalalmegadethConnect With a Mentor Commented:
<?php
if ((1 == 1) && (2 ==2))
{
echo "1 = 1 and 2 = 2";
}
?>

this one is correct and it's for AND



<?php
if ((1 == 1) || (2 ==2))
{
echo "1 = 1 or 2 = 2";
}
?>
this one is for OR


so both of them are correct
0
 
tonelm54Author Commented:
Sorry, the problems were due to PHP not been installed correctly, and me trying to figure if statments out that I havnt done for ages.

Sorry, again, but thanks, I think its fair to distrubute the points equally between everyone who contributed.

Thanks again, to everyone
0
All Courses

From novice to tech pro — start learning today.