Jump to content

if check with multiple logical operators failing with error


raphael75

Recommended Posts

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

Link to comment
Share on other sites

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();

 

Link to comment
Share on other sites

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.

  • Like 1
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.