160 likes | 313 Views
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
E N D
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 is comments */ HTML Comment Tag <!-- This is a comment -->
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;
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";
Output Outputting several lines: echo <<<theEnd <table rules=all> <TR> <TD align=center> theEnd;
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
Operators for Math and Logic same as C++ $A = $B + $C; $A++; $A += $B; if ($A == $B) if ($A != $B && $A != $C)
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))
Selection (conditional statements) same as C++, mostly if (...) { ... } else { ... } if (…) … elseif (…) … else … if (...): blah blah; yaddayadda; endif;
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;
Iteration (loops) while loops and for loops are the same as C++ for ($A = 1; $A <= 10; $A++) { echo $A . "<br>"; }
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
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];
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
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.