|
home—lects—exams—hws
D2L—breeze (snow day)
From PHP Visual Quickstart Guide by Larry Ullman
Originally based on notes by Jack Davis (jcdavis@radford.edu)
<?php ini_set('display_errors', 'stderr'); ini_set('display_startup_errors', true); error_reporting(E_ALL|E_STRICT); ?> |
php.radford.eduis running php 5.3.
By default, using an uninitialized/undeclared variable is not an error!
(This is unfortunate, since it means misspelling a variable-name
turns into a hard-to-track bug.)
We'll see later how to configure php (or a web server using php)
to emit a warning when using uninitialized variables;
you want to always have that as a default.
round(4.35) == 4 round(4.289,2) == 4.29 $num = 236.26985; round ($num) == 236 |
<?php echo "hello there everybody!"; echo ' -- Dr. Nick '; ?> |
$str = "How are you?"; $first_name = "John"; $str = "How are you " . $first_name . "?"; $str = "How are you $first_name?"; // alternately $date = "1/01/2010"; // this is a string! $str2 = "I said, \"how are you?\""; |
if (!$fname) { // We reach here if $fname was undefined *or* the empty string. echo "No first name provided."; … } |
$dbConnection = |
$arr = array("john","mary","zimbo"); echo "index 1 contains ", $arr[1], "\n"; // "mary" $arr[] = "betty"; // Insert at the *next* location. echo "index 3 contains ", $arr[3], "\n"; // "betty" $arr[23] = "justine"; echo "index 17 contains ", $arr[17], "\n"; // NULL echo "index 23 contains ", $arr[23], "\n"; // "justine" $arr[] = "rachel"; // Wait, what counts as the *next* location now? 24, not 4 (!). echo "index 4 contains ", $arr[ 4], "\n"; // NULL echo "index 24 contains ", $arr[24], "\n"; // "rachel" |
$crsarr = array("web"=>325, "java1"=>120); |
print_r( $crsarr ); print_r( array(20, 21, "howdy" => "bye", "aloha" => "sayonara", 2 => 22 ) ); |
function sumLengths( $data ) { foreach($arr AS $x) { $lenSoFar += strlen($x); } return $lenSoFar; } |
define ("PI",3.14152926535); echo "The constant PI holds ", PI, "<br/>\n"; |
number_format(428.4959,2) == '428.50' number_format(428,2) == '428.00' number_format(123456789) == '123,456,789' |
2 Well, “just like in other Algol-based syntaxes.” ↩
3 Well, “reasonably fully parenthesize” — the precedence rules are designed to make operators like function call, array-lookup, field-lookup etc. work as expected. Precedence between arithmetic operators isn't the true purpose of precedence rules — they're so that you can let the other operators have a convenient, easy-to-read, easy-to-write syntax that doesn't get in the way of what you're trying to write. ↩
4
So
Bottom line: use
home—lects—exams—hws
D2L—breeze (snow day)
©2012, Ian Barland, Radford University Last modified 2012.Sep.09 (Sun) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |