Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,855 questions

51,776 answers

573 users

How to use logical operators in PHP

1 Answer

0 votes
function and_test($x, $y)
{
    $z = ($x && $y) ? "true" : "false"; // True if $x and $y are true  
    
    return $x . " and " . $y . " = " . $z . "<br />";    
}
echo and_test(1, 1);
echo and_test(1, 0);
echo and_test(0, 1);
echo and_test(0, 0);

// if ($x && $y)

echo "<br />";

function or_test($x, $y)
{
    $z = ($x || $y) ? "true" : "false"; // True if $x or $y is true
    
    return $x . " or " . $y . " = " . $z . "<br />"; 
}
echo or_test(1, 1);
echo or_test(1, 0);
echo or_test(0, 1);
echo or_test(0, 0);

// if ($x || $y)

echo "<br />";

function xor_test($x, $y)
{
    $z = ($x xor $y) ? "true" : "false"; // True if $x or $y is true, but not both
    
    return $x . " xor " . $y . " = " . $z . "<br />"; 
}
echo xor_test(1, 1);
echo xor_test(1, 0);
echo xor_test(0, 1);
echo xor_test(0, 0);

// if ($x xor $y)

echo "<br />";

function not_test($x)
{
    $z = (!$x) ? "true" : "false"; // True if $x is false
    
    return "!" . $x . " = " . $z . "<br />"; 
}

echo not_test(1);
echo not_test(0);

// if (!$x)

/*
run:

1 and 1 = true
1 and 0 = false
0 and 1 = false
0 and 0 = false

1 or 1 = true
1 or 0 = true
0 or 1 = true
0 or 0 = false

1 xor 1 = false
1 xor 0 = true
0 xor 1 = true
0 xor 0 = false

!1 = false
!0 = true

*/

 



answered Nov 7, 2015 by avibootz

Related questions

1 answer 121 views
121 views asked Oct 12, 2022 by avibootz
1 answer 114 views
114 views asked Aug 13, 2022 by avibootz
1 answer 111 views
111 views asked Aug 13, 2022 by avibootz
1 answer 151 views
1 answer 177 views
1 answer 176 views
1 answer 159 views
159 views asked Jan 12, 2016 by avibootz
...