raphael75 Posted November 10, 2021 Share Posted November 10, 2021 Here is my PHP code: <?php $a = 76; $b = true; $c = 0; $d = 500; $check = ( ($a > 0) && ($b == false) && ($c == 0) && ($d > 0) ); echo '[' . $check . ']' . "\n"; //echoes [] // ------------------------------- $a = 76; $b = false; $c = 0; $d = 500; $check = ( ($a > 0) && ($b == false) && ($c == 0) && ($d > 0) ); echo '[' . $check . ']' . "\n"; ?> This generates this error: PHP Fatal error: Uncaught Error: Call to undefined function () in /home/abc/code/php_test/test_boolean.php:26 Stack trace: #0 {main} thrown in /home/abc/code/php_test/test_boolean.php on line 26 I can't figure out why this error is occurring. Why does changing $b to false make it think it's a function call? I get this error if I run it in the browser or on the command line. However, testing it in different online php testers doesn't cause an error and produces the expected output. I don't know why this is causing an error on my local. I tried with both php7.3.31 and php 8.0.12 Quote Link to comment https://forums.phpfreaks.com/topic/314199-if-check-with-multiple-logical-operators-failing-with-error/ Share on other sites More sharing options...
ginerjm Posted November 10, 2021 Share Posted November 10, 2021 I am unfamiliar with the statements you are writing. I"m guessing you want to know the value of the set of 4 conditions. Why don't you write an If statement to do that? And what do you want to see after you perform this test? You are only showing a result wrapped in brackets. When I run this it works just fine but it looks rather silly. Here is how I would have done this: echo "Test #1 gives "; $a = 76; $b = true; $c = 0; $d = 500; if(($a > 0) && ($b == false) && ($c == 0) && ($d > 0)) echo "The result is 'true'<br>"; else echo "The result is 'false'<br>"; echo "Test #2 gives "; $a = 76; $b = false; $c = 0; $d = 500; if(($a > 0) && ($b == false) && ($c == 0) && ($d > 0)) echo "The result is 'true'<br>"; else echo "The result is 'false'<br>"; exit(); Quote Link to comment https://forums.phpfreaks.com/topic/314199-if-check-with-multiple-logical-operators-failing-with-error/#findComment-1591918 Share on other sites More sharing options...
raphael75 Posted November 10, 2021 Author Share Posted November 10, 2021 It turned out to be a non-breaking space that was on the line ($b == false) && after the 2nd &. Removing that fixed the issue. Quote Link to comment https://forums.phpfreaks.com/topic/314199-if-check-with-multiple-logical-operators-failing-with-error/#findComment-1591925 Share on other sites More sharing options...
ginerjm Posted November 10, 2021 Share Posted November 10, 2021 It's still an odd looking way to write a simple If. Makes one have to puzzle over the code in the future. Why not write a 'normal' if statement to make it easier for the next guy who has to work with your code? Quote Link to comment https://forums.phpfreaks.com/topic/314199-if-check-with-multiple-logical-operators-failing-with-error/#findComment-1591926 Share on other sites More sharing options...
gizmola Posted November 10, 2021 Share Posted November 10, 2021 3 hours ago, ginerjm said: It's still an odd looking way to write a simple If. Makes one have to puzzle over the code in the future. Why not write a 'normal' if statement to make it easier for the next guy who has to work with your code? Agree, but it's pretty clear this is someone learning PHP who is perhaps doing coursework. Writing code of this sort would not be viewed kindly in any professional environment where there are code reviews. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314199-if-check-with-multiple-logical-operators-failing-with-error/#findComment-1591942 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.