PHP Reference Book Blog

PHP Reference: Beginner to Intermediate PHP5

Archive for January, 2009

Jan-13-09

Print_r optional parameter correction

posted by Mario Lurig

(pg. 38)

In the book, the print_r() function is described as follows:

print_r($variable)

Output the contents of $variable . Typically used to display the contents of an array.

However, there is also an optional flag for the print_r() function that makes the function return the output rather than printing it to the screen. Therefore, the section of the book should be amended as follows:

print_r($variable [,return])

return – [optional] $boolean default: FALSE, print output
Output the contents of $variable. Typically used to display the contents of an array. If return is set to TRUE, print_r() returns the output rather than displaying it, such as when storing it in a variable.

Tags:
Jan-12-09

Display a Query String Value on a Web Page

posted by Mario Lurig

It seems simple, almost basic, but many people want to display the contents of a query string on a page for the user or just for troubleshooting purposes. Everyone does it, everyone needs to do it, and there are a few different options I’m going to introduce below. First, our sample URL:

http://www.phpreferencebook.com/?variable=value&name=Mario%20Lurig&gender=male%21

or

http://www.phpreferencebook.com/?variable=value&name=Mario Lurig&gender=male!
  1. Display the whole string (everything after the question mark)
    • <?php echo $_SERVER['QUERY_STRING']; ?>
      • variable=value&name=Mario%20Lurig&gender=male
  2. Display the whole string decoded (convert %## to original characters)
    • <?php echo urldecode($_SERVER['QUERY_STRING']); ?>
      • variable=value&name=Mario Lurig&gender=male!
  3. Show each variable and value as an array (already decoded)
    • <?php print_r($_GET); ?>
      • Array ( [variable] => value [name] => Mario Lurig [gender] => male!
    • <?php echo '<pre>'; print_r($_GET); echo '< /pre>'; ?>
      • Array
        (
            [variable] => value
            [name] => Mario Lurig
            [gender] => male!
        )
        

There are lots of options from there, so don’t let this be the end of your exploration of how to display a query string value on a page. If you have any great tricks, feel free to share them in the comments below!

Jan-2-09

Control Structures Without Curly Braces

posted by Mario Lurig

In the book I cover the standard syntax methods for if, elseif, else, while, do-while, switch, for, and foreach. Here is the if statement syntax:

if (expr) {
// If expr is TRUE,  do this, then exit the IF loop
}elseif (expr2) {
// If expr is FALSE, and expr2 is TRUE, do this, then exit the loop
}else{
// If all expr's are FALSE, do this, then exit
}

While accurate, there is a shorthand method that does not involve the curly bracket. If there is only a single evaluation, you can remove the curly braces completely. The following two examples are both completely valid syntax for control structures:

if ($a == 1) echo $a;
 
if ($a == 1) {echo $a; $a=2;}

Now, I still recommend using curly brackets, if only for clarity and consistency (excluding them for the second line above would make your life much harder than necessary, but I thought a clarification was in order. For more information on Control Structions, check out chapter 3, pages 25-32 of the free PHP book PDF.

Jan-1-09

mysql_result() correction

posted by Mario Lurig

(pg. 123)

The function has the following correct syntax:

mysql_result(resource, row[, column])

However, in the explanation paragraph, 2nd sentence, it uses the term field erroneously instead of the term column. It should read as follows:

If column is specified instead of the value of the
first column, the specified column is retrieved (can be referenced by number
starting with 0 or by name/alias).

It’s the little things, like changing the term used to be clearer to the reader and forgetting to update everything.