4. PHP – Operators.

Posted: September 20, 2013 in Hypertext Preprocessor (PHP)
Tags: ,

==>Arithmetic operators.

We can perform various arithmetic operations like addition (+), subtraction (-), multiplication (*), division (/) and modulus (%).
$variable (or a constant value) operator $variable (or a constant value).
Here is the code that demonstrates various arithmetic operations.

<?php
//Arithmetic operators.
echo "<strong>Arithmetic operations</strong> <br>";
$x=10.35;
$y=6;
echo "Additon <br>";
echo ($x + $y); //Addition.
echo "<br>Subtraction <br>";
echo ($x - $y); //Subtraction.
echo "<br>Multiplication <br>";
echo ($x * $y); //Multiplication.
echo "<br>Division <br>";
echo ($x / $y); //Division.
echo "<br>Modulus <br>";
echo ($x % $y); //Modulus.
?>

==>Assignment Operator.

We use ‘=’ to assign.
$variable = <value>
The example code :

<?php
//Assignment Operator.
echo "<br> <br><strong>Assignment operations</strong> <br>";
$x = $y;
echo "\$x has been assigned the value of \$y. Now \$x=$x";
?>

==>String Operators.

Concatenation : We use the dot (.) operator to achieve concatenation.
$variable (or a string).$variable (or a string)

<?php
//String Operators.
echo "<br> <br><strong>String operations</strong> <br>";
echo "Concatenation.<br>";
$a = "open ";
$b = "source";
$c = $a.$b; //concatenation.
echo "$c";
?>

==>Increment/Decrement Operators.

1. Pre-increment (++$variable).
2. Post-increment ($variable++).
3. Pre-decrement (–$variable).
4. Post-decrement ($variable–).

<?php
//Increment/Decrement Operators.
echo "<br> <br><strong>Increment/Decrement operations</strong> <br>";
echo "1.Pre-increment.<br>";
$x=0;
echo ++$x; // Pre-increment
echo "<br>2.Post-increment.<br>";
$y=0;
echo $y++; // Post-increment
echo "<br>3.Pre-decrement.<br>";
$z=10;
echo --$z; // Pre-decrement
echo "<br>4.Post-decrement.<br>";
$a=10;
echo $a--; // Post-decrement
?>

==>Comparison Operators.

1. Equal (==) :  Data types are not considered, just the values are considered.
2. Identical (===) : Data types are considered.
3. Not equal (!= ).
4. Not identical (!==).
5. Less than (<).
6. Less than or equal (<=).
7. Greater than (>).
8. Greater than or equal (>=).

<?php
//Comparison Operators.
echo "<br> <br><strong>Comparison operations</strong> <br>";
$x=2013;
$y="2013";
echo "1.Equal.<br>";
var_dump($x == $y); //Equal
echo "<br>2.Identical.<br>";
var_dump($x === $y); // Identical
echo "<br>3.Not equal.<br>";
var_dump($x != $y); // Not equal
echo "<br>4.Not identical.<br>";
var_dump($x !== $y); // Not identical
$a=5;
$b=10;
echo "<br>5.Greater than.<br>";
var_dump($a > $b); //Greater than
echo "<br>6.Less than<br>";
var_dump($a < $b); //Less than
?>

==>Logical Operators.

1. AND ($variable and $variable / $variable && $variable).
2. OR ($variable or $variable / $variable || $variable).
3. XOR ($variable xor $variable).
4. NOT (!$variable).

<?php
//Logical Operators.
echo "<br> <br><strong>Logical operations</strong> <br>";
$x=true;
$y=false;
echo "1.AND<br>";
var_dump($x && $y); // And
echo "<br>2.OR<br>";
var_dump($x || $y); // Or
echo "<br>3.XOR<br>";
var_dump($x xor $y); // Xor
echo "<br>4.NOT<br>";
var_dump(!$x); // Not
?>

==>Array operators.

1. Union (+) : Duplicates are removed.
2. Equality (==).
3. Identity (===).
4. Inequality (!=).
5. Inequality (<>).
6. Non-identity (!==).

<?php
//Array operators.
echo "<br> <br><strong>Array operations</strong> <br>";
$x = array("a" => "mango", "b" => "cherry");
$y = array("b" => "cherry", "c" => "apple");
$z = $x + $y; // union of $x and $y
echo "1.union<br>";
var_dump($z);
echo "<br>2.Equality<br>";
var_dump($x == $y);
echo "<br>3.Identity<br>";
var_dump($x === $y);
echo "<br>4.Inequality<br>";
var_dump($x != $y);
echo "<br>5.Inequality<br>";
var_dump($x <> $y);
echo "<br>6.Non-identity<br>";
var_dump($x !== $y);
?>
Advertisements
Comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s