lice200 Posted December 23, 2006 Share Posted December 23, 2006 what I want is two variable's in a single if statement. The only thing is I cannot make it work the way i have it. I do not want to have 7 if statements if possible. [code][code=php:0]<?php$day = Sat;if ($day = ('Sat', 'Sun')) {echo "It is the weekend!";}?>[/code][/code] Link to comment https://forums.phpfreaks.com/topic/31679-multi-variable-if-statements/ Share on other sites More sharing options...
Mutley Posted December 23, 2006 Share Posted December 23, 2006 This works, use || as OR.[code]<?php$day = Sat;if ($day = "Sat" || "Sun") {echo "It is the weekend!";}?>[/code] Link to comment https://forums.phpfreaks.com/topic/31679-multi-variable-if-statements/#findComment-146857 Share on other sites More sharing options...
Orio Posted December 23, 2006 Share Posted December 23, 2006 You can also use the [url=http://www.php.net/in-array]in_array()[/url] function.[code]<?php$week_days = array("Mon", "Tue", "Wen", "Thu", "Fri");if(in_array($day, $week_days)) echo "Regular day";?>[/code]Orio. Link to comment https://forums.phpfreaks.com/topic/31679-multi-variable-if-statements/#findComment-146860 Share on other sites More sharing options...
kenrbnsn Posted December 23, 2006 Share Posted December 23, 2006 To Mutley: Did you try your solution? It does not work, since the syntax is incorrect. The correct syntax is:[code]<?php$day = 'Sat';if ($day == 'Sat' || $dat == 'Sun') echo 'Its is the weekend';?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/31679-multi-variable-if-statements/#findComment-146872 Share on other sites More sharing options...
Mutley Posted December 23, 2006 Share Posted December 23, 2006 I tried it and it worked, unless it echoed it by default error. Link to comment https://forums.phpfreaks.com/topic/31679-multi-variable-if-statements/#findComment-146904 Share on other sites More sharing options...
Orio Posted December 23, 2006 Share Posted December 23, 2006 That's because the value "Sun" is true (always). I mean- even if you set the value of $day to "foo", the statement will be true.As said in the manual, in the section about booleans (link [url=http://www.php.net/manual/en/language.types.boolean.php]here[/url]):[quote] When converting to boolean, the following values are considered FALSE:-the boolean FALSE itself-the integer 0 (zero)-the float 0.0 (zero)-the empty string, and the string "0"-an array with zero elements-an object with zero member variables (PHP 4 only)-the special type NULL (including unset variables)-SimpleXML objects created from empty tags Every other value is considered TRUE (including any resource).[/quote]Look at the last line ;)Orio. Link to comment https://forums.phpfreaks.com/topic/31679-multi-variable-if-statements/#findComment-146913 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.