Slide môn học PHP session 2c conditional statements in PHP

18 171 0
Slide môn học PHP session 2c conditional statements in PHP

Đ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

Conditional Statements in PHP Session Review - I    Operators are pre-defined symbols that enable us perform specific actions Operators enable us to control the program flow The types of operators are:       Arithmetic operators Logical Operators Relational Operators Bitwise Operators Assignment Operators String Operators PHP / Session / Slide of 18 Review - II     Arithmetic operators are used to perform mathematical operations Relational operators compare two operands and determine the relationship between operands Logical operators handle multiple conditions and combines two or more test expression in a condition Bitwise operators operate on the bits of an operand PHP / Session / Slide of 18 Objectives    Use the if statement Use the Switch statement Use the Ternary (?) operator PHP / Session / Slide of 18 Introduction     Flow refers to the order in which the statements in a program execute Statements executes in a sequence, from the first to the last statement Conditional statements change the order of flow in a program Conditional statements execute a set of statements only when a specified condition is satisfied PHP / Session / Slide of 18 if statement    Executes a block of code only when the specified condition is true The block of code is ignored if the condition is false The syntax for the if statement: if(condition) //Code executes if the condition is true PHP / Session / Slide of 18 if statement - Example $a=50; $b=100; if($a < $b) echo "a is less than b"; $a < $b PHP displays, a is less than b PHP / Session / Slide of 18 if …else statement   Executes a block of code when the specified condition is false Syntax for if …else statement: if(condition) { //Code executes if the condition is true } else { //Code executes if the condition is false } PHP / Session / Slide of 18 if …else statement - Example if($a < $b) { echo “A is less than B”; } else($a > $b) { echo “B is less than A”; } PHP / Session / Slide of 18 Nested if statement   Specify if statement within if or else statement Syntax for nested if statement: if(condition) { //code to be executed } else { //code to be executed if(condition) { //code to be executed } } PHP / Session / Slide 10 of 18 Nested if statement - Example if($a < 500) { echo “A is les than 500”; } else { if ($a >= 500 and $a $y) ? “X is greater than Y” : “Y is greater than X”; Condition True Condition False PHP / Session / Slide 16 of 18 Summary - I      Conditional statements execute a set of statements only when a specified condition is satisfied if statement executes a block of code only when the specified condition is true In a Nested if statement, we specify if statement within if statement or else statement switch statement checks single variable against multiple values switch statement executes a block of code based on the value it matches PHP / Session / Slide 17 of 18 Summary - II    break statement moves the control to the statements following the switch statement default statement is used when none of the case statements matches the value of the switch variable Ternary operator simplifies complex conditions into one line statements PHP / Session / Slide 18 of 18 [...]... of code based on the value it matches PHP / Session 7 / Slide 17 of 18 Summary - II    break statement moves the control to the statements following the switch statement default statement is used when none of the case statements matches the value of the switch variable Ternary operator simplifies complex conditions into one line statements PHP / Session 7 / Slide 18 of 18 ... True Condition False PHP / Session 7 / Slide 16 of 18 Summary - I      Conditional statements execute a set of statements only when a specified condition is satisfied if statement executes a block of code only when the specified condition is true In a Nested if statement, we specify if statement within if statement or else statement switch statement checks single variable against multiple values... default: echo “Enter correct value”; break; }; PHP / Session 7 / Slide 14 of 18 Ternary Operator    Also known as conditional operator Simplifies complex conditions into one line statements Syntax of ternary operator: Term1 ? Term2 : Term3; Term1 - Condition is evaluated Term2 - Executed if condition is True Term3 - Executed if condition if False PHP / Session 7 / Slide 15 of 18 Example of Ternary Statement... between 500 and 700”; else { echo “A is greater than 700”; } } } PHP / Session 7 / Slide 11 of 18 switch statement - I    Checks single variable against multiple values Executes a block of code based on the value it matches Executes the code only when the value of the case statement matches the value of the switch variable PHP / Session 7 / Slide 12 of 18 Syntax of switch statement switch(variable) {... equals value1 break; Moves the control to the statements case value2: //Code executes if condition equals value2 break; default: Used when none of the case statements matches the value of the switch variable //Code executes if the variable does not matches any specified value } PHP / Session 7 / Slide 13 of 18 Example for a switch Statement switch($var) { case Minor: echo “Not eligible to vote”; break;

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

Từ khóa liên quan

Mục lục

  • Conditional Statements in PHP

  • Slide 2

  • Review - II

  • Objectives

  • Introduction

  • if statement

  • if statement - Example

  • if …else statement

  • if …else statement - Example

  • Nested if statement

  • Nested if statement - Example

  • switch statement - I

  • Syntax of switch statement

  • Example for a switch Statement

  • Ternary Operator

  • Example of Ternary Statement

  • Summary - I

  • Summary - II

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

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

Tài liệu liên quan