Title: Goals of this class Include
1Goals of this classInclude Require in
PHPGenerating Random Numbers in PHPArrays
Numerically Indexed and AssociativeProgram Flow
Conditionals and Loops
2Include Require in PHP
3Include in PHPallows functions to be shared
across multiple PHP scriptsIncludeAny 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.phplt?phpfu
nction bold(string) echo "ltbgt" . string .
"lt/bgt\n" ?gt 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"
4Require in PHPuseful 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. lt?require "footer.inc" ?gt
"footer.inc" lthrgtltbrgt(c) 2001 Hugh E. Williams
and David Lane
5Generating Random Numbers in PHP
6Generating Random Numbers in PHPmt_rand(PHP
3gt 3.0.6, PHP 4 )mt_rand -- Generate a better
random valueDescriptionint mt_rand ( int min,
int max)//generate random numberrandom_num
mt_rand(0, 10)// print resultprint("ltbgtrandom
_numlt/bgt")
7Arrays Numeric and Associative
8Arrays 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 valuesThere are 2 types of
arraysYou use the index number to access
variables in a Numerically Indexed
Arraysnumbers 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 wordarray
array("first"gtTom, last"gtJones,
city"gtNew York)
9Arrays Numerically Indexed Construction
newArray array("Potatoes" "Carrots, "Spinach"
) newArray0 "Potatoes"newArray1
"Carrots"newArray2 "Spinach"
Accessdinner newArray0i2dinner
newArrayiNote that the index for an array
often starts at ZERO
10Arrays Associative Associative Arrays have Key
/ Value pairs Construction newArray
array(" favorite "gt Potatoes, " least_favorite
"gt "Carrots, " soso "gt "Spinach" )
newArrayfavorite "Potatoes"newArrayl
east_favorite "Carrots"newArraysoso
"Spinach" Accessdinner newArrayfavorite
11Arrays Basic Functions
12Arrays Counting elements in arraysThe count(
) function returns the number of elements in the
array var integer count(mixed var) The
following example prints 7 as expecteddays
array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
"Sun")echo count(days) // 7The count( )
function works on any variable type and returns 0
when either an empty array or a variable that
isn't set is examined.
13Arrays Finding the maximum and minimum values in
an arrayThe 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
14Arrays 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
15Conditionals, Logic Program Flow
16Conditionals 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 gt 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 gt
5) echo "The variable is greater than 5"
else echo "The variable is NOT greater than
5"
17Conditionals ELSEIF Using ELSEIF allows us to
continue to test for different conditions if
(var gt 5) echo "The variable is greater than
5" elseif (var lt 5) echo "The variable is
less than 5" else echo "The variable is
5"
18Conditionals SWITCHThe 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.
19Conditional 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 gt, lt, gt,
and lt if (var lt 5) echo "Less than 5"
if (var lt 5) echo "Less than or equal to
5"
20Conditional 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"
21Conditional Expressions - STRINGSPHP 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
22LoopsThere are 4 main types of logical loops in
PHP While do...while for foreach
23Loops - WHILEThe while loop is the simplest
looping structure. The while loop repeats one
or more statementsthe loop bodyas 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 1while (counter lt
11) echo counter echo " " // Add one to
counter counter
24Loops - FORThe for loop is the most complicated
of the loop constructs, but it also leads to the
most compact code. for(counter1 counterlt11
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.
25Loops - 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") foreac
h (words as value) echo "the current
value valueltbr /gt\n" Associative
Arrays colors'favorite''orange' colors'le
ast_fav''grey' colors'soso''brown' fore
ach (colors as key gt value) echo "
key is value ltbrgt"