Inscribed By: Kian    PHP  &  MySQL  &  Dreamweaver
PART I <ul><li>An Introductory to PHP’s world !  </li></ul><ul><li> </li></ul>
Needed Installers <ul><li>PHP Installer (phpdev423) </li></ul><ul><li>MySQL-front </li></ul><ul><li>Apache Server </li></u...
PHP  <ul><li>PHP was first created by Rasmus Lerdorf as a Personal Home Page tool in 1994. </li></ul><ul><li>Later recursi...
Important PHP Features  <ul><li>Designed from beginning as a server-side web scripting language. </li></ul><ul><li>Runs on...
Important PHP Features  <ul><li>Interpreted language </li></ul><ul><li>Loosely typed language  </li></ul><ul><ul><li>a var...
PHP-Apache Integration PHP Module PHP file Apache Web Server Web Browser Send page Request page HTML doc
<ul><li>PHP code can be standalone (like Perl) or embedded in HTML code (like ASP) </li></ul><ul><li>PHP script tags are u...
Alternate Tags <ul><li>If short tags is enabled in the  php.ini  file you can use </li></ul><ul><ul><li><?   PHP code goes...
Variable Types <ul><li>There are 8 data types in PHP </li></ul><ul><li>4 are scalar types: </li></ul><ul><ul><li>integer, ...
Declaring Variables <ul><li>Variable store a value </li></ul><ul><li>Variable types are not declared </li></ul><ul><li>All...
Notes About Variables <ul><li>The value of a variable is the value of its most recent assignment. </li></ul><ul><li>Variab...
More On Variables <ul><li>Declaring Variables: </li></ul><ul><li>Because in PHP types are associated with values rather th...
Naming Conventions <ul><li>The convention in PHP (coming from C) is that the underscore character is used to simulate a sp...
Constants <ul><li>Constants are defined using the define function and do not begin with a dollar sign </li></ul><ul><li>Th...
Examples <ul><li><?php </li></ul><ul><li>define (&quot;CONSTANT&quot;, &quot;Hello world.&quot;); echo CONSTANT;   // outp...
Three kinds of comments <ul><li>C, Java style multi-line comments </li></ul><ul><ul><li>/* This is a multi-line </li></ul>...
Literals & Strings <ul><li>Integer literals   </li></ul><ul><ul><li>-1 ,  1 ,  2 ,  3 </li></ul></ul><ul><li>Floating poin...
Variable Interpolation  <ul><li>Interpolation takes place inside doubly quoted strings. </li></ul><ul><li>This means that ...
Variable Interpolation  <ul><li>If  $name  has the value  “Rizwan&quot;  then </li></ul><ul><li>&quot;Hello $name&quot;  h...
Displaying Strings  <ul><li>The  print  function displays text on the standard output. </li></ul><ul><ul><li>print ( &quot...
Notes <ul><li>Line break in HTML is:  <BR>  and not (“
 ”) </li></ul><ul><li>Echo & Print:   </li></ul><ul><li>echo “This ...
Examples <ul><li>Strings: </li></ul><ul><ul><li>$string_1   =  “This is a string in double quotes! ”; </li></ul></ul><ul><...
Arithmetical Operators <ul><li>The usual operators are  + ,  - ,  * , and  / </li></ul><ul><li>Note that  /  is always a f...
Relational Operators  (Logical Operators) <ul><li>PHP has the usual C and Java style operators </li></ul><ul><ul><li>||   ...
Comparison Operators <ul><li>PHP has the usual C and Java style operators </li></ul><ul><ul><li>< ,  <= ,  == ,  != ,  > ,...
Comparison of == and === <ul><li>Suppose  $a  has the value  &quot;0&quot;  as a string </li></ul><ul><li>Suppose  $b  has...
Assignment Operators $a = $a % $b $a %= $b %= $a = $a / $b $a /= $b /= $a = $a * $b $a *= $b *= $a = $a - $b $a -= $b -= $...
Expression & Associativity & Evaluation Order <ul><li>$result  = 2 * 2  + 3 * 3 + 5  //Is equal to 18 </li></ul><ul><li>$r...
Functions For Type Testing <ul><li>gettype(arg)  :  Returns a staring representing the type of arg (either  integer or dou...
Functions For Type Testing <ul><li>is_string(arg)  :  Returns a true value if arg is a string, and false if not </li></ul>...
Example <ul><li><?php </li></ul><ul><li>$this->myid = 2;  </li></ul><ul><li>if (is_string($this->myid))  </li></ul><ul><li...
Conditional Statements <ul><li>Conditional allow us to perform different statements depending on certain conditions. </li>...
<ul><li><?  php  //Note the space; will generate error </li></ul><ul><li>$a=2; </li></ul><ul><li>$b=1; </li></ul><ul><li>i...
For Loop <ul><li>The for loop is probably the most frequently used type of loop in PHP scripting and has his syntax:  </li...
While <ul><li>While is a function that allow us to perform actions WHILE a condition is true. </li></ul><?php $i=0; $num=5...
Switch <ul><li>The switch statement works in an unusual way. First it evaluates a given expression then seeks a lable to m...
<ul><li>There are many math constants in PHP. For example  is  M_PI  and  is  M_SQRT2 </li></ul>Math Functions  <?php $rad...
<ul><li>There are many math functions in PHP: </li></ul><ul><ul><li>abs   sqrt </li></ul></ul><ul><ul><li>sin   cos   tan ...
String Operations  <ul><li>The dot operator is used to concatenate strings </li></ul><ul><ul><li>&quot;Hello &quot; . &quo...
String Operations  <ul><li>Converting  $s  to upper case or lower case </li></ul><ul><ul><li>$upper  =  strtoupper ( $s );...
String Comparison  <ul><li>can use the operators </li></ul><ul><ul><li>< ,  <= ,  > ,  >= ,  == ,  != ,  === ,  !== </li><...
Substrings & Replacement <ul><li>Substr   (string, start) </li></ul><ul><ul><li>return substring of string beginning at in...
Examples of substr_replace <ul><li><?php $var = 'ABCDEFGH:/MNRPQR/'; echo &quot;Original: $var<hr />
&quot;; echo &quot;Th...
Exploding a String <ul><li>explode  (separator, string) </li></ul><ul><ul><li>extracts an  array  of  substrings  using th...
Imploding a String <ul><li>implode   (separator, array) </li></ul><ul><ul><li>inverse of explode: makes a single string fr...
Two types of arrays <ul><li>Indexed arrays: </li></ul><ul><ul><li>the indices are 0, 1, 2, .... </li></ul></ul><ul><ul><li...
Indexed Arrays  <ul><li>The general form for setting the key of an array equal to a value is:   $array[ key ] =  value ; <...
Indexed Arrays  <ul><li>Ranges: </li></ul><ul><li>$digits  =  range (0,9); </li></ul><ul><ul><li>$digits [0]  is 0,  </li>...
Examples <ul><li><?php // array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) foreach (range(0, 12) as $number) {    echo $num...
Associative Arrays  <ul><li>In an associative array a key is associated with a value. </li></ul><ul><li>array can be used ...
Associative Arrays <ul><li>An associative array is a table of key-value pairs. Here the names are the keys and the values ...
Sorting Arrays  <ul><li>sort(array)   &  rsort(array) </li></ul><ul><ul><li>sort array in ascending alphabetical or numeri...
Example <ul><li><?php </li></ul><ul><li>$names = array('Fred', 'Ted', 'Barney', 'Gord');  </li></ul><ul><li>sort($names); ...
User-defined Functions <ul><li>User defined functions have the form </li></ul>function  name   ($ argument,  $ argument ) ...
A max Function <ul><li>Return the max of two numeric values </li></ul>function  max2 ( $a ,  $b ) { $a=3; $b=1;  if ( $a  ...
Reserved Words in PHP for E_ALL elseif class FALSE endwhile else cfunction extends endswitch echo( ) case exit( ) endif di...
Reserved Words in PHP print( ) not $HTTP_ENV_VARS PHP_VERSION new $HTTP_POST_FILES $PHP_SELF list( ) $HTTP_POST_VARS PHP_O...
Reserved Words in PHP _wakeup TRUE _sleep $this _LINE_ stdclass _FILE_ switch while static virtual( ) return( ) xor requir...
<ul><li>THE END  </li></ul><ul><li>SEE YOU NEXT SESSION!   </li></ul>
Upcoming SlideShare
Loading in …5
×

P H P Part I, By Kian

3,754 views

Published on

1 Comment
6 Likes
Statistics
Notes
  • sir plz send this ppt to email id nnn96325@gmail.com...........it's urgent plzzzzzzzzzzzzz
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
3,754
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
1
Likes
6
Embeds 0
No embeds

No notes for slide

P H P Part I, By Kian

  1. 1. Inscribed By: Kian  PHP & MySQL & Dreamweaver
  2. 2. PART I <ul><li>An Introductory to PHP’s world ! </li></ul><ul><li> </li></ul>
  3. 3. Needed Installers <ul><li>PHP Installer (phpdev423) </li></ul><ul><li>MySQL-front </li></ul><ul><li>Apache Server </li></ul><ul><li>PHP Editor (Crimson) </li></ul><ul><li>Macromedia Dreamweaver (V.8) </li></ul>
  4. 4. PHP <ul><li>PHP was first created by Rasmus Lerdorf as a Personal Home Page tool in 1994. </li></ul><ul><li>Later recursively renamed to P HP H ypertext P reprocessor </li></ul><ul><li>PHP 1.0, June 1995 </li></ul><ul><li>PHP 2.0, April 1996 </li></ul><ul><li>PHP 3.0, June 1998 </li></ul><ul><li>PHP 4.3.4 (Dec 2003) </li></ul><ul><li>Current version of PHP is 5.1 </li></ul>
  5. 5. Important PHP Features <ul><li>Designed from beginning as a server-side web scripting language. </li></ul><ul><li>Runs on all major operating systems. </li></ul><ul><li>Built-in support for several relational databases such as MySQL </li></ul><ul><li>Built-in support for session management </li></ul>
  6. 6. Important PHP Features <ul><li>Interpreted language </li></ul><ul><li>Loosely typed language </li></ul><ul><ul><li>a variable can hold a value of any type at different stages in its lifetime </li></ul></ul><ul><li>Hybrid language </li></ul><ul><ul><li>can be used in a non object-oriented style or an object-oriented style or a mixture of both. </li></ul></ul><ul><li>Parts of PHP are similar to Perl </li></ul>
  7. 7. PHP-Apache Integration PHP Module PHP file Apache Web Server Web Browser Send page Request page HTML doc
  8. 8. <ul><li>PHP code can be standalone (like Perl) or embedded in HTML code (like ASP) </li></ul><ul><li>PHP script tags are used to distinguish PHP code from HTML code: </li></ul><ul><ul><li><?php </li></ul></ul><ul><ul><li>PHP code goes here </li></ul></ul><ul><ul><li>?> </li></ul></ul><ul><li>These tags are XML compliant and should always be used for fully portable PHP code </li></ul>Where does PHP code go? Start PHP mode End PHP mode
  9. 9. Alternate Tags <ul><li>If short tags is enabled in the php.ini file you can use </li></ul><ul><ul><li><? PHP code goes here ?> </li></ul></ul><ul><li>If you want to print the result of executing the PHP code you can use an = sign as in </li></ul><ul><ul><li><?= PHP code goes here ?> </li></ul></ul><ul><li>Example (both are equivalent) </li></ul><ul><ul><li><? echo 'Hello World' ?> </li></ul></ul><ul><ul><li><?= 'Hello World' ?> </li></ul></ul>
  10. 10. Variable Types <ul><li>There are 8 data types in PHP </li></ul><ul><li>4 are scalar types: </li></ul><ul><ul><li>integer, floating point (double), string, and boolean </li></ul></ul><ul><li>2 are structured types: </li></ul><ul><ul><li>array and object </li></ul></ul><ul><li>2 are special types: </li></ul><ul><ul><li>resource and the NULL type </li></ul></ul>
  11. 11. Declaring Variables <ul><li>Variable store a value </li></ul><ul><li>Variable types are not declared </li></ul><ul><li>All variables begin with a dollar sign ( $ ) </li></ul><ul><ul><li>first character is a letter or underscore (_) </li></ul></ul><ul><ul><li>remaining are letters, underscore or digits </li></ul></ul><ul><ul><li>remember that all Statements in PHP end with ; </li></ul></ul>$count = 0; $name = “David&quot; ; $price = 45.50; $big_num = 1.2345E23; $success = TRUE;
  12. 12. Notes About Variables <ul><li>The value of a variable is the value of its most recent assignment. </li></ul><ul><li>Variables are assigned with a ‘=‘ operator. </li></ul><ul><li>The order of assigning is from right to left. </li></ul><ul><li>Variables do not need to be declared before assignment. </li></ul><ul><li>Variables have no intrinsic type other than the type of their current values. </li></ul><ul><li>Variable that are used before they are assigned have default values. </li></ul>
  13. 13. More On Variables <ul><li>Declaring Variables: </li></ul><ul><li>Because in PHP types are associated with values rather than variables, no such declaration is needed. The first step to use a variable is to assign it a value. </li></ul><ul><li>Assigning Variables: </li></ul><ul><li>$pi = 3 + 0.14159 // approximately </li></ul><ul><li>Reassigning Variables: </li></ul><ul><li>$my_num_var = “This should be a number – hope it’s reassigned! ”; </li></ul><ul><li>$my_num_var = 5; </li></ul>
  14. 14. Naming Conventions <ul><li>The convention in PHP (coming from C) is that the underscore character is used to simulate a space in variable names: </li></ul><ul><li>Example: </li></ul><ul><ul><li>$number_of_files = 3; </li></ul></ul><ul><li>This is different from the uppercase convention that is used in Java </li></ul><ul><ul><li>numberOfFiles = 3; </li></ul></ul>
  15. 15. Constants <ul><li>Constants are defined using the define function and do not begin with a dollar sign </li></ul><ul><li>The convention is to use all uppercase letters for names of constants </li></ul><ul><li>Examples: </li></ul><ul><ul><li>define ( “COURSE ” , &quot;Internet Tools&quot; ); </li></ul></ul><ul><ul><li>define ( ‘ CM_TO_INCH’ , 2.54); </li></ul></ul><ul><li>Now use COURSE and CM_TO_INCH to refer to these constants </li></ul>
  16. 16. Examples <ul><li><?php </li></ul><ul><li>define (&quot;CONSTANT&quot;, &quot;Hello world.&quot;); echo CONSTANT; // outputs &quot;Hello world.&quot; echo Constant; // outputs &quot;Constant&quot; and issues a notice. define (&quot;GREETING&quot;, &quot;Hello you.&quot;, true); echo GREETING; // outputs &quot;Hello you.&quot; echo Greeting; // outputs &quot;Hello you.&quot; ?> </li></ul>
  17. 17. Three kinds of comments <ul><li>C, Java style multi-line comments </li></ul><ul><ul><li>/* This is a multi-line </li></ul></ul><ul><ul><li>comment </li></ul></ul><ul><ul><li>*/ </li></ul></ul><ul><li>C++, Java single line comments </li></ul><ul><ul><li>// a single line comment </li></ul></ul><ul><li>Unix shell script and Perl comments </li></ul><ul><ul><li># a single line comment </li></ul></ul>
  18. 18. Literals & Strings <ul><li>Integer literals </li></ul><ul><ul><li>-1 , 1 , 2 , 3 </li></ul></ul><ul><li>Floating point literals </li></ul><ul><ul><li>1.234 , -4.56 , 1.34E-12 </li></ul></ul><ul><li>There are two kinds of string literal </li></ul><ul><ul><li>single quotes: 'hello' </li></ul></ul><ul><ul><li>double quotes: &quot;hello&quot; </li></ul></ul><ul><li>There are two boolean literals </li></ul><ul><ul><li>TRUE , FALSE </li></ul></ul>
  19. 19. Variable Interpolation <ul><li>Interpolation takes place inside doubly quoted strings. </li></ul><ul><li>This means that variables are replaced by their values and special control sequences such as take effect. </li></ul><ul><li>Interpolation does not take place inside singly quoted strings so is just two characters </li></ul><ul><li>This is similar to interpolation in Perl </li></ul>
  20. 20. Variable Interpolation <ul><li>If $name has the value “Rizwan&quot; then </li></ul><ul><li>&quot;Hello $name&quot; has the value Hello Rizwan </li></ul><ul><li>'Hello $name' has the value Hello $name </li></ul><ul><li>&quot;$name = $name&quot; has the value $name = Rizwan </li></ul><ul><li>In the last case, the escape character is used to specify that the first $ is a literal character. </li></ul>
  21. 21. Displaying Strings <ul><li>The print function displays text on the standard output. </li></ul><ul><ul><li>print ( &quot;Hello $name &quot; ); </li></ul></ul><ul><li>The echo function is similar but parentheses are not needed and several arguments can be supplied </li></ul><ul><ul><li>echo &quot;Hello &quot; , $name , &quot; &quot; ; </li></ul></ul><ul><li>There is also a C-style printf function that produces formatted output. </li></ul>
  22. 22. Notes <ul><li>Line break in HTML is: <BR> and not (“ ”) </li></ul><ul><li>Echo & Print: </li></ul><ul><li>echo “This will print in the user’s browser window. ”; </li></ul><ul><li>echo (“This will print the same script! ”); </li></ul><ul><li>echo “This will print” , ”the same too! ”; </li></ul><ul><li>echo (“But this will produce” , “PARSE ERROR! ”); </li></ul><ul><li>Echo & Print differ in two important differences: </li></ul><ul><ul><li>Unlike ‘echo’ , ‘print’ can accept only one argument. </li></ul></ul><ul><ul><li>Unlike ‘echo’ , ‘print’ returns a value, which represent whether the print statement succeeded. </li></ul></ul><ul><li>print(“3.14159”); //will print a string </li></ul><ul><li>print(3.14159); //will print a number </li></ul>
  23. 23. Examples <ul><li>Strings: </li></ul><ul><ul><li>$string_1 = “This is a string in double quotes! ”; </li></ul></ul><ul><ul><li>$string_2 = ‘This is a string in single quotes! ’; </li></ul></ul><ul><ul><li>$string_39 = “This string has thirty-nine characters.”; </li></ul></ul><ul><ul><li>$string_0 = “ ”; //A string with 0 characters </li></ul></ul><ul><ul><li>$literally = ‘My $variable will not print! ’; </li></ul></ul><ul><ul><li>print($ literally); //Output: My $variable will not print! </li></ul></ul><ul><ul><li>$singly_quoted = ‘This quote mark:’ ‘is no big deal’; </li></ul></ul><ul><ul><li>$singly_quoted = ‘This quote mark ‘s no big deal either’; </li></ul></ul>
  24. 24. Arithmetical Operators <ul><li>The usual operators are + , - , * , and / </li></ul><ul><li>Note that / is always a floating point divide </li></ul><ul><li>For integer divide use (int)( $a / $b ) </li></ul><ul><li>The remainder (mod) operator is % so if $a and $b have integer values then $a % $b is the remainder when $a is divided by $b . </li></ul><ul><li>The ++ and -- operators are available </li></ul><ul><li>String Concatenation . </li></ul>
  25. 25. Relational Operators (Logical Operators) <ul><li>PHP has the usual C and Java style operators </li></ul><ul><ul><li>|| logical or </li></ul></ul><ul><ul><li>&& logical and </li></ul></ul><ul><ul><li>! negation </li></ul></ul><ul><li>There are also different precedence versions </li></ul><ul><ul><li>Or logical or </li></ul></ul><ul><ul><li>and logical and </li></ul></ul><ul><ul><li>xor logical exclusive XOR </li></ul></ul><ul><ul><ul><li>( Will evaluate its two operands and return TRUE only if either one of the operands itself returns true but not both. ) </li></ul></ul></ul>
  26. 26. Comparison Operators <ul><li>PHP has the usual C and Java style operators </li></ul><ul><ul><li>< , <= , == , != , > , >= </li></ul></ul><ul><li>These operators perform type conversions if necessary before the comparison </li></ul><ul><li>There are also the operators </li></ul><ul><ul><li>=== , and !== </li></ul></ul><ul><li>These operators compare value and type so no type conversion is done </li></ul><ul><li>These operators are also used for strings </li></ul>
  27. 27. Comparison of == and === <ul><li>Suppose $a has the value &quot;0&quot; as a string </li></ul><ul><li>Suppose $b has the value 0 as an integer </li></ul><ul><li>Then </li></ul><ul><ul><li>$a == $b is true since both have different data types </li></ul></ul><ul><ul><li>$a === $b is false since $a and $b have different types </li></ul></ul>
  28. 28. Assignment Operators $a = $a % $b $a %= $b %= $a = $a / $b $a /= $b /= $a = $a * $b $a *= $b *= $a = $a - $b $a -= $b -= $a = $a . $b $a .= $b .= (for strings) $a = $a + $b $a += $b += (for numbers) $a = $b $a = $b = Equivalent Example Operator
  29. 29. Expression & Associativity & Evaluation Order <ul><li>$result = 2 * 2 + 3 * 3 + 5 //Is equal to 18 </li></ul><ul><li>$result1 = 2 + 3 * 4 + 5 // Is equal to 19 </li></ul><ul><li>$result2 = (2 + 3) * (4 + 5) // Is equal to 45 </li></ul><ul><li>$result3 = 2 + 2 * “Non-sense” + True; </li></ul><ul><ul><ul><li>//The output would be 3 </li></ul></ul></ul><ul><ul><ul><li>A mixture of Associativity+BooleanValues+String </li></ul></ul></ul><ul><ul><ul><li>Booleans are counted as 0 or 1 </li></ul></ul></ul><ul><ul><ul><li>String is always counted as 0 </li></ul></ul></ul>
  30. 30. Functions For Type Testing <ul><li>gettype(arg) : Returns a staring representing the type of arg (either integer or double or …). </li></ul><ul><li>is_int(arg) : Returns a true value if arg is an integer, and false if not </li></ul><ul><li>is_integer(arg) : </li></ul><ul><li>is_long(arg) : Returns a true value if arg is a long, and false if not </li></ul><ul><li>is_double(arg) : Returns a true value if arg is a double, and false if not </li></ul><ul><li>is_float(arg) : Returns a true value if arg is a float, and false if not </li></ul><ul><li>is_real(arg) : Returns a true value if arg is a real, and false if not </li></ul>
  31. 31. Functions For Type Testing <ul><li>is_string(arg) : Returns a true value if arg is a string, and false if not </li></ul><ul><li>is_array(arg) : Returns a true value if arg is an array, and false if not </li></ul><ul><li>is_object(arg) : Returns a true value if arg is an object, and false if not </li></ul><ul><li>is_bool(arg) : Returns a true value if arg is a boolean value (True & False), and false if not </li></ul>
  32. 32. Example <ul><li><?php </li></ul><ul><li>$this->myid = 2; </li></ul><ul><li>if (is_string($this->myid)) </li></ul><ul><li>print &quot;true&quot;; </li></ul><ul><li>else </li></ul><ul><li>print &quot;false&quot;; </li></ul><ul><li>?> </li></ul>
  33. 33. Conditional Statements <ul><li>Conditional allow us to perform different statements depending on certain conditions. </li></ul><ul><li>if </li></ul><ul><li>if else </li></ul><ul><li>elseif </li></ul><ul><li>else </li></ul>if (boolean expression 1) { ... } elseif (boolean expression 2) { ... } else { ... }
  34. 34. <ul><li><? php //Note the space; will generate error </li></ul><ul><li>$a=2; </li></ul><ul><li>$b=1; </li></ul><ul><li>if ($a==$b) </li></ul><ul><li>{ echo ‘value in $a is equal to the value in $b’; } </li></ul><ul><li>elseif ($a>$b) </li></ul><ul><li>{ echo ‘value in $a is greater than value in $b’; } </li></ul><ul><li>else { echo ‘value in $a is NOT greater than OR equal to the value in $b’; } </li></ul><ul><li>?> </li></ul>
  35. 35. For Loop <ul><li>The for loop is probably the most frequently used type of loop in PHP scripting and has his syntax: </li></ul><ul><li>for (initializer, test, increment) {statement/s} </li></ul><?php $a = 0; $b = 0; for ($i=0; $i < 5; $i++;) { $a += 10; $b += 5; } echo (“At the end of the loop a=$a and b=$b !”); ?>
  36. 36. While <ul><li>While is a function that allow us to perform actions WHILE a condition is true. </li></ul><?php $i=0; $num=50; while ($i < 10) { $sum--; $i++; } echo (“Loop stopped at $i</br> $num is now $num !”); ?>
  37. 37. Switch <ul><li>The switch statement works in an unusual way. First it evaluates a given expression then seeks a lable to match the resulting value. </li></ul><?php $num = 2; switch ($num) { case 1: echo (“This is case 1 code!”); break ; case 2: echo (“This is case 2 code!”); break ; case 3: echo (“This is case 3 code!”); break ; default : echo (“This is default code!”); } ?>
  38. 38. <ul><li>There are many math constants in PHP. For example is M_PI and is M_SQRT2 </li></ul>Math Functions <?php $radius = 1.0; $circ = 2.0 * M_PI * $radius ; $area = M_PI * $radius * $radius ; echo &quot;Circumference is $circ &quot; ; echo &quot;Area is $area &quot; ; ?>
  39. 39. <ul><li>There are many math functions in PHP: </li></ul><ul><ul><li>abs sqrt </li></ul></ul><ul><ul><li>sin cos tan </li></ul></ul><ul><ul><li>asin acos atan atan2 </li></ul></ul><ul><ul><li>exp expm1 log logp1 </li></ul></ul><ul><ul><li>rand round </li></ul></ul><ul><ul><li>min max </li></ul></ul><ul><ul><li>pow </li></ul></ul><ul><li>and many more </li></ul>Math Functions
  40. 40. String Operations <ul><li>The dot operator is used to concatenate strings </li></ul><ul><ul><li>&quot;Hello &quot; . &quot;World&quot; is the same as &quot;Hello World&quot; </li></ul></ul><ul><li>echo , print , printf , print_r , functions display strings on standard output </li></ul><ul><li>The length of a string $s is strlen ( $s ) </li></ul>
  41. 41. String Operations <ul><li>Converting $s to upper case or lower case </li></ul><ul><ul><li>$upper = strtoupper ( $s ); </li></ul></ul><ul><ul><li>$lower = strtolower ( $s ); </li></ul></ul><ul><li>Converting first character of $s to uppercase </li></ul><ul><ul><li>$upper_first = ucfirst ( $s ); </li></ul></ul><ul><li>Converting first character of every word in $s to uppercase </li></ul><ul><ul><li>$upper_word = ucwords ( $s ); </li></ul></ul>
  42. 42. String Comparison <ul><li>can use the operators </li></ul><ul><ul><li>< , <= , > , >= , == , != , === , !== </li></ul></ul><ul><ul><li>Note: Don't use == and != , use === and !== </li></ul></ul><ul><li>Unexpected results for mixed comparisons of numbers and strings </li></ul><?php $str1=‘99’;$str2=99; if ( $str1 === $str2 ) { echo &quot;Strings are equal ! &quot;; } else echo “Strings are not equal !”; ?>
  43. 43. Substrings & Replacement <ul><li>Substr (string, start) </li></ul><ul><ul><li>return substring of string beginning at index start and continuing to end of the string </li></ul></ul><ul><li>Substr (string, start, length) </li></ul><ul><ul><li>return substring of string beginning at index start and consisting of length characters </li></ul></ul><ul><li>There are also functions strrev, substr_replace </li></ul>
  44. 44. Examples of substr_replace <ul><li><?php $var = 'ABCDEFGH:/MNRPQR/'; echo &quot;Original: $var<hr /> &quot;; echo &quot;The length of the original is &quot;. strlen($var); </li></ul><ul><li>/* These two examples replace all of $var with 'bob'. */ echo substr_replace($var, 'bob', 0) . &quot;<br /> &quot;; echo substr_replace($var, 'bob', 0, strlen($var)) . &quot;<br /> &quot;; /* Insert 'bob' right at the beginning of $var. */ echo substr_replace($var, 'bob', 0, 0) . &quot;<br /> &quot;; /* These next two replace 'MNRPQR' in $var with 'bob'. */ echo substr_replace($var, 'bob', 10, -1) . &quot;<br /> &quot;; echo substr_replace($var, 'bob', -7, -1) . &quot;<br /> &quot;; /* Delete 'MNRPQR' from $var. */ echo substr_replace($var, '', 10, -1) . &quot;<br /> &quot;; ?> </li></ul>
  45. 45. Exploding a String <ul><li>explode (separator, string) </li></ul><ul><ul><li>extracts an array of substrings using the characters in separator as field separators </li></ul></ul><ul><li>Example </li></ul><?php $data = &quot;foo:*:1023:1000::/home/foo:/bin/sh&quot;; list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(&quot;:&quot;, $data); echo $user; // foo echo $pass; // * ?>
  46. 46. Imploding a String <ul><li>implode (separator, array) </li></ul><ul><ul><li>inverse of explode: makes a single string from the strings in an array using separator string between each string in the array. </li></ul></ul><ul><ul><li>Example </li></ul></ul><?php $array = array('lastname', 'email', 'phone'); $comma_separated = implode(&quot;,&quot;, $array); echo $comma_separated; // lastname,email,phone ?>
  47. 47. Two types of arrays <ul><li>Indexed arrays: </li></ul><ul><ul><li>the indices are 0, 1, 2, .... </li></ul></ul><ul><ul><li>these are like arrays in C, Java, or Perl </li></ul></ul><ul><li>Associative arrays: </li></ul><ul><ul><li>indices are strings (keys) </li></ul></ul><ul><ul><li>these are similar to hashes in Perl </li></ul></ul><ul><li>Any values can be stored in an array </li></ul>An array is a data structure that stores one or more values in a single value.
  48. 48. Indexed Arrays <ul><li>The general form for setting the key of an array equal to a value is: $array[ key ] = value ; </li></ul><ul><li>$employee_array[0] = &quot;Bob&quot;; </li></ul><ul><li>$employee_array[1] = &quot;Sally&quot;; </li></ul><ul><li>$employee_array[2] = &quot;Charlie&quot;; </li></ul><ul><li>$employee_array[3] = &quot;Clare&quot;; </li></ul><ul><li>In the above example we made use of the key / value structure of an array. The keys were the numbers we specified in the array and the values were the names of the employees. Each key of an array represents a value that we can manipulate and reference. </li></ul><ul><li>echo &quot;Two of my employees are $employee_array[0] and $employee_array[1]”; </li></ul>
  49. 49. Indexed Arrays <ul><li>Ranges: </li></ul><ul><li>$digits = range (0,9); </li></ul><ul><ul><li>$digits [0] is 0, </li></ul></ul><ul><ul><li>$digits [1] is 1, ... </li></ul></ul><ul><li>$letters = range ( 'a' , 'z' ); </li></ul><ul><ul><li>$letters [0] is 'a' , </li></ul></ul><ul><ul><li>$letters [1] is 'b' , ... </li></ul></ul>
  50. 50. Examples <ul><li><?php // array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) foreach (range(0, 12) as $number) {    echo $number .&quot; &quot;; } // The step parameter was introduced in 5.0.0 // array(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100) foreach (range(0, 100, 10) as $number) {    echo $number .&quot; &quot;; } // Use of character sequences introduced in 4.1.0 // array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'); foreach (range('a', 'i') as $letter) {    echo $letter .&quot; &quot;; } </li></ul><ul><li>// array('c', 'b', 'a'); foreach (range('c', 'a') as $letter) {    echo $letter .&quot; &quot;; } ?> </li></ul>
  51. 51. Associative Arrays <ul><li>In an associative array a key is associated with a value. </li></ul><ul><li>array can be used to initialize arrays with string indices. We use operator => . The value left of the operator is the array index and the value to the right is the element’s value. </li></ul>$product  = array( 'id'  =>   123 ,  'desc'  =>   null ,  'price'  =>  549 .99 );
  52. 52. Associative Arrays <ul><li>An associative array is a table of key-value pairs. Here the names are the keys and the values are the ages. </li></ul>$age = array( 'Fred' => 37, 'Gord' => 23, 'Alice' => 17, 'Bob' => 23 ); echo &quot;Fred's age is &quot; , $age ['Fred']; $age [ 'Fred' ] = 65; // Fred is now a senior echo &quot;<br>Fred's age is {$age['Fred']}&quot; ; Note braces needed for interpolation
  53. 53. Sorting Arrays <ul><li>sort(array) & rsort(array) </li></ul><ul><ul><li>sort array in ascending alphabetical or numerical order or reverse alphabetical or numerical order </li></ul></ul><?php $names = array ( 'Fred' , 'Ted' , 'Barney' , 'Gord' ); sort ($names); // 'Barney','Fred','Gord','Ted' rsort ($names); // 'Ted','Gord','Fred','Barney‘; ?>
  54. 54. Example <ul><li><?php </li></ul><ul><li>$names = array('Fred', 'Ted', 'Barney', 'Gord'); </li></ul><ul><li>sort($names); //or rsort($names); </li></ul><ul><li>foreach($names as $elements=>$names) </li></ul><ul><li>echo &quot; &quot;.$names; </li></ul><ul><li>?> </li></ul><ul><li>*Also, try to change the $elements to $names, you will get the number of elements in the array. </li></ul>
  55. 55. User-defined Functions <ul><li>User defined functions have the form </li></ul>function name ($ argument, $ argument ) { statement1; statement2; }
  56. 56. A max Function <ul><li>Return the max of two numeric values </li></ul>function max2 ( $a , $b ) { $a=3; $b=1; if ( $a > $b ) return $a; return $b; } echo “Max of 2 and 3 is &quot; , max2 ( );
  57. 57. Reserved Words in PHP for E_ALL elseif class FALSE endwhile else cfunction extends endswitch echo( ) case exit( ) endif die( ) break eval endforeach do $argc E_WARNING endfor default as E_ERROR enddeclare declare $argv E_PARSE empty( ) continue and
  58. 58. Reserved Words in PHP print( ) not $HTTP_ENV_VARS PHP_VERSION new $HTTP_POST_FILES $PHP_SELF list( ) $HTTP_POST_VARS PHP_OS include_once( ) $HTTP_GET_VARS parent include( ) $HTTP_COOKIE_VARS or if global old_function $HTTP_SERVER_VARS function NULL $HTTP_COOKIE_VARS foreach
  59. 59. Reserved Words in PHP _wakeup TRUE _sleep $this _LINE_ stdclass _FILE_ switch while static virtual( ) return( ) xor require_once( ) var require( )
  60. 60. <ul><li>THE END </li></ul><ul><li>SEE YOU NEXT SESSION!  </li></ul>

×