PHP Programs
PHP Programs
<head>
<title>Arithmetical Operators</title>
</head>
<body>
<?php
$a = 42;
$b = 20;
$c = $a + $b;
echo "Addtion Operation Result: $c <br/>";
$c = $a - $b;
echo "Substraction Operation Result: $c <br/>";
$c = $a * $b;
echo "Multiplication Operation Result: $c <br/>";
$c = $a / $b;
echo "Division Operation Result: $c <br/>";
$c = $a % $b;
echo "Modulus Operation Result: $c <br/>";
$c = $a++;
echo "Increment Operation Result: $c <br/>";
$c = $a--;
echo "Decrement Operation Result: $c <br/>";
?>
</body>
</html>
<html>
<head>
<title>Assignment Operators</title>
</head>
<body>
<?php
$a = 42;
$b = 20;
$c = $a + $b;
echo "Addtion Operation Result: $c <br/>";
$c += $a;
echo "Add AND Assigment Operation Result: $c <br/>";
$c -= $a;
echo "Subtract AND Assignment Operation Result: $c <br/>";
$c *= $a;
echo "Multiply AND Assignment Operation Result: $c <br/>";
$c /= $a;
echo "Division AND Assignment Operation Result: $c <br/>";
$c %= $a;
echo "Modulus AND Assignment Operation Result: $c <br/>";
?>
</body>
</html>
<?php
$a = 42;
$b = 20;
if( $a == $b ) {
echo "TEST1 : a is equal to b<br/>";
}else {
echo "TEST1 : a is not equal to b<br/>";
}
if( $a > $b ) {
echo "TEST2 : a is greater than b<br/>";
}else {
echo "TEST2 : a is not greater than b<br/>";
}
if( $a < $b ) {
echo "TEST3 : a is less than b<br/>";
}else {
echo "TEST3 : a is not less than b<br/>";
}
if( $a != $b ) {
echo "TEST4 : a is not equal to b<br/>";
}else {
echo "TEST4 : a is equal to b<br/>";
}
if( $a >= $b ) {
echo "TEST5 : a is either greater than or equal to b<br/>";
}else {
echo "TEST5 : a is neither greater than nor equal to b<br/>";
}
if( $a <= $b ) {
echo "TEST6 : a is either less than or equal to b<br/>";
}else {
echo "TEST6 : a is neither less than nor equal to b<br/>";
}
?>
</body>
</html>
TEST1 : a is not equal to b
TEST2 : a is greater than b
TEST3 : a is not less than b
TEST4 : a is not equal to b
TEST5 : a is either greater than or equal to b
TEST6 : a is neither less than nor equal to b
Logical operators
<?php
$x = 50;
$y = 30;
if ($x == 50 and $y == 30)
echo "and Success \n";
if ($x == 50 or $y == 20)
echo "or Success \n";
if ($x == 50 || $y == 20)
echo "|| Success \n";
if (!$z)
echo "! Success \n";
?>
o/p
and Success
or Success
xor Success
&& Success
|| Success
! Success
Comparision operators
<?php
$a = 80;
$b = 50;
$c = "80";
Output:
bool(true)
bool(true)
bool(true)
bool(false)
bool(true)
bool(false)
bool(true)
bool(false)
bool(true)
Conditional or Ternary Operators :
These operators are used to compare two values and take either of the results
simultaneously, depending on whether the outcome is TRUE or FALSE. These
are also used as a shorthand notation for if…else statement that we will read in
the article on decision making.
Syntax:
$var = (condition)? value1 : value2;
Here, the condition will either evaluate as true or false. If the condition
evaluates to True, then value1 will be assigned to the variable $var otherwise
value2 will be assigned to it.
<?php
$x = -12;
echo ($x > 0) ? 'The number is positive' : 'The number is negative';
?>
Array Operators: These operators are used in the case of arrays. Here are the
array operators along with their syntax and operations, that PHP provides for
the array operation.
$x ==
== Equality $y Returns true if both has same key-value pair
$x <>
<> Inequality $y Returns True if both are unequal
<?php
$x = array("k" => "Car", "l" => "Bike");
$y = array("a" => "Train", "b" => "Plane");
var_dump($x + $y);
var_dump($x == $y) + "\n";
var_dump($x != $y) + "\n";
var_dump($x <> $y) + "\n";
var_dump($x === $y) + "\n";
var_dump($x !== $y) + "\n";
?>
<?php
$x = 2;
echo ++$x, " First increments then prints \n";
echo $x, "\n";
$x = 2;
echo $x++, " First prints then increments \n";
echo $x, "\n";
$x = 2;
echo --$x, " First decrements then prints \n";
echo $x, "\n";
$x = 2;
echo $x--, " First prints then decrements \n";
echo $x;
?>
Spaceship Operators
This article will make you aware of a very useful operator i.e the spaceship
operator PHP 7. The spaceship operator or combined comparison operator is
denoted by “<=>“. This is a three-way comparison operator and it can perform
greater than, less than and equal comparison between two operands.
This operator has similar behavior like strcmp() or version_compare(). This
operator can be used with integers, floats, strings, arrays, objects, etc.
This <=> operator offers combined comparison :
Return 0 if values on either side are equal
Return 1 if value on the left is greater
Return -1 if the value on the right is greater
// Comparing Integers
// String Comparison
Bitwise Operators
The bitwise operators are used to perform bit-level operations on operands. These operators
allow the evaluation and manipulation of specific bits within the integer.
& And $a & $b Bits that are 1 in both $a and $b are set to 1, otherwise 0.
~ Not ~$a Bits that are 1 set to 0 and bits that are 0 are set to 1
<< Shift left $a << $b Left shift the bits of operand $a $b steps
>> Shift right $a >> $b Right shift the bits of $a operand by $b number of places
<?php
// PHP code to demonstrate Bitwise Operator.
// Bitwise AND
$First = 5;
$second = 3;
$answer = $First & $second;
print_r("\n");
// Bitwise OR
$answer = $First | $second;
print_r("Bitwise | of 5 and 3 is $answer");
print_r("\n");
// Bitwise XOR
$answer = $First ^ $second;
print_r("Bitwise ^ of 5 and 3 is $answer");
print_r("\n");
// Bitwise NOT
$answer = ~$First;
print_r("Bitwise ~ of 5 is $answer");
print_r("\n");
print_r("\n");
print_r("\n");
?>