Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 32 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
32
Dung lượng
219 KB
Nội dung
Using Variables and Expressions in PHP Session 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 tags Elements placed between the tags, automatically becomes a part of the form PHP / Session / Slide 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 / Slide 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 / Slide 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 / Slide of 32 Identifiers - II Valid identifiers are: Firstnum Fnum Firstnum First18num FirstNum Invalid identifiers are: first-num 1num first num first&num PHP / Session / Slide 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 / Slide 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 / Slide 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 / Slide 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 / Slide 10 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 / 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 / Slide 19 of 32 Example for local variables - I For example, to display product of two numbers PHP / Session / 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 / 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 / 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 / Slide 23 of 32 Example for global variable - I For example, to perform multiplication of two numbers PHP / Session / 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 / Slide 25 of 32 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 function var_name – Specifies the variable name PHP / Session / Slide 26 of 32 Example for static variable - I For example, to use the static variable in a function PHP / Session / Slide 27 of 32 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 PHP / Session / Slide 28 of 32 HTTP Environment Variables - I Variables those are system-defined variables and are used in any PHP script Provides information about the transactions held between the client and the server Provides information about the HTTP request or the HTTP response Similar to the user-defined variables because they also begin with the dollar ($) sign PHP / Session / Slide 29 of 32 HTTP Environment Variables - II Some of the environment variables are: SERVER_SOFTWARE SERVER_NAME SERVER_PROTOCOL SERVER_PORT COOKIE_DATA POST_FILES HTTP_USER_AGENT HTTP_ACCEPT HTTP_FROM PHP / Session / Slide 30 of 32 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 The lifetime of a variable is the duration from the time the variable is created to the time the execution of the scripts ends PHP / Session / Slide 31 of 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 and response those are held in between the server and the client PHP / Session / Slide 32 of 32 [...]... 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... 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... 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 Static Variables Retains its... declare the constant, NAME, containing a string value < ?php 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... 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... 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 < ?php echo “The multiplication... ?> PHP / Session 3 / Slide 27 of 32 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 PHP / Session 3 / Slide 28 of 32 HTTP Environment Variables - I Variables those are system-defined variables and are used in any PHP. ..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... 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,... variables and are used in any PHP script Provides information about the transactions held between the client and the server Provides information about the HTTP request or the HTTP response Similar to the user-defined variables because they also begin with the dollar ($) sign PHP / Session 3 / Slide 29 of 32 HTTP Environment Variables - II Some of the environment variables are: SERVER_SOFTWARE