Lecture E-Commerce - Chapter 30: PHP (part II)
ppt
75
1 MB
0
1
4.3 (
6 lượt)
Nhấn vào bên dưới để tải tài liệu
Để tải xuống xem đầy đủ hãy nhấn vào bên trên
Chủ đề liên quan
Lecture E-commerce
Thương mại điện tử
Business technology
Social networking
Social e-commerce
The PHP
Nội dung
"; var_dump($x != $y); echo "
"; var_dump($x !== $y); echo "
"; $a=50; $b=90; var_dump($a > $b); echo "
"; var_dump($a < $b); ?> T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 1-4 PHP Logical Operators Operator Name Example Result and And $x and $y True if both $x and $y are true or Or $x or $y True if either $x or $y is true xor Xor $x xor $y True if either $x or $y is true, but not both && || ! And Or Not $x && $y True if both $x and $y are true $x || $y True if either $x or $y is true !$x True if $x is not true T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 1-5 PHP Array Operators The PHP array operators are used to compare arrays: Operator Name Example + Union $x + $y == Equality $x == $y === Identity $x === $y != <> !== T2-Lecture-9 Inequality $x != $y Inequality $x <> $y Non-identity $x !== $y Ahmed Mumtaz Mustehsan Result Union of $x and $y (but duplicate keys are not overwritten) True if $x and $y have the same key/value pairs True if $x and $y have the same key/value pairs in the same order and of the same types True if $x is not equal to $y True if $x is not equal to $y True if $x is not identical to $y www.w3schools.com 1-6 Example The example below shows the different results of using the different array operators: "red", "b" => "green"); $y = array("c" => "blue", "d" => "yellow"); $z = $x + $y; // union of $x and $y var_dump($z); var_dump($x == $y); var_dump($x === $y); var_dump($x != $y); var_dump($x <> $y); var_dump($x !== $y); ?> T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 1-7 PHP Conditional Statements Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. In PHP we have the following conditional statements: if statement - executes some code only if a specified condition is true if...else statement - executes some code if a condition is true and another code if the condition is false if...elseif....else statement - selects one of several blocks of code to be executed switch statement - selects one of many blocks of code to be executed T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 1-8 PHP - The if Statement The if statement is used to execute some code only if a specified condition is true. Syntax if (condition) { code to be executed if condition is true; } The example below will output "Have a good day!" if the current time (HOUR) is less than 20: T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 1-9 Example T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 110 PHP - The if...else Statement Use the if....else statement to execute some code if a condition is true and another code if the condition is false. Syntax if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; } The example below will output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise: T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 111 Example T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 112 PHP - The if...elseif....else Statement Use the if....elseif...else statement to select one of several blocks of code to be executed. Syntax if (condition) { code to be executed if condition is true; } elseif (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; } The example below will output "Have a good morning!" if the current time is less than 10, and "Have a good day!" if the current time is less than 20. Otherwise it will output "Have a good night!": T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 113 Example T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 114 The PHP switch Statement Use the switch statement to select one of many blocks of code to be executed. Syntax switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; case label3: code to be executed if n=label3; break; ... default: code to be executed if n is different from all labels; } T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 115 The PHP switch Statement This is how it works: First we have a single expression n (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically. Thedefault statement is used if no match is found. T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 116 Example T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 117 PHP Loops Often when you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal code-lines in a script, we can use loops to perform a task like this. In PHP, we have the following looping statements: while - loops through a block of code as long as the specified condition is true do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true for - loops through a block of code a specified number of times foreach - loops through a block of code for each element in an array T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 118 The PHP while Loop The while loop executes a block of code as long as the specified condition is true. Syntax while (condition is true) { code to be executed; } The example below first sets a variable $x to 1 ($x=1;). Then, the while loop will continue to run as long as $x is less than, or equal to 5. $x will increase by 1 each time the loop runs ($x++;): T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 119 Example "; $x++; } ?> T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 120 The PHP do...while Loop The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true. Syntax do { code to be executed; } while (condition is true); The example below first sets a variable $x to 1 ($x=1;). Then, the do while loop will write some output, and then increment the variable $x with 1. Then the condition is checked (is $x less than, or equal to 5?), and the loop will continue to run as long as $x is less than, or equal to 5: T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 121 Example "; $x++; } while ($x<=5); ?> T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 122 The PHP do...while Loop Notice that in a do while loop the condition is tested AFTER executing the statements within the loop. This means that the do while loop would execute its statements at least once, even if the condition fails the first time. The example below sets the $x variable to 6, then it runs the loop, and then the condition is checked: Example "; $x++; } while ($x<=5); ?> T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 123 The PHP for Loop The for loop is used when you know in advance how many times the script should run. Syntax for (init counter; test counter; increment counter) { code to be executed; } Parameters: init counter: Initialize the loop counter value test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends. increment counter: Increases the loop counter value T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 124 Example The example below displays the numbers from 0 to 10: "; } ?> T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 125 The PHP foreach Loop The foreach loop works only on arrays, and is used to loop through each key/value pair in an array. Syntax foreach ($array as $value) { code to be executed; } For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element. The following example demonstrates a loop that will output the values of the given array ($colors): T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 126 Example "; } ?> T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 127 PHP User Defined Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used repeatedly in a program. A function will not execute immediately when a page loads. A function will be executed by a call to the function. T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 128 Create a User Defined Function in PHP A user defined function declaration starts with the word "function": Syntax function functionName() { code to be executed; } Note: A function name can start with a letter or underscore (not a number). T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 129 Example In the example below, we create a function named "writeMsg()". The opening curly brace ( { ) indicates the beginning of the function code and the closing curly brace ( } ) indicates the end of the function. The function outputs "Hello world!". To call the function, just write its name: T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 130 PHP Function Arguments Information can be passed to functions through arguments. An argument is just like a variable. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just seperate them with a comma. The following example has a function with one argument ($fname). When the familyName() function is called, we also pass along a name (e.g. Jani), and the name is used inside the function, which outputs several different first names, but an equal last name: T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 131 Example "; } familyName("Jani"); familyName("Hege"); familyName("Stale"); familyName("Kai Jim"); familyName("Borge"); ?> T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 132 Example The following example has a function with two arguments ($fname and $year): "; } familyName("Hege","1975"); familyName("Stale","1978"); familyName("Kai Jim","1983"); ?> T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 133 Example PHP Default Argument Value The following example shows how to use a default parameter. If we call the function setHeight() without arguments it takes the default value as argument: "; } setHeight(350); setHeight(); // will use the default value of 50 setHeight(135); setHeight(80); ?> T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 134 PHP Functions - Returning values To let a function return a value, use the return statement: Example: "; echo "7 + 13 = " . sum(7,13) . "
"; echo "2 + 4 = " . sum(2,4); ?> T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 135 What is an Array? An array is a special variable, which can hold more than one value at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this: $cars1="Volvo"; $cars2="BMW"; $cars3="Toyota"; T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 136 What is an Array? However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300? The solution is to create an array! An array can hold many values under a single name, and you can access the values by referring to an index number. T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 137 Create an Array in PHP In PHP, the array() function is used to create an array: array(); In PHP, there are three types of arrays: Indexed arrays - Arrays with numeric index Associative arrays - Arrays with named keys Multidimensional arrays - Arrays containing one or more arrays T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 138 PHP Indexed Arrays There are two ways to create indexed arrays: The index can be assigned automatically (index always starts at 0): $cars=array("Volvo","BMW","Toyota"); or the index can be assigned manually: $cars[0]="Volvo"; $cars[1]="BMW"; $cars[2]="Toyota"; T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 139 Example The following example creates an indexed array named $cars, assigns three elements to it, and then prints a text containing the array values: Example T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 140 Get The Length of an Array - The count() Function The count() function is used to return the length (the number of elements) of an array: Example T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 141 Loop Through an Indexed Array To loop through and print all the values of an indexed array, you could use a for loop, like this: Example "; } ?> T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 142 PHP Associative Arrays Associative arrays are arrays that use named keys that you assign to them. There are two ways to create an associative array: $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); or: $age['Peter']="35"; $age['Ben']="37"; $age['Joe']="43"; T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 143 PHP Associative Arrays The named keys can then be used in a script: Example "35","Ben"=>"37","Joe"=>"43"); echo "Peter is " . $age['Peter'] . " years old."; ?> T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 144 Loop Through an Associative Array To loop through and print all the values of an associative array, you could use a foreach loop, like this: Example "35","Ben"=>"37","Joe"=>"43"); foreach($age as $x=>$x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "
"; } ?> T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 145 PHP - Sort Functions For Arrays In this chapter, we will go through the following PHP array sort functions: sort() - sort arrays in ascending order rsort() - sort arrays in descending order asort() - sort associative arrays in ascending order, according to the value ksort() - sort associative arrays in ascending order, according to the key arsort() - sort associative arrays in descending order, according to the value krsort() - sort associative arrays in descending order, according to the key T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 146 Sort Array in Ascending Order - sort() The following example sorts the elements of the $cars array in ascending alphabetical order: Example T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 147 Example The following example sorts the elements of the $numbers array in ascending numerical order: Example T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 148 Sort Array in Descending Order - rsort() The following example sorts the elements of the $cars array in descending alphabetical order: Example T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 149 Example The following example sorts the elements of the $numbers array in descending numerical order: T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 150 Sort Array in Ascending Order, According to Value - asort() The following example sorts an associative array in ascending order, according to the value: Example "35","Ben"=>"37","Joe"=>"43"); asort($age); ?> T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 151 Sort Array in Ascending Order, According to Key - ksort() The following example sorts an associative array in ascending order, according to the key: Example "35","Ben"=>"37","Joe"=>"43"); ksort($age); ?> T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 152 Sort Array in Descending Order, According to Value - arsort() The following example sorts an associative array in descending order, according to the value: Example "35","Ben"=>"37","Joe"=>"43"); arsort($age); ?> T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 153 Sort Array in Descending Order, According to Key - krsort() The following example sorts an associative array in descending order, according to the key: Example "35","Ben"=>"37","Joe"=>"43"); krsort($age); ?> T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 154 PHP Global Variables - Superglobals Several predefined variables in PHP are "superglobals", which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special. The PHP superglobal variables are: • $GLOBALS • $_SERVER • $_REQUEST • $_POST • $_GET • $_FILES • $_ENV • $_COOKIE • $_SESSION T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 155 PHP $GLOBALS $GLOBALS is a PHP super global variable which is used to access global variables from anywhere in the PHP script (also from within functions or methods). PHP stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable. The example below shows how to use the super global variable $GLOBALS: T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 156 Example In the example above, since z is a variable present within the $GLOBALS array, it is also accessible from outside the function! T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 157 PHP $_SERVER $_SERVER is a PHP super global variable which holds information about headers, paths, and script locations. The example below shows how to use some of the elements in $_SERVER: T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 158 Example "; echo $_SERVER['SERVER_NAME']; echo "
"; echo $_SERVER['HTTP_HOST']; echo "
"; echo $_SERVER['HTTP_REFERER']; echo "
"; echo $_SERVER['HTTP_USER_AGENT']; echo "
"; echo $_SERVER['SCRIPT_NAME']; ?> T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 159 PHP $_SERVER The following table lists the most important elements that can go inside $_SERVER: Element/Code $_SERVER['PHP_SELF'] Description Returns the filename of the currently executing script $_SERVER['GATEWAY_IN TERFACE'] Returns the version of the Common Gateway Interface (CGI) the server is using $_SERVER['SERVER_ADD Returns the IP address of the host server R'] $_SERVER['SERVER_NAM Returns the name of the host server (such as E'] www.w3schools.com) $_SERVER['SERVER_SOF TWARE'] Returns the server identification string (such as Apache/2.2.24) $_SERVER['SERVER_PRO TOCOL'] Returns the name and revision of the information protocol (such as HTTP/1.1) $_SERVER['REQUEST_ME Returns the request method used to access the page THOD'] (such as POST) $_SERVER['REQUEST_TIM Returns the timestamp of the start of the request E'] (such as 1377687496) $_SERVER['QUERY_STRIN Returns the query string if the page is accessed via a G'] query string $_SERVER['HTTP_ACCEP T'] Returns the Accept header from the current request $_SERVER['HTTP_ACCEP T_CHARSET'] Returns the Accept_Charset header from the current request (such as utf-8,ISO-8859-1) $_SERVER['HTTP_HOST'] Returns the Host header from the current request T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 160 Example T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 161 PHP $_REQUEST PHP $_REQUEST is used to collect data after submitting an HTML form. The example above shows a form with an input field and a submit button. When a user submits the data by clicking on "Submit", the form data is sent to the file specified in the action attribute of the T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 163 PHP $_POST PHP $_POST is widely used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables. The example above shows a form with an input field and a submit button. When a user submits the data by clicking on "Submit", the form data is sent to the file specified in the action attribute of the T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 167 PHP – HTTP POST When the user fills out the form above and clicks the submit button, the form data is sent for processing to a PHP file named "welcome.php". The form data is sent with the HTTP POST method. To display the submitted data you could simply echo all the variables. The "welcome.php" looks like this: T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 168 PHP – HTTP POST Welcome
Your email address is: The output could be something like this: ◦ Welcome John Your email address is john.doe@example.com T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 169 PHP – HTTP GET The same result could also be achieved using the HTTP GET method: Example T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 170 PHP – HTTP GET and "welcome_get.php" looks like this: Welcome
Your email address is: The code above is quite simple. However, the most important thing is missing. You need to validate form data to protect your script from malicious code. T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 171 GET vs. POST Both GET and POST create an array (e.g. array( key => value, key2 => value2, key3 => value3, ...)). This array holds key/value pairs, where keys are the names of the form controls and values are the input data from the user. Both GET and POST are treated as $_GET and $_POST. These are superglobals, which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special. $_GET is an array of variables passed to the current script via the URL parameters. $_POST is an array of variables passed to the current script via the HTTP POST method. T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 172 When to use GET? Information sent from a form with the GET method is visible to everyone (all variable names and values are displayed in the URL). GET also has limits on the amount of information to send. The limitation is about 2000 characters. However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases. GET may be used for sending non-sensitive data. Note: GET should NEVER be used for sending passwords or other sensitive information! T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 173 When to use POST? Information sent from a form with the POST method is invisible to others (all names/values are embedded within the body of the HTTP request) and has no limits on the amount of information to send. Moreover POST supports advanced functionality such as support for multi-part binary input while uploading files to server. However, because the variables are not displayed in the URL, it is not possible to bookmark the page. T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 174 The End PHP Part-II Thank You T2-Lecture-9 Mustehsan Ahmed Mumtaz www.w3schools.com 175
This site is protected by reCAPTCHA and the Google
Privacy Policy
and
Terms of Service
apply.