Working With Variables
Working With Variables
VARIABLES
$year; 2017
NULL
$year = 2017; year
VARIABLE NAMES ARE CASE SENSITIVE
$x = 200; 200
x
$X = 200; 200
X
WEAK TYPING
$myVariable;
false
Hello
200
NULL
true
myVariable
$myVariable = 200;
$myVariable = “Hello”;
$myVariable = ‘Hello’;
$myVariable = true;
$myVariable = false;
STRINGS
echo $message;
WORKING WITH STRINGS
Array
Compound
Object
Null
Special
Resources
Type
integer Double/float String Boolean
Example
5 10.56 “php” True/false
Description
Integers are the simplest type Strings are any combination A boolean expresses a truth
of data. They are simply Floating point numbers of letters, numbers, spaces value. It can be
numbers. (Whole numbers) and other characters either TRUE or FALSE.
Type
Array Object Null Resources
Example
Array(1.2.3) $bar = new foo; Id#2
Description
An array is a multitude
A resources is a special
of elements that have The special NULL value
collection of variables data type, holding a
been grouped together represents a variable
& functions reference to an
within a single PHP with no value
external resource
variable.
USING OPERATORS
Unary
Unary operators act on one operand
++ -- !
Binary
Binary operators are used on two operands:
Arithmetic + - * / %
Arithmetic Assignment += -= *= /=
Assignment =
Comparison == === != !== < >
<= >=
Logical and or xor ! && ||
Ternary
?:
ASSIGNMENT OPERATOR
$balance = 500;
ARTHIMETIC OPERATORS
+ - * /
$a = 100;
$b = 50;
$result = $a +-/* $b;
150
2
5000
50
Comparison Operators
OPERATOR PRECEDENCE
$result = 5 + 5 * 10;
55 5 + 50
OPERATOR PRECEDENCE
$result = (5 + 5) * 10;
100 10 * 10
ARTHIMETIC OPERATORS
$a = $a + 1; $a = $a - 1;
$a += 1; $a -= 1;
$a++; $a--;
++$a; --$a;
Concatenation Operator (.)
• The concatenation operator concatenates two
strings. This operator works only on strings; thus,
any non-string operand is first converted to one.
• The following example would print out “welcome
to Hidaya" :
• <?php
• $name = “Hidaya”;
• echo " welcome to" . $name;
• ?>
PHP Functions
• settype() - Set the type of a variable
• gettype() - Get the type of a variable
• isset() — Determine if a variable is set and is not NULL
• is_array() - Finds whether a variable is an array
• is_bool() - Finds out whether a variable is a boolean
• is_float() - Finds whether the type of a variable is float
• is_int() - Find whether the type of a variable is integer
• is_null() - Finds whether a variable is NULL
• is_numeric() - Finds whether a variable is a number or a numeric string
• is_object() - Finds whether a variable is an object
• is_resource() - Finds whether a variable is a resource
• is_scalar() - Finds whether a variable is a scalar
• is_string() - Find whether the type of a variable is string
• empty() - Determine whether a variable is empty
• unset() - Unset a given variable
PHP Functions
• gettype — Get the type of a variable
• Returns the type of the PHP variable.
• <?php
$data = ”hello”;
?>
PHP Functions
• settype — Set the type of a variable
• Returns TRUE on success or FALSE on failure.
• <?php
$data = 10.55;
// convert the type of $data
echo gettype($data);
echo “<br>”;
echo $data;
settype($data,”integer”);
echo “<br>”;
echo gettype($data);
echo “<br>”;
echo $data;
?>
Comments in PHP
• As with most other scripting languages, PHP has a way to make
comments in your code.
• Comments can be used to create notes or to “hide” lines of
code without deleting them. Anything within a commented
section will not be processed by the server as PHP. There are
several ways to create comments in PHP, but the two most
common are:
• A double slash (//) or hash sign (#) will comment out
everything that follows it on that particular line.
• A slash and an asterisk (/*) will comment out everything in the
PHP across-multiple lines until it hits the closing comment tag
of an asterisk and a slash (*/).
Comments in PHP
• <?php
• // single line comment
• # single line comment
•
• /*
• multiple line
• comment
• */
• ?>
Null Coalesce Operator (??)
• <?php
• $name = "Hidaya";
• echo isset($name)?$name:"not set";
• echo "<br />";
• echo $name ?? "not set";
• ?>
Spaceship Operator (<=>)
• The spaceship operator is used for comparing
two expressions. It returns -1, 0 or 1 when $a is
respectively less than, equal to, or greater than
$b.
Spaceship Operator (<=>)
• // 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
// Strings
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1