0% found this document useful (0 votes)
52 views22 pages

Server Side Programming: by Dr. Babaousmail Hassen Lecturer at Binjiang College of NUIST

This document discusses logical operators and array operators in PHP server-side programming. It provides examples of using logical operators like AND, OR, XOR, and NOT to evaluate conditional expressions. It also demonstrates various array operators like union, equality, inequality, identity, and nonidentity to compare arrays and their elements. Logical flowcharts are included to illustrate the logical AND and OR operators. Overall, the document serves as a tutorial on evaluating conditions and comparing arrays in PHP programming.

Uploaded by

Rifat Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views22 pages

Server Side Programming: by Dr. Babaousmail Hassen Lecturer at Binjiang College of NUIST

This document discusses logical operators and array operators in PHP server-side programming. It provides examples of using logical operators like AND, OR, XOR, and NOT to evaluate conditional expressions. It also demonstrates various array operators like union, equality, inequality, identity, and nonidentity to compare arrays and their elements. Logical flowcharts are included to illustrate the logical AND and OR operators. Overall, the document serves as a tutorial on evaluating conditions and comparing arrays in PHP programming.

Uploaded by

Rifat Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Server Side Programming

By Dr. Babaousmail Hassen

Lecturer at Binjiang College of NUIST

1
Logical Operators (Revision)

Example Name Result


$a and $b And TRUE if both $a and $b are TRUE.

$a or $b Or TRUE if either $a or $b is TRUE.


$a xor $b Xor TRUE if either $a or $b is TRUE,
but not both.
! $a Not TRUE if $a is not TRUE.
$a && $b And TRUE if both $a and $b are TRUE.

$a || $b Or TRUE if either $a or $b is TRUE.


PHP logical && operator
In case-1 of the picture, both of the taps are
closed, so the water is not flowing down. Which
explains that if both of conditions are FALSE or
0, the return is FALSE or 0.

In case-2 of the picture, one of the taps are


closed, even then, the water is not flowing
down. Which explains that even if any of
conditions are FALSE or 0, the return is FALSE
or 0.

case-3 of the picture resembles CASE -2.

In case-4 of the picture, both of the taps are


open, so the water is flowing down. Which
explains that if both of conditions are TRUE or
1, the return is TRUE or 1.

So we can conclude that if and only if, both of


the conditions are TRUE or 1, LOGICAL AND
operations returns TRUE or 1.
PHP logical OR operator
In case-1 of the picture, both of the taps
are closed, so the water is not flowing
down. Which explains that if both of
conditions are FALSE or 0, the return is
FALSE or 0.

In case-2 of the picture, one of the taps are


closed, and we can see that the water is
flowing down. Which explains that if any
of conditions are TRUE or 1, the return is
TRUE or 1.

case-3 of the picture resembles CASE -2.


In case-4 of the picture, both of the taps are
open, so the water is flowing down. Which
explains that if both of conditions are
TRUE or 1, the return is TRUE or 1.

So we can conclude that in LOGICAL OR


operation if any of the conditions are true,
the output is TRUE or 1.
The && Operator Example
• We use this operator if we need both values to be true, as in our username and password
test.
• After all, you don't want to let people in if they just get the username right but not the
password! Here's an example:

<?php
$username ='user';
$password ='password';
if ($username =='user' && $password =='password') {
print("Welcome back!");
}
else {
print("Invalid Login Detected");
}
?>

This is an important feature which must be in project of all of you to allow someone
to access your webpage
The | | Operator
We use this symbol when we only need one of your conditions to be true.
For example, suppose you want to grant a discount to people if they have spent more than
100 pounds OR they have a special key (discount voucher).
Else they don't get any discount.
<?php
$total_spent =100;
$special_key ='SK12345';
if ($total_spent ==100 | | $special_key =='SK12345') {
print("Discount Granted!");
}
else {
print("No discount for you!");
}
?>
This time we're testing two conditions and only of them need to be true. If either one of them
is true, then the code gets executed. If they are both false, then PHP will move on.
This is an important feature for those whose project is related to online shoping
system.
AND and OR

These are the same as the first two! AND is the same as && and OR is the same as ||.

As a beginner, you can simply replace this:

$username =='user' && $password =='password


With this
$username =='user' AND $password =='password

And this:
$total_spent ==100 | | $special_key =='SK12345‘
With this:
$total_spent ==100 OR $special_key =='SK12345‘

It's up to you which you use. AND is a lot easier to read than &&. OR is a lot easier to read
than ||.
XOR

It is used when you want to test if one value of two is true but NOT both.
Suppose you had to pick a winner between two contestants.
Only one of them can win. It's an XOR situation!

<?php
$contestant_one = true;
$contestant_two = true;

if ($contestant_one XOR $contestant_two)


{
print("Only one winner!");
}
else {
print("Both can't win!");
}
?>
The ! Operator

This is known as the NOT operator. You can also use it to reverse the value of a true or
false value.
For example, you want to reset a variable to true, if it's been set to false, and vice versa.

<?php
$test_value = false;

if ($test_value == false)
{
print(!$test_value);
}
?>
The code above will print out the number 1!
Array Operators
Example Name Result
$a + $b Union Union of $a and $b.
$a == $b Equality TRUE if $a and $b have
the same key/value
pairs.
$a === $b Identity TRUE if $a and $b have
the same key/value pairs
in the same order and of
the same types.
$a != $b Inequality TRUE if $a is not equal
to $b.
$a <> $b Inequality TRUE if $a is not equal
to $b.
$a !== $b Non-identity TRUE if $a is not
identical to $b.
Examples of Union Array Operators
<?php
$a = array("a" => "apple", "b" => "banana");
$b = array("a" => "pear", "b" => "strawberry", "c" => "cherry");

$c = $a + $b; // Union of $a and $b


echo "Union of \$a and \$b: \n";
var_dump($c);

$c = $b + $a; // Union of $b and $a


echo "Union of \$b and \$a: \n";
var_dump($c);

$a += $b; // Union of $a += $b is $a and $b


echo "Union of \$a += \$b: \n";
var_dump($a);
?>
Union of $a and $b:
array(3) {
["a"]=>
string(5) "apple"
["b"]=>
string(6) "banana"
["c"]=>
string(6) "cherry"
}
Union of $b and $a:
array(3) {
["a"]=>
string(4) "pear"
["b"]=>
string(10) "strawberry"
["c"]=>
string(6) "cherry"
}
Union of $a += $b:
array(3) {
'a' =>
string(5) "apple"
'b' =>
string(6) "banana"
'c' =>
string(6) "cherry"
}
Example
<?php

$array1 = array("a" => "Java", "b" => "Objective-C", "c" => ".Net");
$array2 = array( "a" => "Smalltalk","d" => "Pearl", "e" => "PHP");

$union1 = $array1 + $array2; // Union of $array1 and $array2


echo "Union of \$array1 and \$array2: <br>";
var_dump($union1);

$union2 = $array2 + $array1; // Union of $array2 and $array1


echo "<br>Union of \$array2 and \$array1: <br>";
var_dump($union2);

?>
Result
Union of $array1 and $array2:
array(5) {
["a"]=> string(4) "Java"
["b"]=> string(11) "Objective-C"
["c"]=> string(4) ".Net"
["d"]=> string(5) "Pearl"
["e"]=> string(3) "PHP"
}
Union of $array2 and $array1:
array(5) {
["a"]=> string(9) "Smalltalk"
["d"]=> string(5) "Pearl"
["e"]=> string(3) "PHP"
["b"]=> string(11) "Objective-C"
["c"]=> string(4) ".Net"
}
Example of Equality Array Operators (==)

Equality Array Operator is used to <?php


check if both arrays have the same $array1 = array("Java","Smalltalk","Objective-
elements or not. C","Perl");
$array2 = array("Java","Smalltalk","Objective-
C","Perl");
If array1 and array2 have the same $array3 = array("Java","Smalltalk","Objective-
elements (key value pairs) then C","Perl",".Net");
boolean true is returned, else
boolean false is returned. echo var_dump($array1 == $array2) ."<br>";

echo var_dump($array2 == $array3) ."<br>";


?>

Output

bool(true)
bool(false)
Example of Inequality Array Operators (!=)

Inequality Array Operator is used to <?php


check if both arrays have the same $array1 = array("Java","Smalltalk","Objective-
elements or not. C","Perl");
$array2 = array("Java","Smalltalk","Objective-
If array1 and array2 have do not have C","Perl");
same elements (key value pairs) then $array3 = array("Java","Smalltalk","Objective-
boolean false is returned, else boolean C","Perl",".Net");
true is returned.
echo var_dump($array1 != $array2) ."<br>";

echo var_dump($array2 != $array3) ."<br>";


?>

Output

bool(false)
bool(true)
Example of Identity Array Operators (===)

<?php
$array1 = array("PHP", "Java");
$array2 = array("1" => "Java", "0" => "PHP");

// == returns true
Two arrays are Identical Only when the echo var_dump($array1 == $array2) ."<br>";
Keys and Values both are equal.
// === returns false as keys are different
echo var_dump($array1 === $array2) ."<br>";
?>

Output

bool(true)
bool(false)
Example of Nonidentity Array Operators (!==)

<?php
$array1 = array("PHP", "Java");
$array2 = array("1" => "Java", "0" => "PHP");

Two arrays are Non-identity when the Keys // == returns false


and Values both are not equal. echo var_dump($array1 != $array2) ."<br>";

// === returns true as keys are different


echo var_dump($array1 !== $array2) ."<br>";
?>

Output

bool(false)
bool(true)
String Operators

There are two string operators. The first is the concatenation operator ('.'), which
returns the concatenation of its right and left arguments. The second is the
concatenating assignment operator ('.='), which appends the argument on the right
side to the argument on the left side.
<?php

$a = "Hello ";
$b = $a . "World!";
// now $b contains "Hello World!"

$a = "Hello ";
$a .= "World!";
// now $a contains "Hello World!“
?>
Some examples

<?php

echo "thr"."ee"; //prints the string "three"


echo "twe" . "lve"; //prints the string "twelve"
echo 1 . 2; //prints the string "12"
echo 1+2; //prints the number 3
Be careful don't type "." instead
of ";" at the end of a line. ?>

<? php
echo 'a'.
$c = 'x';
echo 'b';
echo 'c';
?>
Combined Assignment Operators
In addition to the simple assignment, there is a set of combined assignment
operators. Each of these is a shorthand way of doing another operation on a variable
and assigning the result back to that variable. For example
$a += 5;
This is equivalent to writing:
$a = $a + 5;
Combined assignment operators exist for each of the arithmetic operators and for
the string concatenation operator.
A summary of all the combined assignment operators and their effects is shown in
Table below.
Operator Use Equivalent to
+= $a += $b $a = $a + $b
-= $a -= $b $a = $a - $b
*= $a *= $b $a = $a * $b
/= $a /= $b $a = $a / $b
%= $a %= $b $a = $a % $b
.= $a .= $b $a = $a . $b
Home Assignment-III

 Prepare a brief note about this class

send it to this e-mail: [email protected]

Deadline: before Tuesday (6 October).

22

You might also like