Onur Özgür ÖZKAN

Php, Ruby, Kebab, Git Geek

Difference Between & and &&

& is unconditional logical AND; && is conditional logical AND; | is unconditional logical OR; || is  conditional logical OR;

The diffrence  between conditional and unconditional logical operator is the single ampersand always evaluates both arguments whereas the double ampersand will only evaluate the second argument if the first argument is true.

Check the below examples.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php

$c = 1;

(true || $c++);
echo $c . "<br />"; //Output 1

(true | $c++);
echo $c . "<br />"; //Output 2

$c = 1;

(false && $c++);
echo $c . "<br />"; //Output 1

(false & $c++);
echo $c . "<br />"; //Output 2

?>

Best Regards.