Web Technologies and Programming Lecture 23 Introduction to PHP (Part 2)
Operators in PHP
Arithmetic Operators: +, - ,*, /, %, **
Assignment Operators: =
String Operators: . , .=
Increment/decrement Operators: ++ , --
Logical Operators: AND, OR, NOT, XOR, &&, ||, !
Comparison Operators: >, <, <=, >=
Equality Operators: ==, !=, ===
Conditional statements
if statement - executes some code if one condition is true
if...else statement - executes some code if a condition is true and another code if that condition is false
if...elseif....else statement - executes different codes for more than two conditions
switch statement - selects one of many blocks of code to be executed
Looping statements
For Loop
While Loop
Do-While Loop
ForEach Loop
Arrays in PHP
Associative arrays
Sorting arrays
65 trang |
Chia sẻ: hoant3298 | Lượt xem: 593 | Lượt tải: 0
Bạn đang xem trước 20 trang tài liệu Web Technologies and Programming Lecture 23 Introduction to PHP (Part 2), để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
1Web Technologies and ProgrammingLecture 23Introduction to PHP (Part-2)2Summary of Previous LectureSetting the environmentPHP overviewWhat is a PHP FileOpen-sourcePlatform independentWhat Can PHP Do?Why PHP?Basic PHP SyntaxWriting and Executing PHP CodePHP constantsConstants are GlobalPHP variables LocalGlobalStaticType DeterminationPHP StringsPHP is a Loosely Typed Language3Today’s Lecture OutlineOperators in PHPConditional Statements in PHPLooping StatementsArrays in PHP41. Operators in PHP5Operators are used to perform operations on variables and values.PHP divides the operators in the following groups:Arithmetic operatorsAssignment operatorsString operatorsIncrement/Decrement operatorsLogical operatorsComparison operatorsEquality OperatorsArray operators1. Operators in PHP6Arithmetic Operators:The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc. +, - ,*, /, %, **1. Operators in PHP7Assignment 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.=+= ($a +=$b ), *= , /=.= ($a .= $b)1. Operators in PHP81. Operators in PHP9String Operators: PHP has two operators that are specially designed for strings.. , .=$a=“abcd”.”efgh”; $a=abcdefgh$a.=“ijk”; $a=abcdefghijk 1. Operators in PHP10String Operators:1. Operators in PHP11First VariableSecond VariableConcatenationUsing .=1. Operators in PHP121. Operators in PHP13Adds $b in $aConcatenates $b with $a1. Operators in PHP141. Operators in PHP15Increment/decrement Operators: The PHP increment operators are used to increment a variable's value.The PHP decrement operators are used to decrement a variable's value.++ , --$b=$a++$b=++$a1. Operators in PHP16Increment/decrement Operators:1. Operators in PHP17Variable DeclaredIncremented Before DisplayIncremented After DisplayDisplaying Incremented Value1. Operators in PHP181. Operators in PHP19Logical Operators:The PHP logical operators are used to combine conditional statements. AND, OR, NOT, XOR&&, ||, !1. Operators in PHP20Logical Operators:1. Operators in PHP21Comparison Operators:The PHP comparison operators are used to compare two values (number or string): >, =1. Operators in PHP22Comparison Operators:1. Operators in PHP23Equality Operators: ==, !=, === 1. Operators in PHP24Integer ValueString ValueCompares Only ValuesStrict Comparison, Data Types Should Also Match1. Operators in PHP252. Conditional StatementsWhen you want to perform different actions for different conditions. You can use conditional statements in your code to do this.In PHP we have the following conditional statements:if statement - executes some code if one condition is trueif...else statement - executes some code if a condition is true and another code if that condition is falseif...elseif....else statement - executes different codes for more than two conditionsswitch statement - selects one of many blocks of code to be executed262. Conditional Statementsif statement:The if statement executes some code if one condition is true.if(condition) { code to be executed if condition is true;}272. Conditional Statementsif statement Example: \282. Conditional Statementsif-else statement:The if....else statement executes some code if a condition is true and another code if that condition is false.if (condition) { code to be executed if condition is true;} else { code to be executed if condition is false;}292. Conditional Statementsif-else statement Example: 302. Conditional StatementsThe if...elseif....else Statement:The if....elseif...else statement executes different codes for more than two conditions..if (condition) { code to be executed if this condition is true; } elseif (condition) { code to be executed if this condition is true;} else { code to be executed if all conditions are false;}312. Conditional StatementsThe if...elseif....else statement Example: 322. Conditional Statementsswitch statement:Use the switch statement to select one of many blocks of code to be executedswitch(variable){ case option: action break; . .}332. Conditional Statementsswitch (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;}342. Conditional Statements35Switch StartsCase 0Case 12. Conditional Statements363. Looping StatementsFor LoopWhile LoopDo-While LoopForEach Loop373. Looping Statementsfor loopThe 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;}.for($a=0; $a";}?> 393. Looping Statementswhile loopThe while loop executes a block of code as long as the specified condition is true.while(condition is true){ //Statements //Increment/decrement}403. Looping Statementswhile loop Example: "; $x++;} ?>413. Looping Statementsdo-while loopThe 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.do{ //Statements //Increment/decrement}While(condition is true);423. Looping Statementsdo-while loop Example: "; $x++;} while ($x 433. Looping Statementsforeach loopThe foreach loop works only on arrays, and is used to loop through each key/value pair in an arrayis used to read an entire arrayforeach ($array as $value) { code to be executed;}443. Looping Statementsforeach loop example: ";}?>453. Looping Statements46For LoopWhile Loop3. Looping Statements47Output From For LoopOutput From While Loop3. Looping Statements48Array DeclarationForEach Loop StartsUsing Obtained Value3. Looping Statements494. Arrays in PHPAn array stores multiple values in one single variableAn array is a special variable, which can hold more than one value at a time.An array is traditionally defined as a group of items that share certain characteristicsEach item consists of two components:KeyValuePHP doesn’t require that you assign a size to an array at creation time504. Arrays in PHPDeclaring an array:$array_name[key] = value;$players[0] = “Shahid Khan Afridi”;Adding element in an array:$players[1] = “Muhammad Amir”;Accessing element in an arrayecho $players[0];514. Arrays in PHP52Declaring ArrayAdding ElementsForEach Loop4. Arrays in PHP534. Arrays in PHPAssociative arrays: Arrays with named keys$array_name[‘element-name’] = value;$players[‘shahid’] = “Shahid Khan Afridi”;Adding element in an array:$players[‘amir’] = “Muhammad Amir”;Accessing element in an array:echo $players[‘shahid’];544. Arrays in PHPThe array(); can also be used to create an array $array_name = array(item_1, item_2, , item_n);$players = array(“Shahid Khan Afridi”, ”Muhammad Amir”);$players = array(“shahid”=>“Shahid Khan Afridi”, ”amir”=>”Muhammad Amir”);554. Arrays in PHP56Associative Array Declared Using array()Accessing Elements by Name4. Arrays in PHP574. Arrays in PHP58Sorting arrays:sort()Sorts the array in ascending orderrsort()Sorts the array in descending order4. Arrays in PHP59Array DeclarationSorting ArrayForEach Loop for Displaying Arrays4. Arrays in PHP604. Arrays in PHP61Reverse Sorting4. Arrays in PHP62Summary of Today’s LectureOperators in PHPArithmetic Operators: +, - ,*, /, %, **Assignment Operators: =String Operators: . , .=Increment/decrement Operators: ++ , --Logical Operators: AND, OR, NOT, XOR, &&, ||, !Comparison Operators: >, =Equality Operators: ==, !=, === Conditional statementsif statement - executes some code if one condition is trueif...else statement - executes some code if a condition is true and another code if that condition is falseif...elseif....else statement - executes different codes for more than two conditionsswitch statement - selects one of many blocks of code to be executed63Summary of Today’s LectureLooping statementsFor LoopWhile LoopDo-While LoopForEach LoopArrays in PHP Associative arraysSorting arrays64THANK YOU65
Các file đính kèm theo tài liệu này:
- lecture_23_wt_introduction_to_php_part_2_8899_2028585.pptx