Chapter 6 (Basic PHP Syntax) - 2
Chapter 6 (Basic PHP Syntax) - 2
Chapter 6
Outlines
⚫ PHP Operators
⚫ Arithmetic Operators
⚫ Assignment Operators
⚫ Comparison Operators
⚫ Logical Operators
⚫ Conditional Assignment Operators
⚫ Incrementing & Decrementing Operators
⚫ PHP Arrays
2
PHP Operators
PHP Arithmetic Operators
3
Example “Arithmetic Operators”
<?php
$x = 10;
$y = 4;
echo($x + $y); // 0utputs: 14
echo($x - $y); // 0utputs: 6
echo($x * $y); // 0utputs: 40
echo($x / $y); // 0utputs: 2.5
echo($x % $y); // 0utputs: 2
echo($x ** $y); // 0utputs: 10000
?>
PHP Operators
PHP Assignment Operators
5
Example “Assignment Operators”
<?php $x = 5;
$x = 10; $x *= 25;
echo $x; // Outputs: 10 echo $x; // Outputs: 125
$x = 20; $x = 50;
$x += 30; $x /= 10;
echo $x; // Outputs: 50 echo $x; // Outputs: 5
$x = 50; $x = 100;
$x -= 20; $x %= 15;
echo $x; // Outputs: 30 echo $x; // Outputs: 10
?>
PHP Operators
PHP Comparison Operators
7
Example “comparison operators”
<?php
$x = 25;
$y = 35;
$z = 35;
var_dump($y == $z); // Outputs: boolean true
var_dump($x === $z); // Outputs: boolean false
var_dump($x != $y); // Outputs: boolean true
var_dump($x !== $z); // Outputs: boolean true
var_dump($x < $y); // Outputs: boolean true
var_dump($x > $y); // Outputs: boolean false
var_dump($x <= $y); // Outputs: boolean true
var_dump($x >= $y); // Outputs: boolean false
?>
Example “Spaceship <=>”
<?php
// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1
// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1
?>
PHP Operators
PHP Conditional Assignment Operators
10
Ternary Example
$www = 123;
$msg = $www > 100 ? "Large" : "Small" ;
echo "First: $msg \n";
First: Large
Second: Small
Third: Odd
PHP Operators
❑ PHP Incrementing and Decrementing Operators
Operator Name Effect
++$x Pre-increment Increments $x by one, then
returns $x
$x++ Post-increment Returns $x, then
increments $x by one
--$x Pre-decrement Decrements $x by one,
then returns $x
$x-- Post-decrement Returns $x, then
decrements $x by one
Example “Incrementing & Decrementing”
<?php <?php
$x = 10; $x = 10;
echo $x++; // Outputs: 10 echo $x--; // Outputs: 10
echo $x; // Outputs: 11 echo $x; // Outputs: 9
$x = 10; $x = 10;
echo ++$x; // Outputs: 11 echo --$x; // Outputs: 9
echo $x; // Outputs: 11 echo $x; // Outputs: 9
?> ?>
What is the difference between “=> and ->”?
⚫ The two operators, => and -> may look similar but are totally
different in their usage.
⚫ =>
⚫ is referred to as double arrow operator.
⚫ It is an assignment operator used in associative arrays to assign values to
the key-value pairs when creating arrays.
⚫ It is placed in between the key and the value and assigns what is on its
right(value) to what is on its left(key).
Example “=>”
<?php $ <?php $
$person = array( $person = [
"firstName" => "John", "firstName" => "John",
"lastName" => "Doe", "lastName" => "Doe",
"age" => 28, "age" => 28,
"gender" => "Male", "gender" => "Male",
"email" => "[email protected]", "email" => "[email protected]",
"city" => "Germany" "city" => "Germany"
); ];
?> ?>
-> in PHP?
⚫ ->
⚫ is referred to as the object operator or single arrow operator.
⚫ It is used for accessing the methods and properties of a class object.
Working With Arrays
⚫ An array is a compound variable that may contain more than
one value.
⚫ Each value in the array is called an element.
⚫ Each element in the array is identified by an index (also called a key)
that is enclosed in square brackets, [ ].
⚫ The square brackets tell PHP that the variable is an array variable
instead of a regular variable.
⚫ The index can be a number or a string.
⚫ If the index is a number, it’s usually called an index;
⚫ if it’s a string, it’s usually called a key.
⚫ Arrays using string indexes are called associative arrays:
PHP Arrays
⚫ In PHP, there are three kind of arrays:
1) Numeric array - An array with a numeric index
2) Associative array - An array where each ID key is associated with a value
3) Multidimensional array - An array containing one or more arrays
PHP Numeric Arrays
❑ A numeric array stores each array element with a numeric
index.
❑ There are two methods to create a numeric array.
1) In the following example the index is automatically assigned (the
index starts at 0):
$cars = array(“Nissan", “Honda", “Foard");
2) In the following example we assign the index manually:
$cars[0] = “Toyota";
$cars[1] = “Honda“;
$cars[2] = “Foard";
PHP Numeric Arrays
❑ In the following example you access the variable values by
referring to the array name and index:
27