Including the same file twice or declaring a function in the script that is already in an include file causes PHP to complain about the function being redefined. You can dynamically set which include to use if ($netscape == true){ include "netscape.inc"; }else{ include "other.inc"; }"> Including the same file twice or declaring a function in the script that is already in an include file causes PHP to complain about the function being redefined. You can dynamically set which include to use if ($netscape == true){ include "netscape.inc"; }else{ include "other.inc"; }">
Download presentation
Presentation is loading. Please wait.
Published byColin O’Brien’ Modified over 9 years ago
1
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program Flow – Conditionals and Loops
2
Class 2Intro to Databases Include & Require in PHP
3
Class 2Intro to Databases Include in PHP allows functions to be shared across multiple PHP scripts Include Any PHP code in an include file must be surrounded by the PHP start and end script tags. The PHP script engine treats the contents of include files as HTML unless script tags are used include.php ". $string. " \n"; } ?> Including the same file twice or declaring a function in the script that is already in an include file causes PHP to complain about the function being redefined. You can dynamically set which include to use if ($netscape == true){ include "netscape.inc"; }else{ include "other.inc"; }
4
Class 2Intro to Databases Require in PHP useful for creating reusable HTML. Require If a file must always be included, the require directive should be used instead of include. The require directive is processed before the script is executed, and the contents of the required file are always inserted in the script. "footer.inc"; (c) 2001 Hugh E. Williams and David Lane
5
Class 2Intro to Databases Generating Random Numbers in PHP
6
Class 2Intro to Databases Generating Random Numbers in PHP mt_rand (PHP 3>= 3.0.6, PHP 4 ) mt_rand -- Generate a better random value Description int mt_rand ( [int min, int max]) //generate random number $random_num=mt_rand(0, 10); // print result print(" $random_num ");
7
Class 2Intro to Databases Arrays – Numeric and Associative
8
Class 2Intro to Databases Arrays – Numeric and Associative Arrays are compound variables, meaning they can hold more then one value. (think of a carton of eggs) Arrays can hold scalar variables – Boolean, String etc or other arrays. Arrays have a structure that allows us to access, add, modify and delete these values There are 2 types of arrays You use the index number to access variables in a Numerically Indexed Arrays $numbers = array(5, 4, 3, 2, 1); Associative Arrays are made up of key/value pairs. The Value is the variable The Key is the index word or “Key word” $array = array("first"=>”Tom”, “last"=>”Jones”, “city"=>”New York”);
9
Class 2Intro to Databases Arrays – Numerically Indexed Construction $newArray= array("Potatoes"; "Carrots“, "Spinach" ); $newArray[0] = "Potatoes"; $newArray[1] = "Carrots"; $newArray[2] = "Spinach"; Access $dinner= $newArray[0]; $i=2; $dinner= $newArray[$i]; Note that the index for an array often starts at ZERO
10
Class 2Intro to Databases Arrays –Associative Associative Arrays have Key / Value pairs Construction $ newArray = array(" favorite "=>” Potatoes”, " least_favorite "=> "Carrots“, " soso "=> "Spinach" ); $newArray[‘favorite’] = "Potatoes"; $newArray[‘least_favorite’] = "Carrots"; $newArray[‘soso’] = "Spinach"; Access $dinner= $newArray[‘favorite’];
11
Class 2Intro to Databases Arrays –Basic Functions
12
Class 2Intro to Databases Arrays – Counting elements in arrays The count( ) function returns the number of elements in the array var: integer count(mixed var) The following example prints 7 as expected: $days = array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"); echo count($days); // 7 The count( ) function works on any variable type and returns 0 when either an empty array or a variable that isn't set is examined.
13
Class 2Intro to Databases Arrays –Finding the maximum and minimum values in an array The maximum and minimum values can be found from an array numbers with max( ) and min( ), respectively: number max(array numbers) number min(array numbers) If an array of integers is examined, the returned result is an integer, if an array of floats is examined, min( ) and max( ) return a float: $var = array(10, 5, 37, 42, 1, -56); echo max($var); // prints 42 echo min($var); // prints -56
14
Class 2Intro to Databases Arrays –Sorting with sort( ) and rsort( ) The simplest array-sorting functions are sort( ) and rsort( ), which rearrange the elements of the subject array in ascending and descending order, respectively: sort(array subject [, integer sort_flag]) rsort(array subject [, integer sort_flag]) Both functions sort the subject array based on the values of each element. The following example shows the sort( ) function on an array of integers: $numbers = array(24, 19, 3, 16, 56, 8, 171); foreach($numbers as $n) echo $n. " "; // displays 24, 19, 3, 16, 56, 8, 171) sort($numbers); foreach($numbers as $n) echo $n. " "; // displays 3 8 16 19 24 56 171
15
Class 2Intro to Databases Conditionals, Logic & Program Flow
16
Class 2Intro to Databases Conditionals– IF The basic format of an if statement is to test whether a condition is true and, if so, to execute one or more statements. if ( $var > 5 ){ echo "The variable is greater than 5"; } Using ELSE executes a statement (or block of statements) if the expression evaluates as False if ($var > 5){ echo "The variable is greater than 5"; } else{ echo "The variable is NOT greater than 5"; }
17
Class 2Intro to Databases Conditionals– ELSEIF Using ELSEIF allows us to continue to test for different conditions if ($var > 5){ echo "The variable is greater than 5"; } elseif ($var < 5){ echo "The variable is less than 5"; } else{ echo "The variable is 5"; }
18
Class 2Intro to Databases Conditionals – SWITCH The switch statement can be used as an alternative to if to select an option from a list of choices. The switch method is usually more compact, readable, and efficient to type. switch ($menu){ case 1: echo "You picked one"; break; case 2: echo "You picked two"; break; default: echo "You picked another option"; } The use of break statements is important: they prevent execution of statements that follow in the switch statement and continue execution with the statement that follows the closing brace.
19
Class 2Intro to Databases Conditional Expressions Equality is tested with the double-equal operator, ==. Consider an example: $var = 1; if ($var == 1) echo "Equals one!"; Inequality can be tested with the != inequality operator: $var = 0; if ($var != 1) echo "Does not equal one!"; Greater or lesser values can be tested with the >, =, and <= if ($var < 5) echo "Less than 5"; if ($var <= 5) echo "Less than or equal to 5";
20
Class 2Intro to Databases Conditional Expressions - Boolean Boolean Operators Expressions can be combined with parentheses and with the Boolean operators && (and) and || (or). For example, the following expression returns true and prints the message if $var is equal to either 3 or 7: if ($var == 3) || ($var == 7) echo "Equals 3 or 7"; The following expression returns true and prints the message if $var equals 2 and $var2 equals 6: if ($var == 2) && ($var2 == 6) echo "The variables are equal to 2 and 6";
21
Class 2Intro to Databases Conditional Expressions - STRINGS PHP provides the string comparison function strcmp( ) that safely compares two strings, str1 and str2: integer strcmp(string str1, string str2) Takes two strings as arguments, str1 and str2, and returns 0 if the strings are identical 1 if str1 is less than str2 -1 if str1 is greater that str2. print strcmp("aardvark", "zebra"); // -1
22
Class 2Intro to Databases Loops There are 4 main types of logical loops in PHP While do...while for foreach
23
Class 2Intro to Databases Loops - WHILE The while loop is the simplest looping structure. The while loop repeats one or more statements—the loop body—as long as a condition remains true. The condition is checked first, then the loop body is executed. So, the loop never executes if the condition isn't initially true. The following fragment illustrates the while statement by printing out the integers from 1 to 10 separated by a space character: $counter = 1; while ($counter < 11){ echo $counter; echo " "; // Add one to $counter $counter++; }
24
Class 2Intro to Databases Loops - FOR The for loop is the most complicated of the loop constructs, but it also leads to the most compact code. for($counter=1; $counter<11; $counter++){ echo $counter; echo " "; } The for loop statement has three parts separated by semicolons, and all parts are optional: Initial statements Statements that are executed once, before the loop body is executed. Loop conditions The conditional expression that is evaluated before each execution of the loop body. If the conditional expression evaluates as false, the loop body is not executed. End-loop statements Statements that are executed each time after the loop body is executed.
25
Class 2Intro to Databases Loops - FOREACH The foreach statement provides a convenient way to iterate through the values of an array. Like a for loop, the foreach statement executes the loop body once for each value in an array. Numerically Indexed Arrays $words = array("Web", "Database", "Applications"); foreach ($words as $value) { echo "the current value: $value \n"; } Associative Arrays $colors['favorite']='orange'; $colors['least_fav']='grey'; $colors['soso']='brown'; foreach ($colors as $key => $value) { echo " $key is $value "; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.