Translate

Monday, May 5, 2014

7.Operators in PHP

Arithmetic Operators

The example below shows the different results of using the different arithmetic operators:

example:

<?php 
$x=10; 
$y=6;
echo ($x + $y); // outputs 16
echo ($x - $y); // outputs 4
echo ($x * $y); // outputs 60
echo ($x / $y); // outputs 1.6666666666667 
echo ($x % $y); // outputs 4 
?>

Result
16
4
60
1.6666666666667
4

Assignment Operators

The PHP assignment operators is used to write a value to a variable.
The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right.
The example below shows the different results of using the different assignment operators:

example:

<?php 
$x=10; 
echo $x; // outputs 10

$y=20; 
$y += 100;
echo $y; // outputs 120

$z=50;
$z -= 25;
echo $z; // outputs 25

$i=5;
$i *= 6;
echo $i; // outputs 30

$j=10;
$j /= 5;
echo $j; // outputs 2

$k=15;
$k %= 4;
echo $k; // outputs 3
?>

Comparison Operators

The example below shows the different results of using some of the comparison operators:
example:
<?php
$x=100; 
$y="100";

var_dump($x == $y);
echo "<br>";
var_dump($x === $y);
echo "<br>";
var_dump($x != $y);
echo "<br>";
var_dump($x !== $y);
echo "<br>";

$a=50;
$b=90;

var_dump($a > $b);
echo "<br>";
var_dump($a < $b);
?>

Logical Operators

PHP Array Operators
The PHP array operators are used to compare arrays:

The example below shows the different results of using the different array operators:
example:
<?php
$x = array("a" => "red", "b" => "green"); 
$y = array("c" => "blue", "d" => "yellow"); 
$z = $x + $y; // union of $x and $y
var_dump($z);
var_dump($x == $y);
var_dump($x === $y);
var_dump($x != $y);
var_dump($x <> $y);
var_dump($x !== $y);
?>

PHP Increment / Decrement Operators



0 comments:

Post a Comment