Lecture E-Commerce - Chapter 30: PHP (part II)

75 31 0
Lecture E-Commerce - Chapter 30: PHP (part II)

Đ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

After studying this chapter you will be able to understand: PHP comparison operators, PHP conditional statements, PHP - The if...else statement, the PHP switch statement, the PHP while loop, the PHP foreach loop,...

CSC 330 E-Commerce Teacher Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad Virtual Campus, CIIT COMSATS Institute of Information Technology T2-Lecture-10 PHP Part-II T2-Lecture-10 For Lecture Material/Slides Thanks to: www.w3schools.com PHP Comparison Operators  The PHP comparison operators are used to compare two values (number or string): Operator Name Example Result == Equal $x == $y True if $x is equal to $y === Identical $x === $y True if $x is equal to $y, and they are of the same type != Not equal $x != $y True if $x is not equal to $y   Not equal $x $y True if $x is not equal to $y !== Not identical $x !== $y True if $x is not equal to $y, or they are not of the same type >  Greater than $x > $y True if $x is greater than $y Ahmed Mumtaz Mustehsan www.w3schools.com T2-Lecture-9 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 $x && $y True if both $x and $y are true || Or $x || $y ! Not !$x T2-Lecture-9 Ahmed Mumtaz Mustehsan True if either $x or $y is true True if $x is not true 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 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 === Identity $x === $y key/value pairs in the same order and of the same types != Inequality $x != $y True if $x is not equal to $y Inequality $x $y True if $x is not equal to $y !== T2-Lecture-9 Non-identity $x Ahmed Mumtaz Mustehsan !==www.w3schools.com $y True if $x is not identical to $y 1-6 Example  The example below shows the different results of using the different array operators:  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 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 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 tag In this example, we point to this file itself for processing form data If you wish to use another PHP file to process form data, replace that with the filename of your choice Then, we can use the super global variable $_REQUEST to collect the value of the input field: T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 162 Example  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 tag In this example, we point to this file itself for processing form data  Then, we can use the super global variable $_POST to collect the value of the input field: T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 164 PHP $_GET  PHP $_GET can also be used to collect form data after submitting an HTML form with method="get"  $_GET can also collect data sent in the URL  Assume we have an HTML page that contains a hyperlink with parameters:  Test $GET T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 165 PHP $_GET  When a user clicks on the link "Test $GET", the parameters "subject" and "web" is sent to "test_get.php", and you can then acces their values in "test_get.php" with $_GET  The example below shows the code in "test_get.php": Example  T2-Lecture-9 Ahmed Mumtaz Mustehsan www.w3schools.com 166 PHP – HTTP POST  The example below displays a simple HTML form with two input fields and a submit button:  Example  Name: E-mail: 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  Name: E-mail: 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: T2 -Lecture- 9 Ahmed Mumtaz Mustehsan www.w3schools.com 110 PHP. .. 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 -. . .PHP Part-II T2 -Lecture- 10 For Lecture Material/Slides Thanks to: www.w3schools.com PHP Comparison Operators  The PHP comparison operators are used to compare

Ngày đăng: 18/01/2020, 18:04

Mục lục

  • Slide 1

  • PHP Part-II T2-Lecture-10

  • PHP Comparison Operators

  • Example of Comparison Operators

  • PHP Logical Operators

  • PHP Array Operators

  • Example

  • PHP Conditional Statements

  • PHP - The if Statement

  • Slide 10

  • PHP - The if...else Statement

  • Slide 12

  • PHP - The if...elseif....else Statement

  • Example

  • The PHP switch Statement

  • Slide 16

  • Slide 17

  • PHP Loops

  • The PHP while Loop

  • Slide 20

Tài liệu cùng người dùng

Tài liệu liên quan