User Tools

Site Tools


programmieren:grundlagen:logical_operators

Logical operators

Increment

++$a // Pre increments it, returns first then increments
$a++ // Post increments it, increments and then returns
--$a // Same but decrement
$a-- // Same but decrement

Assignment

$a;
 
$a = 5; // = is used as an asigning operator
 
// Arithmetic assignment
$a = 2;
$b = 2;
 
$a += $b; // $a is now equal to 4, also works with all other arithmetic operations
 
// Concatenation
$a = 'hel';
$b = 'lo';
 
$a .= $b; // $a is now equal to 'Hello'
 
// Null coalesence
 
$a = null;
$b = 4;
 
$a ??= $b; // Incase $a is null it now equals $b
 
 
// Assignment with booleans
 
$a = true and false // Assignment operator is stronger and $a equals true
 
$b = (true and false) // Condition is stronger and $b equals false

Comparison

// Is equal
 
$a = 50;
$b = 30;
 
$a == $b; // False
 
$a = 50;
$b = 50;
 
$a == $b; // True
 
$a = 1;
$b = true;
 
$a == $b; // True
 
// Is identical
 
$a = 50;
$b = 30;
 
$a === $b; // False
 
$a = 50;
$b = 50;
 
$a === $b; // True
 
$a = 1;
$b = true;
 
$a === $b; // False
 
// Is not equal
 
$a = 50;
$b = 30;
 
$a != $b; // True
 
$a = 50;
$b = 50;
 
$a != $b; // False
 
$a = 1;
$b = true;
 
$a != $b; // False
 
// Is not identical
 
$a = 50;
$b = 30;
 
$a !== $b; // True
 
$a = 50;
$b = 50;
 
$a !== $b; // False
 
$a = 1;
$b = true;
 
$a !== $b; // True
 
// Is less than
 
$a = 50;
$b = 30;
 
$a < $b; // False
 
$a = 50;
$b = 50;
 
$a < $b; // False
 
$a = 30;
$b = 50;
 
$a < $b; // True
 
// Is greater than
 
$a = 50;
$b = 30;
 
$a > $b; // True
 
$a = 50;
$b = 50;
 
$a > $b; // False
 
$a = 30;
$b = 50;
 
$a > $b; // False
 
// Is less than or equal to
 
$a = 50;
$b = 30;
 
$a <= $b; // False
 
$a = 50;
$b = 50;
 
$a <= $b; // True
 
$a = 30;
$b = 50;
 
$a <= $b; // True
 
// Is greater than or equal to
 
$a = 50;
$b = 30;
 
$a >= $b; // True
 
$a = 50;
$b = 50;
 
$a >= $b; // True
 
$a = 30;
$b = 50;
 
$a >= $b; // False
 
// Spaceship???, returns an int indicating if value is bigger, smaller or equal to other value
 
$a = 50;
$b = 30;
 
$a <=> $b; // 1
 
$a = 50;
$b = 50;
 
$a < $b; // 0
 
$a = 30;
$b = 50;
 
$a < $b; // -1

Logical

$a = true;
$b = false;
 
$a and $b // False
 
$a or $b // True
 
$a xor $b // True
 
$a && $b // False
 
$a || $b // True
 
$a = true;
$b = true;
 
$a and $b // True
 
$a or $b // True
 
$a xor $b // False
 
$a && $b // True
 
$a || $b // True
 
$a = false;
$b = false;
 
$a and $b // False
 
$a or $b // False
 
$a xor $b // False
 
$a && $b // False
 
$a || $b // False
 
// Symbols take presedence over words

String

// Concatenation
 
$a = 'Foo';
$b = 'bar';
 
$a = $a . $b; // $a = 'Foobar'
 
// Assignment
 
$a .= $b; // $a = 'Foobar'

Array

// Union
 
$a = ["a" => "apple", "b" => "banana"];
$b = ["a" => "pear", "b" => "strawberry", "c" => "cherry"];
 
$c = $a + $b; // $c = ["a" => "apple", "b" => "banana", "c" => "cherry"]. Any keys that already exist in the previous array are ignored

Types

// instanceof
 
class ParentClass {}
class MyClass extends ParentClass {}
class NotMyClass {}
 
$a = new MyClass;
$b = new NotMyClass;
$c = new ParentClass;
 
$a instanceof MyClass // True
$a instanceof NotMyClass // False
$a instanceof ParentClass // True, works with any kind of inheritance
 
$b instanceof MyClass // False
$b instanceof NotMyClass // True
$b instanceof ParentClass // False
 
$c instanceof MyClass // False
$c instanceof NotMyClass // False
$c instanceof ParentClass // True
programmieren/grundlagen/logical_operators.txt · Last modified: by admin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki