Jump to content

[SOLVED] Fairly simple syntax question


peps666

Recommended Posts

First post on this forum!

 

I'm currently developping a web program at my job. In some scripts, i have fairly long list of conditions before showing something. Here's a short example:

 

if(($a==2 OR $a==4 OR $a==31))

{

echo "this";

}

 

I'm trying to save some space in the code to make it more readable. I tried to write it like this but it's not working...

 

if(($a==(2 OR 4 OR 31))

{

echo "$a";

}

 

I tried this also...

 

if(($a==(2 || 4 || 31))

{

echo "$a";

}

 

Is there a way to write correctly a statement in the way it is above? Thanks!

 

Link to comment
https://forums.phpfreaks.com/topic/70846-solved-fairly-simple-syntax-question/
Share on other sites

first off The word OR is a mysql operator not php secondly you can say

if($a is this or that or that) you must say

if($s is this or $a is that or $a is that) such as

if($a = 5 || $a = 6 || $a = 8)

 

that holds true if $a is 5 6 or 8 also you can't test if 5<$a<8 you must say if($a>5 && $a <8)

 

to make stuff clearer try using a function based test, or try this

 

<?php 
$tester = array(5,6,
$a = 2;
if(in_array($a,$tester)){
//it matched
}
?>

make sense?

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.