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
https://forums.phpfreaks.com/topic/101069-solved-multiple-variables/
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;
}
?>

 

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";
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.