Jump to content

0 acts as empty, IF statement problems


Mutley

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. :)
Link to comment
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.
Link to comment
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.
Link to comment
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]
Link to comment
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]
Link to comment
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?
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.