Declaring a variable with a data type Declares automatically at the time of initializing the variable Declares with the data type of which the value will stored in it As the value
Trang 1Using Variables and Expressions
in PHP
Session 3
Trang 2<FORM> tags
Trang 3Review - II
values the user placed on the form to the URL
user placed on the form to the PHP script engine in the
body of the HTTP request
information, from a form to a script, or from one form to
Trang 4 Define Identifiers
Discuss about the data types
Use Variables and Constants
Use the scope and lifetime of variables
Use the HTTP environment variables
Trang 5Identifiers - I
Names given to various elements of a program such as variables, constants, arrays, and classes in a program are identifiers
Rules for defining identifiers:
Begins with a letter
Contains only letters(A to Z) or digits(0-9)
May use underscore(_) to add space in the identifier
Trang 7 An identifier whose value keeps changing
Contains a name and a data type
Defines the type of data it holds
Stores user information, intermediate data such as
calculated results, and values returned by the functions
Stores data value in it
Trang 8Data Type
The following data types are:
Integer - Stores numbers without decimal points The value ranges from -2,147,483,648 to +2,147,483,647
Floating-point - Stores floating-point numbers
String - Stores a set of characters It is enclosed within single quotes or double quotes
Boolean - Stores one of the two values, True or False
Objects - Uses for any object reference
Trang 9Declaring a variable with a data
type
Declares automatically at the time of initializing the variable
Declares with the data type of which the value will stored in it
As the value of the variable keeps on changing, the data type
of the variable also keeps on changing
Starts with a dollar ($) symbol and then the variable name
Variables are case sensitive
For example, a variable name, $var_name, is different from
a variable name, $Var_Name
Trang 11Assigning value to a Variable
Means performing an assignment operation that is
placing an equal to (=) sign in between the
variable and the value
Also assigns values of an expressions to it
Trang 12Example for assigning integer to
a variable
For example, to store an integer value in the
variable
$Salary = 5000;
Here, the $Salary variable will be declared as
the numeric variable because the value assigned to
it is of the integer data type.
Trang 13Example for assigning string to a variable
For example, to store string value in the variable
$message = “HELLO! How are you?”
Here, the $message variable will be declared as the string variable because the value assigned to it is of the string data type The string is enclosed within the double quotes
Trang 14Example for assigning an
Here, the $number3 variable will be declared as an
integer variable and the value of the addition expression is
Trang 15executes Constants are identifiers
Trang 17Example for Constant
For example, to declare the constant, NAME, containing a string value
Trang 18Scope of Variables
Indicates the lifetime of a variable It is the portion in the
script within which the variable is defined
The lifetime of a variable is the duration from the time the variable is created to the time the execution of the scripts
ends
The different scopes of variables are:
Local
Trang 19Local Variables
Initialized and used inside a function
The lifetime of a local variable begins when the
function is called and ends when the function is
executed
Trang 20Example for local variables - I
For example, to display product of two numbers
Trang 21Example for local variables - II
Here, the variables, num1 and num2 are declared inside
the function as the local variables of the function
They are initialized and used inside the multiply()
function The product of two variables, num1 and num2
is calculated in the multiply() function
This function executes when the PHP script calls the
function using the function name
Trang 22Global Variables
Retains its value throughout the lifetime of the web
page
Declares with the help of the global keyword
within the function
Accessible from any part of the program
Trang 23Declaring a global variable
Syntax to declare a global variable in a program is:
global $var_name;
Where,
global – Makes a variable global It is a keyword for
making a variable global
$var_name – Specifies the variable name that needs
to be made global
Trang 24Example for global variable - I
For example, to perform multiplication of two numbers
global $var1, $var2;
$var2 = $var1 * $var2;
echo $var2;
}
Trang 25Example for global variable - II
Here, the variables, var1 and var2 are declared as the
global variables
They are initialized outside the function and declared as
global within the multiply() function The product
of two variables, var1 and var2 is calculated in the
multiply() function
This function executes when the PHP script calls the
Trang 26Static Variables
Retains its value even after the function terminates
Uses the STATIC keyword with the variable name
Uses in recursive functions
Syntax to declare a static variable is:
static $var_name = value;
Where,
static – Makes the variable and its value static to the
Trang 27Example for static variable - I
For example, to use the static variable in a function
Trang 28Example for static variable - II
Here, the var1 is declared as the static variable
It is declared inside the sum()function
The variable var1 is local to the sum()function
but it retains its value throughout the program
Trang 29HTTP Environment Variables - I
used in any PHP script
the client and the server
HTTP response
begin with the dollar ($) sign
Trang 31Summary - I
An identifier is use to refer to the memory location with the help
of any variable
When any numeric data or any text data is enclosed in the
quotes, then that value is considered to be a string data type
In a Boolean data type, when a condition is being tested, it
returns only true or false values
A variable is a name or an identifier assigned to a memory
location where the data is stored A programmer uses variables
to retrieve data from the memory location
Trang 32Summary - II
A local variable is declared and used inside a function It
remains till the function terminates
A global variable uses its value throughout the page It is
declared outside the function It is called in the program
with the help of the keyword global
A static variable is a local variable that is made static with
the help of the keyword static It retains its value even
after the function terminates
An environment variable deals with the action of request