frame

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In Register

PHP Operators 6

PHP Operators

Decrement operator: $x--

<!DOCTYPE html>
<html>
<body>


<?php
$x = 10;  
echo $x--;
?>  


</body>
</html>


Logical operator: and

<!DOCTYPE html>
<html>
<body>


<?php
$x = 100;  
$y = 50;


if ($x == 100 and $y == 50) {
    echo "Hello world!";
}
?>  


</body>
</html>

Logical operator: or

<!DOCTYPE html>
<html>
<body>


<?php
$x = 100;  
$y = 50;


if ($x == 100 or $y == 80) {
    echo "Hello world!";
}
?>  


</body>
</html>

Logical operator: xor

<!DOCTYPE html>
<html>
<body>


<?php
$x = 100;  
$y = 50;


if ($x == 100 xor $y == 80) {
    echo "Hello world!";
}
?>  


</body>
</html>


Logical operator: && (and)

<!DOCTYPE html>
<html>
<body>


<?php
$x = 100;  
$y = 50;


if ($x == 100 xor $y == 80) {
    echo "Hello world!";
}
?>  


</body>
</html>


Logical operator: || (or)

<!DOCTYPE html>
<html>
<body>


<?php
$x = 100;  
$y = 50;


if ($x == 100 || $y == 80) {
    echo "Hello world!";
}
?>  


</body>
</html>

Logical operator: not

<!DOCTYPE html>
<html>
<body>


<?php
$x = 100;  


if ($x !== 90) {
    echo "Hello world!";
}
?>  


</body>
</html>

String operator: Concatenation of $txt1 and $txt2

<!DOCTYPE html>
<html>
<body>


<?php
$txt1 = "Hello";
$txt2 = " world!";
echo $txt1 . $txt2;
?>  


</body>
</html>


String operator: Appends $txt2 to $txt1

<!DOCTYPE html>
<html>
<body>


<?php
$txt1 = "Hello";
$txt2 = " world!";
$txt1 .= $txt2;
echo $txt1;
?>  


</body>
</html>


Array operator: Union (+)

<!DOCTYPE html>
<html>
<body>


<?php
$x = array("a" => "red", "b" => "green");  
$y = array("c" => "blue", "d" => "yellow");  


print_r($x + $y); // union of $x and $y
?>  


</body>
</html>


Array operator: Equality (==)

<!DOCTYPE html>
<html>
<body>


<?php
$x = array("a" => "red", "b" => "green");  
$y = array("c" => "blue", "d" => "yellow");  


var_dump($x == $y);
?>  


</body>
</html>

Array operator: Identity (===)

<!DOCTYPE html>
<html>
<body>


<?php
$x = array("a" => "red", "b" => "green");  
$y = array("c" => "blue", "d" => "yellow");  


var_dump($x === $y);
?>  


</body>
</html>


Array operator: Inequality (!=)

<!DOCTYPE html>
<html>
<body>


<?php
$x = array("a" => "red", "b" => "green");  
$y = array("c" => "blue", "d" => "yellow");  


var_dump($x != $y);
?>  


</body>
</html>


Array operator: Inequality (<>)

<!DOCTYPE html>
<html>
<body>


<?php
$x = array("a" => "red", "b" => "green");  
$y = array("c" => "blue", "d" => "yellow");  


var_dump($x <> $y);
?>  


</body>
</html>


Array operator: Non-identity (!==)

<!DOCTYPE html>
<html>
<body>


<?php
$x = array("a" => "red", "b" => "green");  
$y = array("c" => "blue", "d" => "yellow");  


var_dump($x !== $y);
?>  


</body>
</html>

Conditional assignment operator: Ternary (?:)

<!DOCTYPE html>
<html>
<body>


<?php
   // if empty($user) = TRUE, set $status = "anonymous"
   echo $status = (empty($user)) ? "anonymous" : "logged in";
   echo("<br>");


   $user = "John Doe";
   // if empty($user) = FALSE, set $status = "logged in"
   echo $status = (empty($user)) ? "anonymous" : "logged in";
?>  


</body>
</html>


Conditional assignment: Null coalescing (??)

<!DOCTYPE html>
<html>
<body>


<?php
   // variable $user is the value of $_GET['user']
   // and 'anonymous' if it does not exist
   echo $user = $_GET["user"] ?? "anonymous";
   echo("<br>");
  
   // variable $color is "red" if $color does not exist or is null
   echo $color = $color ?? "red";
?>  


</body>
</html>



Tagged:
Sign In or Register to comment.

Developer

| make vanilla clean and light
@ 2016 Your Sitename, All rights reserved. Material Design is a design language developed by Google. Material Design makes more liberal use of grid-based layouts and responsive animations and transitions.
Powered by VanillaForums, Designed by ThemeSteam

Contact us

info@example.com
support@example.com
(800) 3032120

Get In Touch