Web Systems and Technologies PDF
Document Details
Uploaded by Deleted User
Laguna University
John Ren G. Santos,John Wilson O. Relova
Tags
Summary
This document is a module on Web Systems and Technologies, specifically focused on PHP programming. It covers fundamental concepts of PHP, including variables, operators, loops, arrays, strings, and functions. It further explores PHP form handling and cookies/sessions, presenting detailed instructions, examples, and summaries. The document is from Laguna University and aimed at undergraduate-level students.
Full Transcript
Web Systems and Technologies John Ren G. Santos John Wilson O. Relova V2.08082024 Table of Contents Module 5: PHP BASICS Introduction 79 Lesson 1. Variable Naming...
Web Systems and Technologies John Ren G. Santos John Wilson O. Relova V2.08082024 Table of Contents Module 5: PHP BASICS Introduction 79 Lesson 1. Variable Naming 80 Lesson 2. Operators Types 80 Lesson 3. Decision Making 94 Lesson 4. Loops 102 Lesson 5. Arrays 108 Lesson 5.1Numeric Arrays 109 Lesson 5.2Associative Arrays 110 Lesson 5.3Multidimensional Arrays 112 Lesson 6. Functions 113 Lesson 6.1Creating PHP Function 114 Lesson 6.2PHP Functions with Parameters 115 Lesson 6.3Passing Arguments by Reference 116 Lesson 6.4PHP Functions Returning Value 117 Lesson 6.5Setting Default Values for Function Parameters 118 Lesson 7. Strings 119 Lesson 7.1Single Quoted 119 Lesson 7.2Double Quoted 120 Lesson 7.3String Concatenation 121 Lesson 7.4String Functions 122 Assessment Task 125 Summary 127 Module 6: PHP FORM HANDLING Introduction 129 Lesson 1. Understanding HTML Forms 130 Lesson 1.1 GET Method 131 Lesson 1.2 POST Method 132 Lesson 1.3 Difference between GET and POST method 134 Lesson 2. Form Validations 135 Lesson 2.1Validation 138 Lesson 2.2Empty String 139 Lesson 2.3Validate String 139 Lesson 2.4 Validate Numbers 140 Lesson 2.5 Validate E-mail 140 Lesson 2.6 Validate URL 141 Lesson 2.7 Input Length 142 Assessment Task 148 Summary 148 Module 7: PHP COOKIES AND SESSIONS Introduction 149 Lesson 1. Cookies 150 Lesson 1.1 Anatomy of a Cookie 150 Lesson 1.2 Setting Cookies with PHP 151 Lesson 1.3 Creating / Retrieving Cookies 152 Lesson 1.4 Modifying a Cookie 153 Lesson 1.5 Deleting a Cookie 154 Lesson 2. Sessions 155 Lesson 2.1 Starting a Session 156 Lesson 2.2 Retrieving a Session 157 Lesson 2.3 Session Counter 158 Lesson 2.4 Destroying / Deleting a Session 159 Assessment Task 160 Summary 160 REFERENCES 161 List of Tables Table Description 5.2 Arithmetic Operators 81 5.3 Comparison Operators 83 5.4 Logical Operators 86 5.5 Assignment Operators 89 5.6 Increment / Decrement Operators 91 5.7 Conditional / Ternary Operator 93 6.8 Validation Rules 138 List of Figures Figure Description 5.27 Flowchart of IF Statements 95 5.28 Flowchart of IF..Else Statements 96 5.29 Flowchart of IF..Elseif..else Statements 98 5.30 Flowchart of Switch Statements 100 5.31 Flowchart of For Loop 103 5.32 Flowchart of While Loop 105 5.33 Flowchart of Do While Loop 106 7.34 Cookies 150 MODULE 5 PHP Basics Introduction In this module, we’re going to discuss the basics of PHP to get started in creating a dynamic web content. Just like in programming, creating a web content needs the logical structure on how it will be process and displayed in a browser. In order to do that, we’re going to discuss about the different variable types in PHP, operator types, decision and control structures, functions, arrays and its applications. Learning Outcomes At the end of this module, students should be able to: Identify the different types of variables in PHP. Explain the applications of different operators used in PHP. Apply the different control structures to create decisions in applications. Use functions to create a flexible and reusable code. Construct arrays to give control in data used in applications. Modify strings to format and control the texts that are displayed. -- Lesson 3. Variable Naming (geeksforgeeks , 2024) Based in the “PHP Tutorial” by geeksforgeeks ,(2024), a variable in PHP, you simply assign a value to it using the $ symbol followed by the variable name. PHP variables are case-sensitive and must start with a letter or an underscore, followed by any number of letters, numbers, or underscores Rules for PHP variables: A variable start with the $ sign, followed by the name of the variable A variable name must start with a letter or the underscore character A variable name cannot start with a number A variable name can only contain alpha-numeric characters and underscores (A-z, 0- 9, and _) Variable names are case-sensitive ($age and $AGE are two different variables) For more information, about PHP Variables and its uses, you can read the PHP Manual – Types and Variables by visiting this link: https://www.php.net/manual/en/language.types.php you can also watch this PHP Tutorial about Variable Types in YouTube by mmtuts: https://www.youtube.com/watch?v=ZPM8M9jzjjc&list=PL0eyrZgxdwhwBToawjm9faF1ixePe xft- &index=7. Lesson 4. Operator Types (geeksforgeeks, 2024) Based in the “PHP Tutorial” by geeksforgeeks ,(2024), simple answer can be given using expression 4 + 5 is equal to 9. Here 4 and 5 are called operands and + is called operator. PHP language supports following type of operators. Let’s have a look on all operators one by one. 1. Arithmetic Operators - The arithmetic operators are used to perform simple mathematical operations like addition, subtraction, multiplication, etc. Below is the list of arithmetic operators along with their syntax and operations in PHP Assume variable A holds 10 and variable B holds 20 then: 80 Table 5.2. Arithmetic Operators Operator Description Example + Adds two operands A + B will give 30 - Subtracts second operand from the first A - B will give -10 * Multiply both operands A * B will give 200 / Divide numerator by de-numerator B / A will give 2 % Modulus Operator and remainder of after an integer B % A will give 0 division ++ Increment operator, increases integer value by one A++ will give 11 -- Decrement operator, decreases integer value by A-- will give 9 one Try following example to understand all the arithmetic operators. Copy and paste following PHP program in “arithmeticOperator.php” file and keep it in your PHP Server's document root and browse it using any browser. Example: Arithmetical Operators 81 82 Output: Addition Operation Result: 62 Subtraction Operation Result: 22 Multiplication Operation Result: 840 Division Operation Result: 2.1 Modulus Operation Result: 2 Increment Operation Result: 42 Decrement Operation Result: 43 2. Comparison Operators - The PHP comparison operators are used to compare two values (number or string) (geeksforgeeks,2024): Assume variable A holds 10 and variable B holds 20 then: Table 5.3. Comparison Operators Operator Description Example == Checks if the value of two operands are equal or (A == B) is not not, if yes then condition becomes true. true. != Checks if the value of two operands are equal or (A != B) is true. not, if values are not equal then condition becomes true. > Checks if the value of left operand is greater than (A > B) is not true. the value of right operand, if yes then condition becomes true. < Checks if the value of left operand is less than the (A < B) is true. value of right operand, if yes then condition becomes true. >= Checks if the value of left operand is greater than (A >= B) is not or equal to the value of right operand, if yes then true. condition becomes true. 83 Output: TEST1: Either a or b is false TEST2: Either a or b is false TEST3: Either a or b is true TEST4: Either a or b is true TEST5: a is true TEST6: b is true TEST7: a is false TEST8: b is false 4. Assignment Operators - The PHP assignment operators are used with numeric values to write a value to a variable. The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right (geeksforgeeks,2024). Table 5.5. Assignment Operators Operator Description Example = Simple assignment operator, Assigns values from C = A + B will assign right side operands to left side operand value of A + B into C 89 += Add AND assignment operator, It adds right C += A is equivalent operand to the left operand and assign the result to to C = C + A left operand -= Subtract AND assignment operator, It subtracts right C -= A is equivalent to operand from the left operand and assign the result C=C-A to left operand *= Multiply AND assignment operator, It multiplies right C *= A is equivalent to operand with the left operand and assign the result C=C*A to left operand /= Divide AND assignment operator, It divides left C /= A is equivalent to operand with the right operand and assign the result C=C/A to left operand %= Modulus AND assignment operator, It takes C %= A is equivalent modulus using two operands and assign the result to C = C % A to left operand Try following example to understand all the assignment operators. Copy and paste following PHP program in “assignmentOperators.php” file and keep it in your PHP Server's document root and browse it using any browser. Example: Assignment Operators Output: Addition Operation Result: 62 Add AND Assignment Operation Result: 104 Subtract AND Assignment Operation Result: 62 Multiply AND Assignment Operation Result: 2604 Division AND Assignment Operation Result: 62 Modulus AND Assignment Operation Result: 20 5. Increment / Decrement Operators - increment/decrement operators are used to increase/decrease a variable's value. Table 5.6. Increment / Decrement Operators Operator Name Description ++$x Pre-increment Increments $x by one, then returns $x $x++ Post-increment Returns $x, then increments $x by one 91 --$x Pre-decrement Decrements $x by one, then returns $x $x-- Post-decrement Returns $x, then decrements $x by one The following example will show you these increment and decrement operators are used. Example: Increment / Decrement Operators 92 6. Conditional (or ternary) Operator - There is one more operator called conditional operator. This first evaluates an expression for a true or false value and then execute one of the two given statements depending upon the result of the evaluation (geeksforgeeks,2024). The conditional operator has this syntax: Table 5.7. Conditional/Ternary Operator Operator Description Example ?: Conditional If Condition is true? Then value X : Expression Otherwise value Y Operator Categories All the operators we have discussed above can be categorized into following categories: Unary prefix operators, which precede a single operand. Binary operators, which take two operands and perform a variety of arithmetic and logical operations. The conditional operator (a ternary operator), which takes three operands and evaluates either the second or third expression, depending on the evaluation of the first expression. Assignment operators, which assign a value to a variable. For more information, about PHP Operators, you can read the PHP Manual – Operators by visiting this link: https://www.php.net/manual/en/language.operators.php ,you can also watch this PHP Tutorial Series 8 - 12 about PHP Operators in YouTube by mmtuts: https://www.youtube.com/watch?v=- nQHIE0LIDE&list=PL0eyrZgxdwhwBToawjm9faF1ixePexft-&index=8. 93 Lesson 5. Decision Making (geeksforgeeks,2024) Based in the “PHP Tutorial” by geeksforgeeks (2024), PHP allows us to perform actions based on some type of conditions that may be logical or comparative. Based on the result of these conditions i.e., either TRUE or FALSE, an action would be performed as asked by the user. It’s just like a two- way path. If you want something, then go this way or else turn that way. To use this feature, PHP provides us with four conditional statements (geeksforgeeks,2024): if statement if…else statement if…elseif…else statement switch statement Let us now look at each one of these in details: 1. If statement - This statement allows us to set a condition. On being TRUE, the following block of code enclosed within the if clause will be executed. Syntax: if (condition){ // if TRUE then execute this code } 94 Flowchart: Figure 5.27. Flowchart of if statements (PHP Tutorial by geeksforgeeks,2024) Example: Output: The number is positive. 2. If…else statement - We understood that if a condition will hold i.e., TRUE, then the block of code within if will be executed. But what if the condition is not TRUE and we want to perform an action? This is where else comes into play. If a condition is TRUE then if block gets executed, otherwise else block gets executed. 95 Syntax: if (condition) { // if TRUE then execute this code } Else { // if FALSE then execute this code } Flowchart: Figure 5.28. Flowchart of If.Else statements (PHP Tutorial by geeksforgeeks,2024) Example: Output: The number is negative 3. If…elseif…else statement - This allows us to use multiple if…else statements. We use this when there are multiple conditions of TRUE cases. Syntax: if (condition) { // if TRUE then execute this code } elseif { // if TRUE then execute this code } elseif { // if TRUE then execute this code } else { // if FALSE then execute this code } 97 Flowchart: Figure 5.29. Flowchart of if.elseif.else statement (PHP Tutorial by geeksforgeeks,2024) Example: 98 Output: Happy Independence Day!!! 4. Switch statement - The “switch” performs in various cases i.e., it has various cases to which it matches the condition and appropriately executes a particular case block. It first evaluates an expression and then compares with the values of each case. If a case matches, then the same case is executed. To use switch, we need to get familiar with two different keywords namely, break and default. Break - The break statement is used to stop the automatic control flow into the next cases and exit from the switch case. Default - The default statement contains the code that would execute if none of the cases match. Syntax: switch(n) { case statement1: code to be executed if n==statement1; break; case statement2: code to be executed if n==statement2; break; case statement3: code to be executed if n==statement3; break; case statement4: code to be executed if n==statement4; break; default: code to be executed if n != any case; 99 Flowchart: Figure 5.30. Flowchart of switch statements (PHP Tutorial by geeksforgeeks,2024) Example: