Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Mar 2005
    Posts
    59
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question How To Use IF OR -Is it possible?

    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:
    PHP Code:
    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.

  • #2
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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'.

  • #3
    Senior Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    1,963
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
     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:

    PHP Code:
    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;


    I take no responsibility for the above nonsense.


    Left Justified

  • #4
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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).

  • #5
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    != - NOT equal. Same as <>
    !== - NOT equal value and type.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #6
    New Coder
    Join Date
    Mar 2005
    Posts
    59
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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:
    PHP 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


  • #7
    Senior Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    1,963
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    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;


    I take no responsibility for the above nonsense.


    Left Justified

  • #8
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I second mindlesslemming's switch code. A switch will nearly always be more efficient than stacking separate if's like that.

  • #9
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    $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'

    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •