Chapter 2 PDF
Chapter 2 PDF
Chapter 2 PDF
Chapter 2
1
2/17/2022
2
2/17/2022
3
2/17/2022
4
2/17/2022
Variables
The main way to store information in the middle of a PHP program is by using a variable.
Here are the most important things to know about variables in PHP.
• All variables in PHP are denoted with a leading dollar sign ($).
• The value of a variable is the value of its most recent assignment.
• Variables are assigned with the = operator, with the variable on the left-hand side and
the expression to be evaluated on the right.
• Variables can, but do not need, to be declared before assignment.
• Variables in PHP do not have intrinsic types - a variable does not know in advance
whether it will be used to store a number or a string of characters.
• Variables used before they are assigned have default values.
• PHP does a good job of automatically converting types from one to another when
necessary.
5
2/17/2022
PHP has a total of eight data types which we use to construct our variables:
Integers
They are whole numbers, without a decimal point, like 4195. They are
the simplest type .they correspond to simple whole numbers, both
positive and negative. Integers can be assigned to variables, or they can
be used in expressions, like so:
6
2/17/2022
Doubles
They like 3.14159 or 49.1. By default, doubles print with the minimum
number of decimal places needed. For example, the code:
Boolean
They have only two possible values either true or false. PHP provides a
couple of constants especially for use as Booleans: TRUE and FALSE,
which can be used like so:
7
2/17/2022
Strings
Variable Naming
8
2/17/2022
PHP Variables
Scope can be defined as the range of availability a variable has to the
program in which it is declared. PHP variables can be one of four scope
types:
• Local variables
• Global variables
• Static variables
9
2/17/2022
10
2/17/2022
Constants
A constant is a name or an identifier for a simple value. A constant value
cannot change during the execution of the script. By default, a constant is
case-sensitive. By convention, constant identifiers are always uppercase. A
constant name starts with a letter or underscore, followed by any number of
letters, numbers, or underscores. If you have defined a constant, it can never
be changed or undefined.
To define a constant you have to use define() function and to retrieve the
value of a constant, you have to simply specifying its name. Unlike with
variables, you do not need to have a constant with a $. You can also use the
function constant() to read a constant's value if you wish to obtain the
constant's name dynamically.
11
2/17/2022
Constants Function
As indicated by the name, this function will return the value of the
constant.
This is useful when you want to retrieve value of a constant, but you do
not know its name, i.e., it is stored in a variable or returned by a
function.
12
2/17/2022
13
2/17/2022
• Arithmetic Operators
• Comparison Operators
• Logical (or Relational) Operators
• Assignment Operators
• Conditional (or ternary) Operators
Arithmetic Operators
The following arithmetic operators are supported by PHP language:
14
2/17/2022
Example
Comparison Operators
A comparison operator allows you to compare two values
15
2/17/2022
Logical Operators
The logical OR operator returns false if both operands are false.
Logical Operators
The logical AND operator accepts two operands and returns true if both operands are true;
otherwise, it returns false.
16
2/17/2022
Assignment Operators
PHP uses the = to represent the assignment operator. The following
shows the syntax of the assignment operator:
Conditional/Ternary Operators
The ternary operator is a shorthand for the if...else statement. Instead
of writing this:
17