class 2intro to databases goals of this class include & require in php generating random numbers...

25
Class 2 Intro 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

Upload: colin-obrien

Post on 13-Jan-2016

223 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

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

Page 2: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

Class 2Intro to Databases

Include & Require in PHP

Page 3: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

Class 2Intro to DatabasesInclude in PHP

allows functions to be shared across multiple PHP scripts

IncludeAny 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<?phpfunction bold($string){

echo "<b>" . $string . "</b>\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 useif ($netscape == true){

include "netscape.inc";}else{

include "other.inc";}

Page 4: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

Class 2Intro to Databases

Require in PHP

useful for creating reusable HTML.

RequireIf 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.

<?require "footer.inc"; ?>

"footer.inc"; <hr><br>(c) 2001 Hugh E. Williams and David Lane

Page 5: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

Class 2Intro to Databases

Generating Random Numbers in PHP

Page 6: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

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

Descriptionint mt_rand ( [int min, int max])

//generate random number$random_num=mt_rand(0, 10);

// print resultprint("<b>$random_num</b>");

Page 7: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

Class 2Intro to Databases

Arrays – Numeric and Associative

Page 8: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

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 variableThe Key is the index word or “Key word”$array = array("first"=>”Tom”, “last"=>”Jones”, “city"=>”New York”);

Page 9: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

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

Page 10: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

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’];

Page 11: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

Class 2Intro to Databases

Arrays –Basic Functions

Page 12: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

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.

Page 13: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

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 42echo min($var); // prints -56

Page 14: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

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

Page 15: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

Class 2Intro to Databases

Conditionals, Logic & Program Flow

Page 16: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

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";}

Page 17: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

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";}

Page 18: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

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.

Page 19: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

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";

Page 20: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

Class 2Intro to Databases

Conditional Expressions - Boolean

Boolean OperatorsExpressions 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";

Page 21: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

Class 2Intro to Databases

Conditional Expressions - STRINGS

PHP provides the string comparison functionstrcmp( ) 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 identical1 if str1 is less than str2-1 if str1 is greater that str2.

print strcmp("aardvark", "zebra"); // -1

Page 22: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

Class 2Intro to Databases

LoopsThere are 4 main types of logical loops in PHP

Whiledo...whileforforeach

Page 23: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

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++;}

Page 24: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

Class 2Intro to Databases

Loops - FORThe 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.

Page 25: Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program

Class 2Intro to Databases

Loops - FOREACHThe 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<br />\n";

}

Associative Arrays$colors['favorite']='orange';$colors['least_fav']='grey';$colors['soso']='brown';

foreach ($colors as $key => $value) { echo " $key is $value <br>";

}