Download presentation
Presentation is loading. Please wait.
Published byElfrieda Wheeler Modified over 9 years ago
1
PHP - Basic Language Constructs CSCI 297 Scripting Languages - Day Two
2
Topics for Today 1.Comments 2.Variables 3.Output and Input 4.Math Operators and Logic Operators 5.Selection 6.Iteration 7.Functions and Parameters
3
Comments PHP: Same as C++ // this is a comment /* all of this is comments */ HTML Comment Tag
4
Variables $Variable Names start with $, no weird characters, yadda yadda No need to declare variables o Warning: a compiler can act as a helpful spell checker. But PHP will not find this error: $aardvark = 15; if ($aardvrak < 50) Weak Typing o 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 " \n";
6
Output Outputting several lines: <<< echo <<<theEnd theEnd;
7
Input 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. o file IO is in 2 weeks o database IO is 2 nd half of course Browser Web Server PHP Script 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; yadda yadda; endif 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. " "; }
13
Functions 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 Notice: No types
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 = Tue Aug 27th see table 21.1 on page 470 for date() formatting codes Note that time() returns seconds since 1970.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.