aplacito Posted November 3, 2007 Share Posted November 3, 2007 Perhaps I'm just crazy this morning, but it seems that one cannot use three tests joined with && in a php conditional. For example, the following code works: $errors = 0; if ($submitted) { if(($_POST['un'] != "") && ($_POST['pw'] != "")) { $errors = 2; } } //when $submitted is true, $errors is 2, //when $submitted is false, $errors is 0. but the following doesn't work properly: $errors = 0; if ($submitted && ($_POST['un'] != "") && ($_POST['pw'] != "")) { $errors = 2; } //when $submitted is true, $errors is 2, //when $submitted is false, $errors is 2. am I missing something completely? can anyone confirm or refute this oddity? Quote Link to comment https://forums.phpfreaks.com/topic/75911-solved-three-conjoined-tests-in-php-conditional-fails/ Share on other sites More sharing options...
Psycho Posted November 3, 2007 Share Posted November 3, 2007 It should work, but you can simplify the code by losing the extra parens and using empty(): if ( $submitted && !empty($_POST['un']) && !empty($_POST['pw']) ) { Quote Link to comment https://forums.phpfreaks.com/topic/75911-solved-three-conjoined-tests-in-php-conditional-fails/#findComment-384184 Share on other sites More sharing options...
aplacito Posted November 3, 2007 Author Share Posted November 3, 2007 thanks for the quick response, mjdamato. i'm still seeing if there might be other errors in the code, but i'm still coming up dumbfounded. thanks for the advise on !empty() though, i always enjoy cleaning up my code Quote Link to comment https://forums.phpfreaks.com/topic/75911-solved-three-conjoined-tests-in-php-conditional-fails/#findComment-384192 Share on other sites More sharing options...
LemonInflux Posted November 3, 2007 Share Posted November 3, 2007 <?php $errors = 0; if(isset($submitted)) && isset($_POST['un']) && isset($_POST['pw'])) { $errors = 2; } ?> Try that. Quote Link to comment https://forums.phpfreaks.com/topic/75911-solved-three-conjoined-tests-in-php-conditional-fails/#findComment-384194 Share on other sites More sharing options...
Psycho Posted November 3, 2007 Share Posted November 3, 2007 I assume that the OP is checking the posted fields to see if the user entered data. If that is the case, using isset() for a posted value is not enough. If a field is empty when POSTED then it is set as an empty string. Quote Link to comment https://forums.phpfreaks.com/topic/75911-solved-three-conjoined-tests-in-php-conditional-fails/#findComment-384200 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.