Basic PHP

2,904 views

Published on

Presentation covering beginning PHP

Published in: Technology
1 Comment
3 Likes
Statistics
Notes
No Downloads
Views
Total views
2,904
On SlideShare
0
From Embeds
0
Number of Embeds
329
Actions
Shares
0
Downloads
270
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide
  • Since everything is HTML embedded, must have a way of telling the server what is PHP and what is HTML. 1 and 2 are both always available, however number 1 is used most often. 3 and 4 are only available if enabled by the server administrator. Closing tag is optional. Lines of code must end in PHP.
  • Don’t add scope until talking about functions b/c examples don’t make sense until then.
  • Don’t add scope until talking about functions b/c examples don’t make sense until then.
  • Form with 8 names and name1, name2, etc… and it gets submitted For loop 8 times: $temp_name = name$i echo $$temp_name // holds the actual value from the form. End of for
  • String to number- if string starts with number or sign then it gets that value, if it starts with anything else it gets evaluated to zero.
  • Two different variations of AND and OR because they have different precedence
  • Point out that on single command if statements the curly braces are optional.
  • for ($i=1;$i<=10;print $i++) More on for the expressions can be blank or have multiple expressions in them. Go to php.net for more
  • Basic PHP

    1. 1. Introduction to PHP Todd Barber IT Analyst III / University Webmaster Information Technology Services
    2. 2. <ul><li>What is PHP ? </li></ul><ul><ul><li>PHP: Hypertext Preprocessor </li></ul></ul><ul><ul><li>A server-side, cross-platform HTML embedded scripting language </li></ul></ul><ul><li>What do I need? </li></ul><ul><ul><li>PHP-enabled web server </li></ul></ul><ul><ul><li>Text editor </li></ul></ul><ul><ul><li>A way to get PHP scripts from your machine to the server </li></ul></ul>What is it? and What do I need?
    3. 3. Basic Syntax <ul><li>Escaping from HTML </li></ul><ul><ul><li><?php … ?> </li></ul></ul><ul><ul><li><script language=“php”> … </script> </li></ul></ul><ul><ul><li><? … ?> </li></ul></ul><ul><ul><li><% … %> </li></ul></ul><ul><li>For Example… </li></ul><ul><li><?php </li></ul><ul><li>/* The world renowned first program. </li></ul><ul><li> Programmer: Todd Barber </li></ul><ul><li> Date: October 31, 2006 </li></ul><ul><li>*/ </li></ul><ul><li>echo “Hello World!”; #displays on the screen </li></ul><ul><li>// A simple program to illustrate just the basics </li></ul><ul><li>?> </li></ul>
    4. 4. Types <ul><li>Boolean -> TRUE or FALSE </li></ul><ul><li>Integer – number of the set Z = {..., -2, -1, 0, 1, 2, ...} </li></ul><ul><li>Float -> “decimal numbers” </li></ul><ul><li>String – series of characters </li></ul><ul><ul><li>Single quote (‘) – doesn’t expand variables </li></ul></ul><ul><ul><li>Double quote (“) – does expand variables </li></ul></ul><ul><li>Array – ordered map that assigns values to keys </li></ul><ul><li>NULL – represents that a variable has no value. NULL is the only possible value. </li></ul>See http://us2.php.net/manual/en/language.types.php for all possible types and more details.
    5. 5. Variables <ul><li>Variables are represented by a dollar sign followed by the name of the variable. </li></ul><ul><li>The variable name is case-sensitive. </li></ul><ul><li>Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. </li></ul><ul><li>$this_is_a_valid_variable </li></ul><ul><li>$this-one-is-not </li></ul>
    6. 6. More Variables <ul><li>Predefined Variables </li></ul><ul><ul><li>$GLOBALS – all variables currently in the global scope </li></ul></ul><ul><ul><li>$_SERVER – all variables set by the web server </li></ul></ul><ul><ul><li>$_POST – variables provided by the HTTP POST </li></ul></ul><ul><ul><li>$_GET – variables provides by the HTTP GET (in the URL) </li></ul></ul><ul><ul><li>$_SESSION – variables currently registered with the script’s session </li></ul></ul>
    7. 7. More Variables - Scope <ul><li>For the most part PHP variables have a single scope </li></ul><ul><li><?php </li></ul><ul><li>$a=1; </li></ul><ul><li>include ‘header_info.php’; </li></ul><ul><li>?> </li></ul><ul><li>You can reference the $a variable in the file ‘header_info.php’ </li></ul><ul><li><?php </li></ul><ul><li>$b=1; </li></ul><ul><li>function footer_info () { </li></ul><ul><li>echo $b; </li></ul><ul><li>} </li></ul><ul><li>footer_info(); </li></ul><ul><li>?> </li></ul><ul><li>Nothing will output. The echo statement refers to the local scope of the variable (inside the function.) Global variables must be declared global inside the function. </li></ul>
    8. 8. <ul><li>Global Keyword </li></ul><ul><li><?php $a = 1; $b = 2; function Sum() {    global $a, $b;    $b = $a + $b; } Sum(); echo $b; </li></ul><ul><li>?> </li></ul><ul><li>What is $b? </li></ul>More Variables – Scope <ul><li>Passing Variables </li></ul><ul><li><?php $a = 1; $b = 2; function Sum ($a, $b) {    $b = $a + $b; } Sum($a, $b); echo $b; </li></ul><ul><li>?> </li></ul><ul><li>What is $b? </li></ul>3 2
    9. 9. More Variables - Variable <ul><li>Variable names which can be set and used dynamically </li></ul><ul><li><?php </li></ul><ul><li>$a = ‘hello’; </li></ul><ul><li>$$a = ‘world’; </li></ul><ul><li>echo “$a ${$a}”; </li></ul><ul><li>echo “$a $hello”; </li></ul><ul><li>?> </li></ul><ul><li>Constants - value cannot change during the execution of the script A constant is case-sensitive by default. By convention, constant identifiers are always uppercase. </li></ul><ul><li><?php </li></ul><ul><li>define (“PI”,”3.14159265358979323846”); </li></ul><ul><li>echo PI; </li></ul><ul><li>?> </li></ul>This outputs hello world hello world This outputs 3.14159265358979323846
    10. 10. Operator Precedence Associativity Operators Additional Information non-associative new new left [ array() non-associative ++ -- increment/decrement non-associative ~ - (int) (float) (string) (array) (object) @ types non-associative instanceof types right ! logical left * / % arithmetic left + - . arithmetic and string left << >> bitwise non-associative < <= > >= comparison non-associative == != === !== comparison left & bitwise and references left ^ bitwise left | bitwise left && logical left || logical left ? : ternary right = += -= *= /= .= %= &= |= ^= <<= >>= assignment left and logical left xor logical left or logical left , (comma) many uses
    11. 11. Operators <ul><li>Arithmetic </li></ul><ul><ul><li>-$a is negation </li></ul></ul><ul><ul><li>$a + $b is addition </li></ul></ul><ul><ul><li>$a - $b is subtraction </li></ul></ul><ul><ul><li>$a * $b is multiplication </li></ul></ul><ul><ul><li>$a / $b is division (always returns float) </li></ul></ul><ul><ul><li>$a % $b is modulus </li></ul></ul><ul><li>Assignment </li></ul><ul><ul><li>$a = 5 is NOT “equal to.” It is “gets set to” </li></ul></ul><ul><ul><li>Combined operators </li></ul></ul><ul><ul><ul><li>+=, -=, *=, /=, %=, .= </li></ul></ul></ul>
    12. 12. More Operators <ul><li>Comparison </li></ul><ul><ul><li>$a == $b is equal </li></ul></ul><ul><ul><li>$a === $b is identical (includes type) </li></ul></ul><ul><ul><li>$a != $b is not equal </li></ul></ul><ul><ul><li>$a <> $b is not equal </li></ul></ul><ul><ul><li>$a !== $b is not identical </li></ul></ul><ul><ul><li>$a < $b is less than </li></ul></ul><ul><ul><li>$a > $b is greater than </li></ul></ul><ul><ul><li>$a <= $b is less than or equal to </li></ul></ul><ul><ul><li>$a >= $b is greater than or equal to </li></ul></ul><ul><li>Conditional </li></ul><ul><ul><li>?: is ternary – expr1 ? expr2 : expr3 </li></ul></ul><ul><ul><ul><li>If expr1 is TRUE, the expression evaluates to expr2 </li></ul></ul></ul><ul><ul><ul><li>If expr1 is FALSE, the expression evaluates to expr3 </li></ul></ul></ul><ul><ul><li>Shorthand for single expression if statements </li></ul></ul>
    13. 13. Comparing Different Types Operand 1 Operand 2 Result Null or String String Convert NULL to “”, numerical or lexical comparison Bool or null Anything Convert to bool, FALSE<TRUE String or number (or resource) String or number (or resource) Translate strings (and resources) to numbers, usual math Array Array <ul><li>Array with fewer numbers is smaller </li></ul><ul><li>If key from operand 1 is not found in operand 2 then arrays are incomparable </li></ul><ul><li>Compare value by value </li></ul>Array Anything Array is always greater
    14. 14. More Operators <ul><li>Error Control </li></ul><ul><ul><li>@ - suppresses any errors that may be generated </li></ul></ul><ul><ul><ul><li>Works on expressions – if you can take a value of it then you can use it. </li></ul></ul></ul><ul><li>Strings </li></ul><ul><ul><li>Concatenation (.) </li></ul></ul><ul><ul><li>Concatenating Assignment (.=) </li></ul></ul><ul><li><$php </li></ul><ul><li>@include “header_info.php”; // suppress error if file doesn’t exist </li></ul><ul><li>$a = “Hello”; // assign value Hello to $a </li></ul><ul><li>$b = $a . “ World”; // $b evaluates to “Hello World” </li></ul><ul><li>$a .= “ World”; // $a evaluates to “Hello World” </li></ul><ul><li>?> </li></ul>
    15. 15. More Operators <ul><li>Increment and Decrement </li></ul><ul><ul><li>++$a – Preincrement – Increment by one, then return $a </li></ul></ul><ul><ul><li>$a++ - Postincrement – Return $a, then increment by one </li></ul></ul><ul><ul><li>--$a – Predecrement - Decrement by one, then return $a </li></ul></ul><ul><ul><li>$a-- - Postdecrement - Return $a, then decrement by one </li></ul></ul><ul><li><?php $a = 5; echo &quot;Should be 5: &quot; . $a++ . &quot;<br /> &quot;; echo &quot;Should be 6: &quot; . $a . &quot;<br /> &quot;; $a = 5; echo &quot;Should be 6: &quot; . ++$a . &quot;<br /> &quot;; echo &quot;Should be 6: &quot; . $a . &quot;<br /> &quot;; $a = 5; echo &quot;Should be 5: &quot; . $a-- . &quot;<br /> &quot;; echo &quot;Should be 4: &quot; . $a . &quot;<br /> &quot;; $a = 5; echo &quot;Should be 4: &quot; . --$a . &quot;<br /> &quot;; echo &quot;Should be 4: &quot; . $a . &quot;<br /> &quot;; </li></ul><ul><li>?> </li></ul>
    16. 16. More Operators <ul><li>Logical </li></ul><ul><ul><li>$a and $b is AND – TRUE if both are TRUE </li></ul></ul><ul><ul><li>$a or $b is OR – TRUE if either is TRUE </li></ul></ul><ul><ul><li>$a xor $b is XOR – TRUE if either is TRUE, but not both </li></ul></ul><ul><ul><li>! $a is NOT – TRUE is $a is not TRUE </li></ul></ul><ul><ul><li>$a && $b is AND – TRUE if both are TRUE </li></ul></ul><ul><ul><li>$a || $b is OR – TRUE if either is TRUE </li></ul></ul>
    17. 17. More Operators <ul><li>Array </li></ul><ul><ul><li>$a + $b is union – appends the right side to the left side and doesn’t overwrite variables </li></ul></ul><ul><ul><li>$a == $b is Equal – TRUE is they have the same key/value pairs </li></ul></ul><ul><ul><li>$a === $b is Identity – TRUE if they have the same key/value pairs in the same order and of the same types </li></ul></ul><ul><ul><li>$a != $b is Inequality – TRUE if $a is not equal to $b </li></ul></ul><ul><ul><li>$a <> $b – Same as Inequality above </li></ul></ul><ul><ul><li>$a !== $b is Non-identity – TRUE if $a not identical to $b </li></ul></ul>
    18. 18. Array Comparisons <ul><li><?php $a = array (&quot;a&quot; => &quot;apple&quot;, &quot;b&quot; => &quot;banana&quot;); $b = array (&quot;a&quot; => &quot;pear&quot;, &quot;b&quot; => &quot;strawberry&quot;, &quot;c&quot; => &quot;cherry&quot;); $c = $a + $b; // Union of $a and $b echo &quot;Union of $a and $b: &quot;; var_dump ($c); $c = $b + $a; // Union of $b and $a echo &quot;Union of $b and $a: &quot;; var_dump ($c); </li></ul><ul><li>?> </li></ul><ul><li><?php $a = array (&quot;apple&quot;, &quot;banana&quot;); $b = array (1 => &quot;banana&quot;, &quot;0&quot; => &quot;apple&quot;); var_dump($a == $b); // bool(true) var_dump($a === $b); // bool(false) </li></ul><ul><li>?> </li></ul><ul><li><?php $a = array(1,2,3); $b = array(1,7,8,9,10); $c = $a + $b; // Union of $a and $b echo &quot;Union of $a and $b: &quot;; //echo $c print_r($c); </li></ul><ul><li>?> </li></ul>
    19. 19. Control Structures -- if <ul><li>if – evaluates an expression to its Boolean value – if the expression evaluates to TRUE then the conditional code is executed; otherwise it is ignored </li></ul><ul><li><?php </li></ul><ul><li>$a = 5; </li></ul><ul><li>if ($a = 6) </li></ul><ul><li> echo “Hello World”; </li></ul><ul><li>?> </li></ul><ul><li><?php </li></ul><ul><li>$a = 5; </li></ul><ul><li>if ($a == 6) </li></ul><ul><li>{ </li></ul><ul><li> echo “Hello World”; </li></ul><ul><li> $b = 7; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>if block is executed and displays Hello World – note the assignment operator in the expression if block is ignored and nothing is done – note the comparison operator in the expression
    20. 20. Control Structures - else <ul><li>else – extension of if statement that executes code when the if expression evaluates to FALSE </li></ul><ul><li><?php </li></ul><ul><li>$a=3; </li></ul><ul><li>$b=4; </li></ul><ul><li>if ($a < $b) </li></ul><ul><li> echo “Variable A is less than B”; </li></ul><ul><li>else </li></ul><ul><li> echo “Variable B is less than A”; </li></ul><ul><li>?> </li></ul>
    21. 21. Control Structures - elseif <ul><li>elseif – another extension of if </li></ul><ul><li><?php </li></ul><ul><li>$a=3; </li></ul><ul><li>$b=4; </li></ul><ul><li>if ($a < $b) { </li></ul><ul><li> echo “Variable A is less than B”; </li></ul><ul><li>} elseif ($a == $b) { </li></ul><ul><li> echo “Variable A has the same value as B”; </li></ul><ul><li>} else { </li></ul><ul><li> echo “Variable B is less than A”; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
    22. 22. Control Structures - while <ul><li>while – execute the statements as long as the expression evaluates to TRUE </li></ul><ul><li><?php </li></ul><ul><li>$a=3; </li></ul><ul><li>while ($a==4) </li></ul><ul><li>{ </li></ul><ul><li> echo “The value of a is ”.$a; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul><ul><li><?php </li></ul><ul><li>$i = 1; </li></ul><ul><li>while ($i <=10) </li></ul><ul><li>{ </li></ul><ul><li> echo “i is set to “.$i++; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul><?php $j = 0; while ($j <=10) { echo “j is set to “.++$j; } ?> Counts to 10 Counts to 11
    23. 23. Control Structures – do-while <ul><li>do-while – same as while except the code chunk is guaranteed to execute at least once </li></ul><ul><li><?php </li></ul><ul><li>$a=3; </li></ul><ul><li>while ($a==4) </li></ul><ul><li>{ </li></ul><ul><li> echo “The value of a is ”.$a; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul><?php $a=3; do { echo “The value of a is ”.$a; } while ($a==4); ?> Evaluates to FALSE and while loop statement(s) are never executed while expression isn’t evaluated until after at least one iteration of the do-while statements. This echoes “The value of a is 3” to the screen.
    24. 24. <ul><li>for (expr1; expr2; expr3) </li></ul><ul><li>{ statement(s); } </li></ul><ul><ul><li>expr1 is evaluated unconditionally </li></ul></ul><ul><ul><li>expr2 is evaluated at the beginning of each iteration; continues on TRUE </li></ul></ul><ul><ul><li>expr3 is evaluated at end of iteration </li></ul></ul><ul><li><?php </li></ul><ul><li>for($i=1;$i<=10;$i++) { </li></ul><ul><li> echo $i.”<br />”; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>Control Structures - for <?php $i=1; while ($i<=10) { echo $i.”<br />”; $i++; } ?> functions the same as
    25. 25. Control Structures - switch <ul><li>switch – the same as a series of if…elseif statements </li></ul><ul><li><?php </li></ul><ul><li>$i = 2; </li></ul><ul><li>switch ($i) </li></ul><ul><li>{ </li></ul><ul><li>case 0: </li></ul><ul><li>echo $i; </li></ul><ul><li>break; </li></ul><ul><li>case 1: </li></ul><ul><li>echo $i; </li></ul><ul><li>break; </li></ul><ul><li>case 2: </li></ul><ul><li>echo $i; </li></ul><ul><li>break; </li></ul><ul><li>default: </li></ul><ul><li>echo $i; </li></ul><ul><li>break; </li></ul><ul><li>?> </li></ul><?php $i=2; if ($i==0) echo $i; elseif ($i==1) echo $i; elseif ($i==2) echo $i; ?>
    26. 26. More switch <?php $i = 4; switch ($i) { case 0: case 1: case 2: case 3: echo “I is less than 4”; break; case 4: echo “I is equal to 4”; break; default: echo “I is greater than 4”; break; ?> Combining cases Omitting Breaks <?php $i = 4; switch ($i) { case 0: case 1: case 2: case 3: echo “I is less than 4”; case 4: echo “I is equal to 4”; default: echo “I is greater than 4”; ?>
    27. 27. Control Structures – require and include <ul><li>require – includes and evaluates a specific file; failure results in a Fatal Error </li></ul><ul><li><?php </li></ul><ul><li>require ‘header.php’; </li></ul><ul><li>?> </li></ul><ul><li>include - includes and evaluates a specific file; failure results in a Warning </li></ul><ul><li><?php </li></ul><ul><li>include ‘header.php’; </li></ul><ul><li>?> </li></ul>
    28. 28. Control Structures –require_once and include_once <ul><li>require_once – same as require except if the file has already been included, it will not be included again </li></ul><ul><li><?php </li></ul><ul><li>require_once ‘header.php’; </li></ul><ul><li>?> </li></ul><ul><li>include_once - same as include except if the file has already been included, it will not be included again </li></ul><ul><li><?php </li></ul><ul><li>include_once ‘header.php’; </li></ul><ul><li>?> </li></ul><ul><li>Use when the same file might be included and evaluated more than once during a particular execution of a script, and you want to be sure that it is included exactly once to avoid problems with function redefinitions, variable value reassignments, etc. </li></ul>
    29. 29. User-defined functions <ul><li>Any valid PHP code can appear inside a function </li></ul><ul><li>Names follow the same rules as other labels in PHP </li></ul><ul><li>All functions in PHP have the global scope </li></ul><ul><li><?php </li></ul><ul><li>function my_first_function ($arg1, $arg2, … ,$arg_n) //arguments aren’t mandatory </li></ul><ul><li>{ </li></ul><ul><li>echo “PHP code goes here.”; </li></ul><ul><li>return $return_value; //only used when something is returned </li></ul><ul><li>} </li></ul><ul><li>my_first_function(); // example of function call with no arguments </li></ul><ul><li>my_first_function(1,2); // function with static arguments </li></ul><ul><li>my_first_function($variable); // function with a variable as an argument </li></ul><ul><li>} </li></ul>
    30. 30. Functions and Arguments <ul><li>Information can be passed to the function by the argument list, a comma separated value list of expressions </li></ul><ul><li>Arguments may be passed by: </li></ul><ul><ul><li>Value (default) </li></ul></ul><ul><ul><li>Reference </li></ul></ul><ul><ul><li>Default argument list </li></ul></ul><ul><li>Variable-length argument lists are supported only in PHP4 and greater </li></ul>
    31. 31. <ul><li>Passing by value examples </li></ul><ul><li><?php </li></ul><ul><li>######### NUMBERS ########### </li></ul><ul><li>function add_numbers($value1, $value2) </li></ul><ul><li>{ </li></ul><ul><li>echo “The numbers $value1 + $value2 equal “. $value1+$value2; </li></ul><ul><li>} </li></ul><ul><li>add_numbers(3,4); #adding 2 static numbers </li></ul><ul><li>add_numbers($age1,$age2); #adding 2 ages given by a user from a form </li></ul><ul><li>######### STRING ########### </li></ul><ul><li>function display_instructions() </li></ul><ul><li>{ </li></ul><ul><li>echo “Text goes here. Great for text that changes often and is used many places.”; </li></ul><ul><li>} </li></ul><ul><li>display_instructions(); </li></ul><ul><li>######### ARRAYS ########### </li></ul><ul><li>function add_array_values($func_array) </li></ul><ul><li>{ </li></ul><ul><li>echo “The numbers $func_array[0] + $func_array[1] = “. $func_array[0] + $func_array[1]; </li></ul><ul><li>} </li></ul><ul><li>$main_program_array = array(2,3); </li></ul><ul><li>add_array_values($array); </li></ul><ul><li>?> </li></ul>Function Examples
    32. 32. More Function Examples <ul><li>Passing by Reference and Default Argument List </li></ul><ul><li><?php </li></ul><ul><li>$dbc = OCILogon(&quot;A201&quot;,&quot;$password&quot;,&quot;$database&quot;); </li></ul><ul><li>function which_name($identifier,&$conn,$count=&quot;1&quot;) </li></ul><ul><li>{ </li></ul><ul><li>//global $dbc; </li></ul><ul><li>//$conn =& $GLOBALS['dbc']; </li></ul><ul><li>if ($identifier == &quot;last&quot;) </li></ul><ul><li> $sql = &quot;select t100_first_name, t100_last_name from a201t100 where t100_last_name like 'Q%'&quot;; </li></ul><ul><li>else </li></ul><ul><li> $sql = &quot;select t100_first_name, t100_last_name from a201t100 where t100_first_name like 'Q%'&quot;; </li></ul><ul><li>$stmt = OCIParse($conn,$sql); </li></ul><ul><li>OCIExecute($stmt); </li></ul><ul><li>while ($row=OCI_fetch_row($stmt)) { </li></ul><ul><li>echo &quot;<tr><td>&quot;.$count++.&quot;</td><td>$row[0] $row[1]</td></tr>&quot;;} </li></ul><ul><li>OCIFreeStatement($stmt); </li></ul><ul><li>OCILogoff($conn); </li></ul><ul><li>} </li></ul><ul><li>echo &quot;<table border='1' cellpadding='3’ width='30%'><tr><td>Count</td><td>Name</td></tr> “; </li></ul><ul><li>which_name(&quot;first&quot;,$dbc); </li></ul><ul><li>echo&quot;</table>&quot;; </li></ul><ul><li>?> </li></ul>
    33. 33. More Function Examples <ul><li>Passing by reference notes </li></ul><ul><ul><li>Used when you want to change the value of the object you passed in </li></ul></ul><ul><ul><li>I don’t know of a realistic use except when using classes. Can be used to return more than one value from a function (more on that later). </li></ul></ul><ul><li>Passing by default argument list </li></ul><ul><ul><li>Any defaults must be on the right side of the argument list </li></ul></ul><ul><ul><li>Must be a constant expression </li></ul></ul><ul><ul><li>Uses default unless specified otherwise </li></ul></ul>
    34. 34. Variable length argument lists <ul><li>Uses the func_num_args() , func_get_arg() , and func_get_args() functions. </li></ul><ul><li><?php </li></ul><ul><li>function math() </li></ul><ul><li>{ </li></ul><ul><li>$numargs = func_num_args(); </li></ul><ul><li>echo &quot;Number of arguments: $numargs <br />&quot;; </li></ul><ul><li>echo &quot;The second side is: &quot; . func_get_arg(1) . &quot;<br /> &quot;; </li></ul><ul><li>$arg_list = func_get_args(); </li></ul><ul><li>for ($i = 1; $i < $numargs +1; $i++) { </li></ul><ul><li>echo &quot;Side $i is: &quot; . $arg_list[$i -1] . &quot;<br /> &quot;; </li></ul><ul><li>$area += $arg_list[$i -1]; </li></ul><ul><li>} </li></ul><ul><li>return $area; </li></ul><ul><li>} </li></ul><ul><li>$area_of_object = math(3,4,5,6,1); // Prints 'Number of arguments: 5' </li></ul><ul><li>echo &quot;The area of the object is $area_of_object&quot;; </li></ul><ul><li>?> </li></ul>
    35. 35. Returning values <ul><li>A value can be returned by using the optional return() statement </li></ul><ul><li>Function execution is ended immediately and control passed back to the line that called the function </li></ul><ul><li>Returns a single variable </li></ul><ul><ul><li>could be a single number or string </li></ul></ul><ul><ul><li>could be an array with several values </li></ul></ul>
    36. 36. return() examples <ul><li>Single number </li></ul><ul><li><?php </li></ul><ul><li>function square($num) </li></ul><ul><li>{ </li></ul><ul><li>return $num * $num; </li></ul><ul><li>} </li></ul><ul><li>echo square(5); //displays 25 </li></ul><ul><li>?> </li></ul><ul><li>Single String </li></ul><ul><li><?php </li></ul><ul><li>function display($string1, $string2=“World”) </li></ul><ul><li>{ </li></ul><ul><li>return $string1 . $string2; </li></ul><ul><li>} </li></ul><ul><li>echo display(“Hello”,” Todd”);// displays Hello Todd </li></ul><ul><li>echo display(“Hello ”);//displays Hello World </li></ul><ul><li>?> </li></ul>
    37. 37. More return() examples <ul><li>Array – use the built-in list() function to access the array elements </li></ul><ul><li><?php </li></ul><ul><li>function math($num1, $num2) </li></ul><ul><li>{ </li></ul><ul><li>$div = $num1 / $num2; </li></ul><ul><li>$mutli = $num1 * $num2; </li></ul><ul><li>$add = $num1 + $num2; </li></ul><ul><li>$sub = $num1 - $num2; </li></ul><ul><li>return array($div, $mutli, $add, $sub); </li></ul><ul><li>} </li></ul><ul><li>list ($d, $m, $a, $s) = math(&quot;6&quot;,&quot;2&quot;); </li></ul><ul><li>echo &quot;Division: $d&quot;; </li></ul><ul><li>echo &quot;<br />Multiplication:&quot;. $m; </li></ul><ul><li>echo &quot;<br />Addition: $a&quot;; </li></ul><ul><li>echo &quot;<br />Subtraction: $s&quot;; </li></ul><ul><li>?> </li></ul>
    38. 38. More return() examples <ul><li>Passing by reference to return multiple values </li></ul><ul><li><?php </li></ul><ul><li>function split_string($input, $cut_point, &$first, &$second) </li></ul><ul><li>{ </li></ul><ul><li>if(strlen($input) < $cut_point) </li></ul><ul><li>return false; </li></ul><ul><li>$first = substr($input, 0, $cut_point); </li></ul><ul><li>$second = substr($input, $cut_point); </li></ul><ul><li>return true; </li></ul><ul><li>} </li></ul><ul><li>$input_text = &quot;abcdefghijklmnopqrstuvwxyz&quot;; </li></ul><ul><li>if(split_string($input_text, 30, $first_half, $second_half) != true) </li></ul><ul><li>{ </li></ul><ul><li>echo &quot;Could not split input, cut-point is entire string!<br />&quot;; </li></ul><ul><li>} </li></ul><ul><li>if(split_string($input_text, 15, $first_half, $second_half) == true) </li></ul><ul><li>{ </li></ul><ul><li>echo &quot;First segment of input: $first_half<BR>&quot;; </li></ul><ul><li>echo &quot;Second segment of input: $second_half<BR>&quot;; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
    39. 39. Oh, by the way… <ul><li>You can also return by reference. See http://www.php.net/manual/en/language.references.return.php for a simple example and some explanation </li></ul><ul><li>Remember variable variables? (Slide 9) PHP also allows for variable functions </li></ul><ul><li><?php </li></ul><ul><li>function display() </li></ul><ul><li>{ </li></ul><ul><li>echo “In a function…”; </li></ul><ul><li>} </li></ul><ul><li>$variable_function = “display”; </li></ul><ul><li>$variable_function(); </li></ul><ul><li>?> </li></ul><ul><li>Built in Functions - http://www.php.net/manual/en/funcref.php </li></ul><ul><ul><li>Sessions - http://www.php.net/manual/en/ref.session.php </li></ul></ul><ul><ul><li>Oracle - http://www.php.net/manual/en/ref.oci8.php </li></ul></ul><ul><ul><li>Strings - http://www.php.net/manual/en/ref.strings.php </li></ul></ul><ul><ul><li>Date & Time - http://www.php.net/manual/en/ref.datetime.php </li></ul></ul><ul><ul><li>Arrays - http://www.php.net/manual/en/ref.array.php </li></ul></ul><ul><ul><li>File System - http://www.php.net/manual/en/ref.filesystem.php </li></ul></ul><ul><ul><li>PDF - http://www.php.net/manual/en/ref.filesystem.php </li></ul></ul>

    ×