...

View Full Version : How To Use IF OR -Is it possible?



ideffect
06-17-2005, 04:54 AM
Hello,

I'm working on a form validation script and one of the fields would have one of three possible codes that need to be validated. I thought I could use a IF statment with OR to seprate the possibilites but I have not yet got it to work. Here's my last attempt at it:


if (($code != $book_code_1) or ($code != $book_code_2) or ($code != $book_code_3))
{
echo "<font color ='red'><b>The code entered is not vaild. Please check it.</font></b><br>";
$modify =yes;
}


What would be the correct way of doing this statment?

Thanks for the help.

Kid Charming
06-17-2005, 05:10 AM
In PHP, when stacking conditionals with or operators, the statement is not completely evaluated if one of the conditions evaluates as true. So in your code, if $code != $book_code_1, book codes 2 and 3 are not even checked. If you want to make sure $code is none of the 3 book codes, use 'and' instead of 'or'.

mindlessLemming
06-17-2005, 05:33 AM
if ($code !== $book_code_1 || $code !== $book_code_2 || $code !== $book_code_3 )
{
echo "<strong style='color:red'>The code entered is not vaild. Please check it.</strong><br>";
$modify =yes;
}


!= means 'cannot be assigned to'.. in other words, even if the rest of your syntax was correct, the statement would always equate to true as "if ($code != $book_code_1)" means "if the value of $book_code_1 cannot be set to the value of $code".

You want to compare the values of the two, which is !==

A more extendable (future options) way of writing your code is this:



switch ($code) {
case($book_code_1) :
//book code 1 stuff;
break;
case($book_code_2) :
//book code 2 stuff;
break;
case($book_code_3) :
//book code 3 stuff;
break;
default :
echo "<strong style='color:red'>The code entered is not vaild. Please check it.</strong><br>";
break;
}

Kid Charming
06-17-2005, 06:06 AM
Using '!=' is fine, as long as the values you're comparing are of the same type (or you can trust type juggling to cast them correctly).

marek_mar
06-17-2005, 08:17 AM
!= - NOT equal. Same as <>
!== - NOT equal value and type.

ideffect
06-17-2005, 08:42 AM
Thanks for all the suggestions! I see I still have a lot to learn about php. I ended up using a little bit diffrent code. I tried to use the others but it was still not working. I'll take another look at it again tomorrow since it's getting late. Here's my new code:


if ($code == $book_code_1 || $code == $book_code_2 || $code == $book_code_3 )
{
if ($code == '$book_code_1')
{
$book_downloaded = "Book 1";
}
if ($code == '$book_code_2')
{
$book_downloaded = "Book 2";
}
if ($code == '$book_code_3')
{
$book_downloaded = "Book 3";
}
}
else
{
echo "<strong style='color:red'>The code entered is not vaild. Please check it.</strong><br>";
$modify =yes;
}

mindlessLemming
06-17-2005, 10:19 AM
switch ($code) {
case($book_code_1) :
$book_downloaded = "Book 1";
break;
case($book_code_2) :
$book_downloaded = "Book 2";
break;
case($book_code_3) :
$book_downloaded = "Book 3";
break;
default :
echo "<strong style='color:red'>The code entered is not vaild. Please check it.</strong><br>";
break;
}

Kid Charming
06-17-2005, 04:58 PM
I second mindlesslemming's switch code. A switch will nearly always be more efficient than stacking separate if's like that.

raf
06-17-2005, 09:02 PM
$arr_allowed=array('1', '2', '3');
$in_choice = substr($code, -1);
if (in_array($in_choice, $arr_allowed)){
$book_downloaded = 'Book ' . $in_choice;
}else{
echo 'Invalid choice';
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum