9/22/2018

PHP Operators | PHP Tutorial Responsive Blogger Template

Php Operators:- "Php Operators means operations on operands by specific symbols".
Php operators use with data types (String, Boolean, Arrays).



Types of Php Operators:-
  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators
PHP Arithmetic Operators:- Php arithmetic operators perform on mathematics operations. Php arithmetic Operators as follows:
Php operators | photo arithmetic operators
Php Arithmetic Operators
For Example:-
<?php
$a = 5;
$b = 2;

echo($a + $b);
echo "<br>";

echo($a - $b);
echo "<br>";

echo($a * $b);
echo "<br>";

echo($a / $b);
echo "<br>";

echo($a % $b);

?>

Output:- 7
              3
              10
              2.5
              1

PHP Assignment Operators:- Php Assignment operator is used to assign the value to a variable. It represent or stores right-hand side value in left side variable.
Php Assignment Operator




For Example:-
<?php
$a = 10;
echo $a;
echo "<br>";

$a = 20;
$a += 30;
echo $a;
echo "<br>";

$a = 50;
$a -= 20;
echo $a;
echo "<br>";

$a = 5;
$a *= 25;
echo $a;
echo "<br>";

$a = 50;
$a /= 10;
echo $a;
echo "<br>";

$a = 100;
$a %= 15;
echo $a;

?>

Output:- 10
              50
              30
              125
              5
              10
PHP Comparison Operators:- Php comparison operators are used to compare to variable or values. For example A=10 and B=15 then A not equal to B 

For Example:-
<?php
$x = 25;
$y = 35;
$z = "25";

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

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

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

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

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

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

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

var_dump($x >= $y);

?>

Output:- bool(true) 
              bool(false)               
              bool(true)               
              bool(true)
              bool(true)
              bool(false)  
              bool(true)   
              bool(false)
PHP Increment/Decrement Operators:- Php Increment Operators are used to increment value same as Decrement Operators are used to decrementing value. Two types of Increment/Decrement Operators

  • Pre-Increment/Decrement Operators
  • Post-Increment/Decrement Operators
For Example:- 
<?php
$x = 10;
echo ++$x;
echo "<br>";
echo $x;
echo "<hr>";

$x = 10;
echo $x++;
echo "<br>";
echo $x;
echo "<hr>";

$x = 10;
echo --$x;
echo "<br>";
echo $x;
echo "<hr>";

$x = 10;
echo $x--;
echo "<br>";
echo $x;
?>

Output:- 11
                    11

                    10
                    11

                    9
                    9

                    10
                    9
PHP Logical Operators:- PHP Logical Operators are used to combining conditions and output result true or false.
For Example:- 
<?php
$year = 2014;
// Leap years are divisible by 400 or by 4 but not 100
if(($year % 400 == 0) || (($year % 100 != 0) && ($year % 4 == 0))){
    echo "$year is a leap year.";
} else{
    echo "$year is not a leap year.";
}
?>

Output:- 2014 is not a leap year.
PHP String Operators:- PHP String Operators specially design for PHP language.
For Example:- 
<?php
$x = "Hello";
$y = " World!";
echo $x . $y; // Outputs: Hello World!
echo "<br>";

$x .= $y;
echo $x; // Outputs: Hello World!

?>

Output:- Hello World!
              Hello World!
PHP Array Operators:- PHP Array Operators are used to compare arrays.
For Example:-
<?php
$x = array("a" => "Red", "b" => "Green", "c" => "Blue");
$y = array("u" => "Yellow", "v" => "Orange", "w" => "Pink");
$z = $x + $y; // Union of $x and $y
var_dump($z);
echo "<hr>";

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

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

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

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

var_dump($x !== $y);

?>

Output:- bool(false) 
             bool(false)
             bool(true) 
             bool(true)  
             bool(true)

XEM THÊM

PHP Operators | PHP Tutorial
4/ 5
Oleh

2 comments

Tulis comments