Jump to content

Archived

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

Tandem

if help

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?

Share this post


Link to post
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]

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.