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
https://forums.phpfreaks.com/topic/22110-if-help/
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
https://forums.phpfreaks.com/topic/22110-if-help/#findComment-99003
Share on other sites

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.