Jump to content

if statement should definately work, but doesn't!


deed02392

Recommended Posts

<?php
   if (($_GET['ID']) == '18' || '9' || '13')
   
   echo ("<span class=buy>Sorry, this picture cannot be ordered currently.</span>");
      
   else 
   echo "<input type=image src=image/misc/btn_buynow_SM.gif border=0 name=submit alt=Click here to pay now!>";
    
   ?>

 

This code is meant to say, if the picture is either number 18, 9, or 13, then show they can't buy the picture, rather than the submit button if it equals anything else. however, no matter what, it ALWAYS shows that they can't buy the picture, even if ID=1 for example! I've looked at so many help website and they all show this format for else if statements. It just doesn't make sense, because, in theory, this is correct, right?

I think you want:

<?php
   if ($_GET['ID'] == '18' || $_GET['ID'] == '9' || $_GET['ID'] == '13')
   
   echo ("<span class=buy>Sorry, this picture cannot be ordered currently.</span>");
      
   else 
   echo "<input type=image src=image/misc/btn_buynow_SM.gif border=0 name=submit alt=Click here to pay now!>";
    
   ?>

 

I'm pretty sure it was reading if (id = 18) or 9 or 13. 9 is 9 and 13 is 13 so it always returns true.

you could also try this if you don't like all those or case scenarios:

<?php
if(in_array($_GET['ID'], array(18, 9, 13)) { ... }
?>

I am pretty sure the numbers don't need quote since it is in_array should be using == and not === to compare the values but if I am wrong, just check it to the string format.

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.