Jump to content

Archived

This topic is now archived and is closed to further replies.

Leao

Multiple if statements?

Recommended Posts

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

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

else
{echo "ERROR!";}
?>[/code]

Share this post


Link to post
Share on other sites
[code=php:0]
<?php
if (isset($_POST['apples']) && isset($_POST['pears']) && isset($_POST['bananas'])) {
  echo "All the elements are present.";
} else {
  echo "ERROR!";
}
?>
[/code]

Share this post


Link to post
Share on other sites
[code]<?php
if (isset($_POST['apples'],$_POST['pears'],$_POST['bananas'])) {
echo "All the elements are present.";
} else {
  echo "ERROR!";
}
[/code]

Share this post


Link to post
Share on other sites
Damn... I always foget isset can except multiple args.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

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.