Jump to content

if help


Tandem

Recommended Posts

I'm having some trouble with something....

Here is some code i wrote just to give an example:
[code]
<html>
<head>
</head>
<body>
<?



$output = $_POST['output'];

if ($output == "nothing") {
$output = 0;
}

if ($output == "something") {
echo"<br>Hello";
}


echo <<<HERE
<form method="POST" action="forms.php" name="f">
<input type="text" name="output" value="">
<input type="submit" name="submit" value="Submit!">
</form>
HERE;
?>
</body>
</html>[/code]

In this form if you enter the word 'something' into the text input it will echo "Hello". If you enter the word 'nothing' it will set the the variable $output to 0, but then for some reason "Hello" is echoed.

Why is this, and what ways are there to stop this?
Link to comment
Share on other sites

Well, your actually setting the comparasor test to if(0 == "something") - always returning true

This works, setting to false instead

[color=blue]if ($output == "nothing") {
$output = false;
}

if ($output == "something") {
echo"< br >Hello";
}[/color]

But if your purpose with this is to check if formfields are submitted empty or not, use

[color=blue]if(!empty($field))
{
// not empty
}
else
{
// empty
}[/color]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.