Jump to content

[SOLVED] Multiple Variables?


Leadwing

Recommended Posts

Hi there,

Im fairly new to PHP...

Is it possible to have something like:

 

 

if variable1=true AND variable2=true

echo "result1"

 

elseif variable1=true AND variable2=false

echo "result2"

 

elseif variable1=false AND variable2=true

echo "result3"

 

elseif variable1=false AND variable2=false

echo "result4"

 

 

In reality there are more variables, but just so you understand.

I dont know how to have multiple variables in a if statement... the "AND"

Can i just use ";" or something?

Thanks

Link to comment
Share on other sites

You can use AND or you can use &&. Most people would use &&:

 

<?php
$foo = 1;
$bar = 10;

if($foo==1 && $bar < 5){
echo 'result1';
}elseif($foo==1 && $bar > 10){
echo 'result2';
}else{
echo 'result3';
}
?>

 

If you want an if statement to evaluate to true when one of two things are true, use OR (||):

 

<?php
$foo = 1;
$bar = 2;
if($foo==1 || $bar ==1){//note, this will be true when one or both of these statements are true.
echo $result1;
}
?>

 

Link to comment
Share on other sites

Yes, and its just as youve written it.

 

<?php

if ($variable1 AND $variable2) {
 echo "result1";
} elseif ($variable1 AND !$variable2) {
 echo "result2";
} elseif (!$variable1 AND $variable2) {
 echo "result3";
} elseif (!$variable1 AND !$variable2) {
 echo "result4";
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.