Tải bản đầy đủ (.ppt) (32 trang)

Slide môn học PHP session 2a using variables and expressions in PHP

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (373.95 KB, 32 trang )

Using Variables and Expressions
in PHP
Session 3


Review - I









PHP has a built-in support for collecting data from a
HTML form
Information from the form is automatically available to the
script engine
Server recognizes the form as a collection of codes
In order to create a Form, we use the opening and closing
<FORM> tags
Elements placed between the<FORM> tags, automatically
becomes a part of the form
PHP / Session 3 / Slide 2 of 32


Review - II









The GET method specifies the browser to send the
values the user placed on the form to the URL
POST method informs the browser to send the values the
user placed on the form to the PHP script engine in the
body of the HTTP request
Hidden form fields enables form developers to pass
information, from a form to a script, or from one form to
another, before being passed to a script
Hidden form fields are not visible to users
PHP / Session 3 / Slide 3 of 32


Objectives






Define Identifiers
Discuss about the data types
Use Variables and Constants
Use the scope and lifetime of variables
Use the HTTP environment variables


PHP / Session 3 / Slide 4 of 32


Identifiers - 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
Never include any special characters including blank space
PHP / Session 3 / Slide 5 of 32


Identifiers - II


Valid identifiers are:









Firstnum
Fnum
Firstnum
First18num
FirstNum

Invalid identifiers are:





first-num
1num
first num
first&num
PHP / Session 3 / Slide 6 of 32


Variables








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

PHP / Session 3 / Slide 7 of 32


Data 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

PHP / Session 3 / Slide 8 of 32


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 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

PHP / Session 3 / Slide 9 of 32


Declaring a Variable
Syntax for initializing a variable is:

$variable_name = value;
Where,







$variable_name – Specifies the name of the
variable
value – Specifies the value for the variable

PHP / Session 3 / Slide 10 of 32


Assigning 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

PHP / Session 3 / Slide 11 of 32


Example 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.

PHP / Session 3 / Slide 12 of 32


Example 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.

PHP / Session 3 / Slide 13 of 32


Example for assigning an
expression to a variable



For example, to store an expression in the variable
$number1 = 1019;
$number2 = 126;
$number3 = $number1 + $number2;
echo $number3;
?>

Here, the $number3 variable will be declared as an
integer variable and the value of the addition expression is
assigned to the variable.
PHP / Session 3 / Slide 14 of 32


Constants






Contains values that do not change as the program
executes. Constants are identifiers
Constants are case-sensitive
Gets declared using the define() function
Has a global scope of existence

PHP / Session 3 / Slide 15 of 32



Declaring a Constant


Syntax to declare a constant using the define( )
function is:
define(string_name, mixed_value)
Where,




string_name – Specifies the variable name for the
constant
mixed_value – Specifies a numeric or string value that
is to be made constant

PHP / Session 3 / Slide 16 of 32


Example for Constant


For example, to declare the constant, NAME, containing a
string value
define(“NAME”, “John Smith”);
echo NAME;
?>
Here, a constant is declared. On executing the code, the string

value stored in the constant will be displayed
PHP / Session 3 / Slide 17 of 32


Scope 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
Global
Static
PHP / Session 3 / Slide 18 of 32


Local 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

PHP / Session 3 / Slide 19 of 32


Example for local variables - I


For example, to display product of two numbers
echo “The multiplication value of 14 * 15 = “;
function multiply()
{
$num1=14;
$num2=15;
$num2=$num1 * $num2;
echo $num2;
}
multiply();
?>
PHP / Session 3 / Slide 20 of 32


Example 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

PHP / Session 3 / Slide 21 of 32


Global 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

PHP / Session 3 / Slide 22 of 32



Declaring 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

PHP / Session 3 / Slide 23 of 32


Example for global variable - I


For example, to perform multiplication of two numbers
$var1 = 4;
$var2 = 15;
function multiply()
{
global $var1, $var2;
$var2 = $var1 * $var2;

echo $var2;
}
echo “The multiplication value of 4 * 15 =”;
multiply();
?>
PHP / Session 3 / Slide 24 of 32


Example 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
function using the function name

PHP / Session 3 / Slide 25 of 32


×