1 / 16

PHP - Basic Language Constructs

PHP - Basic Language Constructs. CSCI 297 Scripting Languages - Day Two . Topics for Today. Comments Variables Output and Input Math Operators and Logic Operators Selection Iteration Functions and Parameters. Comments. PHP: Same as C++ // this is a comment /* all of this

kimama
Download Presentation

PHP - Basic Language Constructs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP - Basic Language Constructs CSCI 297 Scripting Languages - Day Two

  2. Topics for Today • Comments • Variables • Output and Input • Math Operators and Logic Operators • Selection • Iteration • Functions and Parameters

  3. Comments PHP: Same as C++ // this is a comment /* all of this is comments */ HTML Comment Tag <!-- This is a comment -->

  4. Variables • Variable Names start with $, no weird characters, yaddayadda • No need to declare variables • Warning: a compiler can act as a helpful spell checker. But PHP will not find this error: $aardvark = 15; if ($aardvrak < 50) • Weak Typing • So, this is okay $bob = 0.0; $bob = "Hello World"; • Type Casting (same as C++) $bob = (float) $joe;

  5. Output The equivalent of C++'s cout is echo. Output variable contents: echo $cnt; Output a string: echo "Hello World"; Concatenate two items: echo "Hello " . $name; There is no endln: echo "<P> \n";

  6. Output Outputting several lines: echo <<<theEnd <table rules=all> <TR> <TD align=center> theEnd;

  7. Browser Web Server Input PHP Script Direct Input is Not Applicable • PHP is for writing server scripts, not a GUI. • Data is passed to your script via variables from the web server process. • Data may also be inside a file or a database. • file IO is in 2 weeks • database IO is 2nd half of course MySQL

  8. Operators for Math and Logic same as C++ $A = $B + $C; $A++; $A += $B; if ($A == $B) if ($A != $B && $A != $C)

  9. Misc Operators $files = `ls`; // directory listing into variable define ('MAX', 100); // constant exit; // kill the script if (is_numeric ($input)) // plus thousands more if (is_null ($input))

  10. Selection (conditional statements) same as C++, mostly if (...) { ... } else { ... } if (…) … elseif (…) … else … if (...): blah blah; yaddayadda; endif;

  11. Selection switch ( $option ) { case "A": ... ... ... break; case "B": ... default: } // C = larger of A and B $C = ($A > $B) ? $A : $B; That "ternary" operator is the same as: if ($A > $B) $C = $A; else $C = $B;

  12. Iteration (loops) while loops and for loops are the same as C++ for ($A = 1; $A <= 10; $A++) { echo $A . "<br>"; }

  13. Functions Notice: No types Returning One Value function fix_cap_lock ($string1, $string2) { $string1 = ucfirst(strtolower($string1)); $string2 = ucfirst(strtolower($string2)); return $string1 . " " . $string2; } $A = "hElLo"; $B = "WORlD"; echo fix_cap_lock ($A, $B); //prints Hello World

  14. Functions Returning an Array function fix_cap_lock ($string1, $string2) { $string1 = ucfirst(strtolower($string1)); $string2 = ucfirst(strtolower($string2)); return array ($string1, $string2); } $A = "hElLo"; $B = "WORlD"; $C = fix_cap_lock ($A, $B); echo $C[0] . " " . $C[1];

  15. Functions Pass by Reference function fix_cap_lock (&$string1, &$string2) { $string1 = ucfirst(strtolower($string1)); $string2 = ucfirst(strtolower($string2)); } $A = "HeLlO"; $B = "WORlD"; fix_cap_lock ($A, $B); echo $A . " " . $B; // prints Hello World

  16. Homework Write a for loop that will print the next seven dates, starting with today. • Format = TueAug 27th see table 21.1 on page 470 for date() formatting codes • Note that time() returns seconds since 1970.

More Related