1. Trang chủ
  2. » Mẫu Slide

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

32 239 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 32
Dung lượng 219 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 1

Using Variables and Expressions

in PHP

Session 3

Trang 2

<FORM> tags

Trang 3

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

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

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 8

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

Trang 9

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

Trang 11

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

Trang 12

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.

Trang 13

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

Trang 14

Example for assigning an

Here, the $number3 variable will be declared as an

integer variable and the value of the addition expression is

Trang 15

executes Constants are identifiers

Trang 17

Example for Constant

 For example, to declare the constant, NAME, containing a string value

Trang 18

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

Trang 19

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

Trang 20

Example for local variables - I

 For example, to display product of two numbers

Trang 21

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

Trang 22

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

Trang 23

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

Trang 24

Example for global variable - I

 For example, to perform multiplication of two numbers

global $var1, $var2;

$var2 = $var1 * $var2;

echo $var2;

}

Trang 25

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

Trang 26

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

Example for static variable - I

 For example, to use the static variable in a function

Trang 28

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

HTTP Environment Variables - I

used in any PHP script

the client and the server

HTTP response

begin with the dollar ($) sign

Trang 31

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

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

Ngày đăng: 30/11/2016, 22:11

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN