Jump to content


Photo

Multiple if statements?


  • Please log in to reply
4 replies to this topic

#1 Leao

Leao
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 19 October 2006 - 10:15 PM

Hi,

How do I do multiple if statements? I'm sending some variables from an HTML form to the PHP script below. The script is supposed to check if the 3 variables 'apples', 'pears' and 'bananas' have been received by the form and echo "All the elements are present." If all the elements aren't present the script should echo "ERROR!"

Cheers,

Leao

<?php
if (isset($_POST['apples']))
if (isset($_POST['pears']))
if (isset($_POST['bananas']))
{echo "All the elements are present.";}

else
{echo "ERROR!";}
?>


#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 19 October 2006 - 10:18 PM

<?php
if (isset($_POST['apples']) && isset($_POST['pears']) && isset($_POST['bananas'])) {
  echo "All the elements are present.";
} else {
  echo "ERROR!";
}
?>


#3 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 19 October 2006 - 10:19 PM

<?php
if (isset($_POST['apples'],$_POST['pears'],$_POST['bananas'])) {
 echo "All the elements are present.";
} else {
  echo "ERROR!";
}

follow me on twitter @PHPsycho

#4 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 19 October 2006 - 10:23 PM

Damn... I always foget isset can except multiple args.

#5 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 19 October 2006 - 10:27 PM

LOL.

I always try it empty... then trash myself to within an inch of my life for not remembering for the millionth time that you can't!!!!!

One think to note though is that even if a variable is NULL (or empty) isset will return true (except checkboxes and radio buttons) - so if you are looking to make sure that all these variables actually have values use thorpes firts structure replacing isset with !empty
follow me on twitter @PHPsycho




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users