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
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?

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.