|
home—lects—hws
D2L—breeze (snow day)
From PHP Visual Quickstart Guide by Larry Ullman
Originally based on notes by Jack Davis (jcdavis@radford.edu)
If you use a variable that hasn't been initialized (whether intentional or not), that variable is created and has the value NULL.
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.)
rule-of-thumb: In all php files, include (near the start)error_reporting(E_ALL); since you want to know when you are using an undeclared variable. (Later, we'll put this command and a few others into a file startup.php, and then all our files will start withrequire_once('startup.php'); .
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."; … } if (!$_POST['fname']) { // If this program is handling a web-form submitted via a 'POST' action: echo "On a web-form, the tag with |
$dbConnection = |
define ("PI",3.14152926535); echo "The constant PI holds ", PI, "<br/>\n"; |
One useful superglobal is the array
1 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. ↩
2 A similar term is “transclusion”, including one document inside another, like ↩
3
So
Bottom line: use
4 In php, a "global" variable is one that can be used anywhere, but to use it you must first explicitly import it inside a function:
$n = 17 function f($x) { global $n; // this line required to use $n inside this function. return $x + $n; } |
home—lects—hws
D2L—breeze (snow day)
©2014, Ian Barland, Radford University Last modified 2015.Feb.09 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |