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.
print
for scalar (single-valued) variables, and print_r()
for complex variables (arrays and objects).input type="hidden"
.GET
to request info from the server; use POST
to trigger a server-side action. GET
sends info via URL; POST
sends it invisibly.number_format()
rounds and formats.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.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.foreach
loop is used to iterate through an array.implode()
turns an array into a string. join() is synonymous.explode()
turns a string into an array.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.define()
.if ($_SERVER['REQUEST_METHOD'] == 'POST')
{.header()
controls HTTP headers, and can be used to redirect to another page.setcookie()
sends a cookie.sesion_start()
starts a session.$_SESSION = array()
, then session_destroy()
. To delete an individual session value, use unset()
.global
makes a variable global, usable outside the function.@
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.move_uploaded_file()
moves an uploaded file from the temp directory.md5()
creates a hash.fgets()
and fgetscsv()
read files incrementally.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.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.TRUNCATE
rather than DELETE FROM
to empty a table.mysqli_affected_rows()
returns the number of records altered.LIMIT 1
to UPDATE
queries to affect only 1 row.