PHP Programs

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

<html>

<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>

Addtion Operation Result: 62


Add AND Assigment Operation Result: 104
Subtract AND Assignment Operation Result: 62
Multiply AND Assignment Operation Result: 2604
Division AND Assignment Operation Result: 62
Modulus AND Assignment Operation Result: 20
<body>

<?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 xor $y == 20)


echo "xor Success \n";

if ($x == 50 && $y == 30)


echo "&& 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";

// Here var_dump function has been used to


// display structured information. We will learn
// about this function in complete details in further
// articles.
var_dump($a == $c) + "\n";
var_dump($a != $b) + "\n";
var_dump($a <> $b) + "\n";
var_dump($a === $c) + "\n";
var_dump($a !== $c) + "\n";
var_dump($a < $b) + "\n";
var_dump($a > $b) + "\n";
var_dump($a <= $b) + "\n";
var_dump($a >= $b);
?>

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.

Operator Name Operation

If the condition is true? then $x : or else $y. This means that if


the condition is true then the left result of the colon is
?: Ternary accepted otherwise the result is on right.

<?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.

Operator Name Syntax Operation

+ Union $x + $y Union of both i.e., $x and $y


Operator Name Syntax Operation

$x ==
== Equality $y Returns true if both has same key-value pair

!= Inequality $x != $y Returns True if both are unequal

$x === Returns True if both have the same key-value


=== Identity $y pair in the same order and of the same type

Non- $x !== Returns True if both are not identical to each


!== Identity $y other

$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";
?>

Increment/Decrement Operators: These are called the unary operators as


they work on single operands. These are used to increment or decrement
values.
Operator Name Syntax Operation

++ Pre-Increment ++$x First increments $x by one, then return $x

— Pre-Decrement –$x First decrements $x by one, then return $x

++ Post-Increment $x++ First returns $x, then increment it by one

— Post-Decrement $x– First returns $x, then decrement it by one

<?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;
?>

String Operators: This operator is used for the concatenation of 2 or more


strings using the concatenation operator (‘.’). We can also use the
concatenating assignment operator (‘.=’) to append the argument on the right
side to the argument on the left side.
Operator Name Syntax Operation

. Concatenation $x.$y Concatenated $x and $y

Concatenation and First concatenates then assigns,


.= assignment $x.=$y same as $x = $x.$y
<?php
$x = "Geeks";
$y = "for";
$z = "Geeks!!!";
echo $x . $y . $z, "\n";
$x .= $y . $z;
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

echo 1 <=> 1; // outputs 0


echo 3 <=> 4; // outputs -1
echo 4 <=> 3; // outputs 1

// String Comparison

echo "a" <=> "a"; // outputs 0


echo "m" <=> "y"; // outputs -1
echo "y" <=> "c"; // outputs 1

Bitwise Operators

The Bitwise operators is used to perform bit-level operations on the operands.


The operators are first converted to bit-level and then calculation is performed
on the operands. The mathematical operations such as addition , subtraction ,
multiplication etc. can be performed at bit-level for faster processing. In PHP,
the operators that works at bit level are:

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.

Operator Name Example Explanation

& And $a & $b Bits that are 1 in both $a and $b are set to 1, otherwise 0.

| Or (Inclusive or) $a | $b Bits that are 1 in either $a or $b are set to 1

^ Xor (Exclusive or) $a ^ $b Bits that are 1 in either $a or $b are set to 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("Bitwise & of 5 and 3 is $answer");

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");

// Bitwise Left shift


$second = 1;
$answer = $First << $second;
print_r("5 << 1 will be $answer");

print_r("\n");

// Bitwise Right shift


$answer = $First >> $second;
print_r("5 >> 1 will be $answer");

print_r("\n");
?>

You might also like