Mutley Posted October 4, 2006 Share Posted October 4, 2006 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"> </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. :) Quote Link to comment https://forums.phpfreaks.com/topic/22965-0-acts-as-empty-if-statement-problems/ Share on other sites More sharing options...
pedrobcabral Posted October 4, 2006 Share Posted October 4, 2006 [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 Link to comment https://forums.phpfreaks.com/topic/22965-0-acts-as-empty-if-statement-problems/#findComment-103634 Share on other sites More sharing options...
Mutley Posted October 4, 2006 Author Share Posted October 4, 2006 I see, but how would I implement that into my code? Quote Link to comment https://forums.phpfreaks.com/topic/22965-0-acts-as-empty-if-statement-problems/#findComment-103635 Share on other sites More sharing options...
pedrobcabral Posted October 4, 2006 Share Posted October 4, 2006 Try to put this at the begining if ((!$scorehome) && ($scorehome != "0")) instead of your "empty($score) clause. Quote Link to comment https://forums.phpfreaks.com/topic/22965-0-acts-as-empty-if-statement-problems/#findComment-103640 Share on other sites More sharing options...
Mutley Posted October 4, 2006 Author Share Posted October 4, 2006 Didn't work. :( Quote Link to comment https://forums.phpfreaks.com/topic/22965-0-acts-as-empty-if-statement-problems/#findComment-103650 Share on other sites More sharing options...
eric1235711 Posted October 4, 2006 Share Posted October 4, 2006 what about using is_null instead of empty ? Quote Link to comment https://forums.phpfreaks.com/topic/22965-0-acts-as-empty-if-statement-problems/#findComment-103736 Share on other sites More sharing options...
Hi I Am Timbo Posted October 4, 2006 Share Posted October 4, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/22965-0-acts-as-empty-if-statement-problems/#findComment-103746 Share on other sites More sharing options...
alpine Posted October 4, 2006 Share Posted October 4, 2006 [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] Quote Link to comment https://forums.phpfreaks.com/topic/22965-0-acts-as-empty-if-statement-problems/#findComment-103752 Share on other sites More sharing options...
ponsho Posted October 4, 2006 Share Posted October 4, 2006 Maybe you can use this [code]if(!is_numeric($value)){?><span class="notplayed"> </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 Link to comment https://forums.phpfreaks.com/topic/22965-0-acts-as-empty-if-statement-problems/#findComment-103760 Share on other sites More sharing options...
Daniel0 Posted October 4, 2006 Share Posted October 4, 2006 Use intval Quote Link to comment https://forums.phpfreaks.com/topic/22965-0-acts-as-empty-if-statement-problems/#findComment-103847 Share on other sites More sharing options...
Mutley Posted October 6, 2006 Author Share Posted October 6, 2006 [quote author=ponsho link=topic=110466.msg446705#msg446705 date=1159977415]Maybe you can use this [code]if(!is_numeric($value)){?><span class="notplayed"> </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? Quote Link to comment https://forums.phpfreaks.com/topic/22965-0-acts-as-empty-if-statement-problems/#findComment-105140 Share on other sites More sharing options...
alpine Posted October 6, 2006 Share Posted October 6, 2006 This will detect empty, but the number 0 will pass as not empty[code]<?phpif(empty($scorehome) && !preg_match("/^[0]+$/", $scorehome)){echo "value is empty but NOT number 0";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22965-0-acts-as-empty-if-statement-problems/#findComment-105234 Share on other sites More sharing options...
kenrbnsn Posted October 6, 2006 Share Posted October 6, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/22965-0-acts-as-empty-if-statement-problems/#findComment-105309 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.