Review: PHP & MySQL: The Missing Manual

PHP & MySQL: The Missing Manual by Brett McLaughlin

My rating: 3 of 5 stars

This intro to PHP was informative but not as engaging as Head First PHP & MySQL. It's written for those who know HTML, CSS, and some JavaScript. It uses a basic web app as the example project throughout the book, and each section builds on the previous ones. It covers a few PHP best practices, but not as many as I would've liked.

I read this book because as a WordPress web designer, I use PHP to customize WordPress themes and understand how plugins work.

PHP Syntax

  • $_REQUEST is an array that holds data entered into a web form by a user.
  • Wrap a variable in {} to use it within a string. This doesn’t work for constants.
  • Print new lines with n.
  • === denotes identity, meaning that not only do the two values evaluate to equal values, they are also of the same data type.
  • PHP and SQL prefer underscores over capitalization in variable names.
  • $_FILES holds data about a file uploaded by a user.
  • Put @ directly before a function to suppress errors.
  • Reference an object's method with ->; the syntax is $object -> method().
  • Use a heredoc to mark a piece of text. Start with <<<EOD and end with EOD (on its own line, with no space before).
  • Retrieve cookie values with the $_COOKIE superglobal.
  • Use the $_SESSION superglobal when dealing with sessions.

PHP functions

  • trim removes whitespace; rtrim removes whitespace to the right; ltrim removes whitespace to the left.
  • Use preg_match to test if a regex occurs and preg_match_all to count all occurrences.
  • In most cases you want to require, not include, because you need the file to run. Use require_once unless you truly need to require something multiple times.
  • header sends a raw HTTP header to the browser. It must be called before any other output. header("Location: path") changes location to the specified path.
  • is_uploaded_file ensures that a file name references a file uploaded with HTTP.
  • getimagesize returns an array of info about an image, including MIME type, height, and width.
  • sprintf prints to a string. You construct a string using any calculations you need and pass info to sprintf.
  • is_null tells if something lacks a value.
  • crypt does one-way encryption.
  • setcookie sets a cookie.
  • empty evaluates PHP types and returns true if empty, false if not.
  • session_start starts a session.

PHP best practices

  • Even though MySQL records only take as much space as the stored data needs, it's still good to use the proper types and lengths because they provide info about what goes in each column.
  • All things being equal, it's better to store media on a file server and store the path in the database.
  • Try to keep the bulk of PHP at the beginning of a script, then just insert data into HTML below it.
  • Make as few function calls as you need. Use small functions with groupings or higher-level functions that assemble small functions in useful ways. Make simple calls rather than many calls.
  • To log a user out, set the cookie's value to empty and expiration to a date in the past.
  • There's no functional difference between $_REQUEST, $_GET, and $_POST in terms of getting request info. So, use the one that will be most clear and specific in a certain case. Use $_POST when you can, $_GET when you're getting a GET request, and $_COOKIE when you're looking for a cookie.
  • Sessions are generally preferable to cookies, but you might use cookies when you need data that persists or store data that's not sensitive.
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