Server Side Programming: by Dr. Babaousmail Hassen Lecturer at Binjiang College of NUIST
Server Side Programming: by Dr. Babaousmail Hassen Lecturer at Binjiang College of NUIST
1
Logical Operators (Revision)
<?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 ||.
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;
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");
$array1 = array("a" => "Java", "b" => "Objective-C", "c" => ".Net");
$array2 = array( "a" => "Smalltalk","d" => "Pearl", "e" => "PHP");
?>
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 (==)
Output
bool(true)
bool(false)
Example of Inequality Array Operators (!=)
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");
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
<? 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
22