Solved

Stupid if statment

Posted on 2006-11-24
6
245 Views
Last Modified: 2006-11-26
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";
}
?>
0
Comment
Question by:tonelm54
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 28

Assisted Solution

by:gamebits
gamebits earned 100 total points
ID: 18007412
Hi tonelm54,
And what is the problem

Gamebits

Cheers!
0
 
LVL 13

Assisted Solution

by:rhickmott
rhickmott earned 200 total points
ID: 18007426
Yes those statements are correct.

0
 
LVL 48

Assisted Solution

by:hernst42
hernst42 earned 100 total points
ID: 18007457
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Accepted Solution

by:
rhickmott earned 200 total points
ID: 18007536
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
 
LVL 5

Assisted Solution

by:jalalmegadeth
jalalmegadeth earned 100 total points
ID: 18008770
<?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
 

Author Comment

by:tonelm54
ID: 18014795
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

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question