Jump to content

Making "0" a value?


Perad

Recommended Posts

I have this code to check to see if information is entered into the form...

[code]
if (empty($_POST['oppscore1'])) {
$os1 = FALSE;
$message .= '<p>You forgot to enter the Opponents Score ht!</p>';
} else {
$os1 = $_POST['oppscore1'];
}[/code]

Unfortunately when i enter "0" into the form field for Score, it gets rejected because it thinks 0(Zero) isn't a number/value.

Could someone help me correct this please.

Perad
Link to comment
https://forums.phpfreaks.com/topic/24094-making-0-a-value/
Share on other sites

[quote author=Perad link=topic=111650.msg452590#msg452590 date=1161005954]

[code]
if (empty($_POST['oppscore1'])) {
$os1 = FALSE;
$message .= '<p>You forgot to enter the Opponents Score ht!</p>';
} else {
$os1 = $_POST['oppscore1'];
}[/code]

[/quote]

Much better to use:

[code]
<?php

if (isset($_POST['oppscore1'])) {
$os1 = FALSE;
$message .= '<p>You forgot to enter the Opponents Score ht!</p>';
} else {
$os1 = $_POST['oppscore1'];
}

?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/24094-making-0-a-value/#findComment-109524
Share on other sites

is_numeric() will work, also this is an approach to detect "real" empty

[code]

<?php

if(empty($_POST['oppscore1']) && !preg_match("/^[0]+$/", $_POST['oppscore1']))
{
  echo "is genuine empty and a 0 is not here";
}
else
{
  echo "is not empty and counting 0 as a value";
}

?>

[/code]
Link to comment
https://forums.phpfreaks.com/topic/24094-making-0-a-value/#findComment-109641
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.