PHP Lecture Notes PDF
Document Details
Uploaded by ExpansiveElation8024
Tags
Summary
This document provides notes on various PHP concepts, including operators, arrays (indexed and associative), conditional statements (if, else if, else, switch), loops (while, do-while, for, foreach), and the break and continue statements. It demonstrates examples of coding in PHP, and may be useful for learning PHP programming.
Full Transcript
INTERNET APPLICATIONS (2) Lecture # 2 Week # 3 CONTROL STRUCTURES IN PHP 1. PHP Operators ( Review) 2. Arrays 3. Conditional statements( if statement – if…else – if…… elseif …. else – switch ) 4. Loops (while – do….while – for – foreach) 5. Break and continue statements. PHP OPERATO...
INTERNET APPLICATIONS (2) Lecture # 2 Week # 3 CONTROL STRUCTURES IN PHP 1. PHP Operators ( Review) 2. Arrays 3. Conditional statements( if statement – if…else – if…… elseif …. else – switch ) 4. Loops (while – do….while – for – foreach) 5. Break and continue statements. PHP OPERATORS Operators are used to perform operations on variables and values. 1. Arithmetic Operators PHP OPERATORS 2. Assignment Operators 3. Logical Operators PHP OPERATORS 4. Comparison Operators ARRAY: An array variable is a storage area holding a number or text. The problem is, a variable will hold only one value. An array is a special variable, which can store multiple values in one single variable. We can store number, string and object in the PHP array. Syntax Rules for naming arrays: Begin with dollar sign Continue with a letter or underscore CREATE AN ARRAY IN PHP In PHP, the array() function is used to create an array: In PHP, there are three types of arrays: 1. Indexed arrays - Arrays with a numeric index 2. Associative arrays - Arrays with named keys 3. Multidimensional arrays - Arrays containing one or more arrays PHP INDEXED ARRAYS An Indexed array stores each array element with a numeric index. Syntax: $array_name = array(value1, value2, value3,..., valueN); The method to create a numeric array. In the following example the index is automatically assigned (the index starts at 0): 1. All PHP array elements are assigned to an index number by default PHP INDEXED ARRAYS You can access the variable values by referring to the array name and index: EXAPMLE PHP ASSOCIATIVE ARRAYS An associative array is a type of array where the key has its own value. In an associative array, we make use of key and value. 1. Keys are descriptive captions of the array element used to access the value of the array. 2. value is the value assigned to the array element. Syntax: PHP ASSOCIATIVE ARRAYS When storing data about specific named values, a numerical array is not always the best way to do it. Like: When you want to record the salaries of your employees. we could use the employees' names as the keys in our associative array, and the value would be their respective salary. PHP ASSOCIATIVE ARRAYS The method to create an associative array: In this example we use an array to assign ages to the different persons: EXAMPLE: EXAMPLE MULTIDIMENSIONAL ARRAYS A multidimensional array is an array containing one or more arrays. It can be represented in the form of matrix which is represented by row * column And each element in the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index. Syntax : TWO-DIMENSIONAL ARRAYS A two-dimensional array is an array of arrays (a three-dimensional array is an array of arrays of arrays). First, take a look at the following table: We are displaying 3 rows and 3 columns. CONTINUE Now the two-dimensional $cars array contains 3 arrays, and it has two indices: row and column. EXAMPLE: To get access to the elements of the $cars array we must point to the two indices (row and column): EXAMPLE CONDITIONAL STATEMENTS In PHP we have the following conditional statements 1. If statement - executes some code if one condition is true 2. If….else estatement - executes some code if a condition is true and another code if that condition is false 3. If…..elseif……else statement - executes different codes for more than two conditions 4. Switch statement selects one of many blocks of code to be executed THE IF STATEMENT The if statement executes some code if one condition is true Syntax: if (condition) { code to be executed if condition is true; } Example: PHP DATE() FUNCTION The date/time functions allow you to get the date and time from the server where your PHP script runs. Note: These functions depend on the locale settings of your server. THE IF...ELSE STATEMENT The if....else statement executes some code if a condition is true and another code if that condition is false Syntax: if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; } Example: THE IF...ELSE STATEMENT THE IF...ELSEIF...ELSE STATEMENT The if....elseif......else statement executes different codes for more than two conditions Syntax: if (condition) { code to be executed if this condition is true; } elseif (condition) { code to be executed if first condition is false and this condition is true; } else { code to be executed if all conditions are false; } Example: EXERCISE Output :Hello World if $a is greater than $b Answer: THE SWITCH STATEMENT The switch statement is used to perform different actions based on different conditions 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; } THE SWITCH STATEMENT Example: THE SWITCH STATEMENT Example: LOOPS 1. while - loops through a block of code as long as the specified condition is true 2. do……while - loops through a block of code once, and then repeats the loop as long as the specified condition is true 3. For - loops through a block of code a specified number of times 4. Foreach - loops through a block of code for each element in an array 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; } Example:s WHILE - LOOP Example2: Count to 100 by tens: D0….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); Example: FOR - LOOP The for loop Loops through a block of code a specified number of times. Syntax: for (init counter; test counter; increment counter) { code to be executed for each iteration; } Example: FOREACH - LOOP The foreach loop Loops through a block of code for each element in an array 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; } Example: FOREACH - LOOP Example: 1. BREAK AND CONTINUE PHP break: We used the break statement before in this lesson It was used to "jump out" of a switch statement. The break statement can also be used to jump out of a loop. Example: jumps out the loop when x is equal to 4 BREAK AND CONTINUE PHP Continue: The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop Example: print from 0 to 9 and skip 4 Thank You Any Questions