Review: PHP for the Web: Visual QuickStart Guide by Larry Ullman

PHP for the Web: Visual QuickStart Guide by Larry Ullman

My rating: 3 of 5 stars

This book introduces PHP through well-explained, realistic examples. After exploring the basic concepts and syntax of PHP, it shows how to work with files and databases. It’s not as straightforward as Head First PHP & MySQL, but it definitely covers a lot more ground. Each chapter ends with several tips (usually best practices) and suggestions for digging deeper.

Variables

  • Use print for scalar (single-valued) variables, and print_r() for complex variables (arrays and objects).
  • Items in single quotes are treated literally; items in double quotes are extrapolated (processed).

HTML Forms and PHP

  • In forms, pass preset values using input type="hidden".
  • Use GET to request info from the server; use POST to trigger a server-side action. GET sends info via URL; POST sends it invisibly.

Using Numbers

  • number_format() rounds and formats.

Using Strings

  • nl2br() converts newlines to break tags.
  • htmlentities(), htmlspecialchars(), and strip_tags() make user-provided data safe to print to the browser.
  • urlencode() encodes a string so it can be passed in a URL.
  • trim() removes whitespace from the beginning and end of a string.

Control Structures

  • empty() returns TRUE if a variable has no value, value of 0, or value of FALSE. isset() returns TRUE if a variable has any value, including 0, FALSE, or the empty string.
  • break exits the current structure.
  • continue terminates the current iteration of the loop and checks the loop condition again.
  • exit and die terminate execution.
  • for loop performs specific statements for a determined number of iterations.
  • while loop continues as long as the condition is TRUE; most often used to retrieve from a database.
  • do...while loop is same as while loop, but guarantees that statements are executed at least once.

Using Arrays

  • foreach loop is used to iterate through an array.
  • Wrap array constructs in curly brackets to avoid problems with quotation marks.
  • There are several sorting functions to get by values or keys. Pay attention to maintaining key-value association if necessary.
  • implode() turns an array into a string. join() is synonymous.
  • explode() turns a string into an array.

Creating Web Applications

  • Include files with include(), which warns but continues in case of errors, or require(), which terminates execution in case of errors. Generally avoid include_once() and require_once(), which adversely affect performance.
  • Constants have unchanging values. Names are uppercase, with no initial $. Assign values with define().
  • To test whether a form has been submitted, use if ($_SERVER['REQUEST_METHOD'] == 'POST') {.
  • header() controls HTTP headers, and can be used to redirect to another page.

Cookies and Sessions

  • setcookie() sends a cookie.
  • Delete a cookie by sending a cookie with the cookie’s name and a value of FALSE.
  • Use cookies when security isn’t a concern, and for < 4 KB of data. Use sessions for more security and more data.
  • sesion_start() starts a session.
  • Delete a session with $_SESSION = array(), then session_destroy(). To delete an individual session value, use unset().

Creating Functions

  • In functions, arguments with default values must come after arguments without default values.
  • global makes a variable global, usable outside the function.
  • Put an @ in front of a function to suppress errors, notices, and warnings.
  • file_put_contents() opens and writes a file.
  • file() returns file contents as an array.

Files and Directories

  • Put writable files and directories outside of the Web root directory, for security.
  • move_uploaded_file() moves an uploaded file from the temp directory.
  • md5() creates a hash.
  • fgets() and fgetscsv() read files incrementally.

Intro to Databases

  • mysqli_query() sends a SQL command to MySQL.
  • mysqli_connect() opens a connection to MySQL; mysqli_close() closes it.
  • mysqli_select_db selects the database.
  • Don’t use die() when a database error occurs; it’s too heavy-handed.
  • mysqli_real_escape_string() escapes potentially harmful characters.
  • mysqli_fetch_array() fetches 1 row at a time.
  • Use TRUNCATE rather than DELETE FROM to empty a table.
  • mysqli_affected_rows() returns the number of records altered.
  • Add LIMIT 1 to UPDATE queries to affect only 1 row.

PHP Learning Resources

Filed Under: 
Tagged With: ,

Want tips to rocket-boost your website?

Simply sign up.
Ready to Blast Off?

Let's talk.

Contact OptimWise
crossmenuarrow-right