Jump to content

Archived

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

Mutley

0 acts as empty, IF statement problems

Recommended Posts

I've done a code to decide who has won/lost/drawn or not played depending on the database entries. The problem I have is if the score is "0 - 10" for example, it displays it as not played, how would I solve this problem so if no points are scored it still figures out who won/lost?

Heres my code:
(York is the team who it is for, so when it says "Won" it means York Won)
[code] if(empty($scorehome)) {
?><span class="notplayed">&nbsp;</span><?
}
else {
if($scorehome == $scoreaway) {
?><span class="draw">Draw</span><?
}
else
{

if($home == York) {
if($scorehome > $scoreaway) {
$home?> <span class="win">Won</span> <?
}
else {
$away?> <span class="loss">Loss</span> <?
}
}

else {
if($away == York) {
if($scorehome < $scoreaway) {
$home?> <span class="win">Won</span> <?
}
else {
$away?> <span class="loss">Loss</span> <?
}
}
}
}
}
[/code]

Thanks in advance. :)

Share this post


Link to post
Share on other sites
[code]
<?php
$var = 0;
if ((!$var) && ($var != "0")) echo "It is empty."; else echo "not empty";
?>
[/code]

In your code your variable it is set to 0, that means the false, the empty.

Try to deal with the "0" as a string, and not a number 0.
String will make the var full, number will set it to empty.

Share this post


Link to post
Share on other sites
[quote author=pedrobcabral link=topic=110466.msg446574#msg446574 date=1159960252]
[code]
<?php
$var = 0;
if ((!$var) && ($var != "0")) echo "It is empty."; else echo "not empty";
?>
[/code]

In your code your variable it is set to 0, that means the false, the empty.

Try to deal with the "0" as a string, and not a number 0.
String will make the var full, number will set it to empty.
[/quote]

I would use !== instead of != because in the latter, a false value would be considered as 0.

Share this post


Link to post
Share on other sites
[code]

<?php

$value = "0";
$arr = array();
$arr["no 1"] = $value;
$arr["no 2"] = "$value";
$arr["no 3"] = (float)$value;
$arr["no 4"] = (string)$value;
$arr["no 5"] = (bool)$value;
$arr["no 6"] = (object)$value;
$arr["no 7"] = (int)$value;

foreach($arr as $key=>$value)
{
  if(empty($value)) echo $key." is empty | ";
  else echo $key." is not empty | ";
}

?>

[/code]

echo -->
[code]

no 1 is empty | no 2 is empty | no 3 is empty | no 4 is empty | no 5 is empty | no 6 is not empty | no 7 is empty |

[/code]

Share this post


Link to post
Share on other sites
Maybe you can use this

[code]
if(!is_numeric($value)){
?><span class="notplayed">&nbsp;</span><?
}
else {
if($scorehome == $scoreaway) {
?><span class="draw">Draw</span><?
}
else
{
if($home == York) {
if($scorehome > $scoreaway) {
$home?> <span class="win">Won</span> <?
}
else {
$away?> <span class="loss">Loss</span> <?
}
}

else {
if($away == York) {
if($scorehome < $scoreaway) {
$home?> <span class="win">Won</span> <?
}
else {
$away?> <span class="loss">Loss</span> <?
}
}
}
}
}
[/code]

Share this post


Link to post
Share on other sites
[quote author=ponsho link=topic=110466.msg446705#msg446705 date=1159977415]
Maybe you can use this

[code]
if(!is_numeric($value)){
?><span class="notplayed">&nbsp;</span><?
}
else {
if($scorehome == $scoreaway) {
?><span class="draw">Draw</span><?
}
else
{
if($home == York) {
if($scorehome > $scoreaway) {
$home?> <span class="win">Won</span> <?
}
else {
$away?> <span class="loss">Loss</span> <?
}
}

else {
if($away == York) {
if($scorehome < $scoreaway) {
$home?> <span class="win">Won</span> <?
}
else {
$away?> <span class="loss">Loss</span> <?
}
}
}
}
}
[/code]
[/quote]

Didn't work.

@ Daniel0, how?

Share this post


Link to post
Share on other sites
This will detect empty, but the number 0 will pass as not empty
[code]
<?php

if(empty($scorehome) && !preg_match("/^[0]+$/", $scorehome))
{
echo "value is empty but NOT number 0";
}

?>
[/code]

Share this post


Link to post
Share on other sites
Here's another way if you don't want to deal with regular expresions:
[code]<?php if (empty($scorehome) && strlen($scorehome) == 0) echo 'value is really empty'; ?>[/code]

Ken

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.