Kevin Yank’s done it again!*He’s just completed a new 2012 edition of his*best-selling book (Build Your Own Database Driven Web Site, 4th Edition) which is now titled -*PHP & MySQL: Novice to Ninja. And, today you can:

  • Take a sneak peek inside the book (with an excerpt from the Chapter 3 -Introducing PHP)
  • Place a pre-order for the book, SAVE up to 50%, and also receive a*FREE: PHP Poster and 1-month Learnable membership, as part of our Early-Bird BONUS (see the details at the end of the post).
Now let’s jump into the excerpt from the book. Over to you Kev …

Introducing PHP

PHP is a server-side language. This concept may be a little difficult to grasp, especially if you’ve only ever designed websites using client-side languages like HTML, CSS, and JavaScript.

A server-side language is similar to JavaScript in that it allows you to embed little

programs (scripts) into the HTML code of a web page. When executed, these programs give you greater control over what appears in the browser window than HTML alone can provide. The key difference between JavaScript and PHP is the stage of loading the web page at which these embedded programs are executed.

Client-side languages like JavaScript are read and executed by the web browser after downloading the web page (embedded programs and all) from the web server. In contrast, server-side languages like PHP are run by the web server, before sending the web page to the browser. Whereas client-side languages give you control over how a page behaves once it’s displayed by the browser, server-side languages let you generate customized pages on the fly before they’re even sent to the browser.

Once the web server has executed the PHP code embedded in a web page, the result takes the place of the PHP code in the page. All the browser sees is standard HTML code when it receives the page, hence the name “server-side language.” Let’s look back at the today.php example presented in Chapter 1:

Today’s Date Today’s date (according to this web server) is

Most of this is plain HTML except the line between is PHP code.* marks its end. The web*server is asked to interpret everything between these two delimiters and convert it*to regular HTML code before it sends the web page to the requesting browser. The*browser is presented with the following:

Notice that all signs of the PHP code have disappeared. In its place the output of*the script has appeared, and it looks just like standard HTML. This example demonstrates several advantages of server-side scripting:

Today’s Date Today’s date (according to this web server) is Sunday, April 1st 2012.

Notice that all signs of the PHP code have disappeared. In its place the output of*the script has appeared, and it looks just like standard HTML. This example*demonstrates several advantages of server-side scripting:

No browser compatibility issues

PHP scripts are interpreted by the web server alone, so there’s no need to worry*about whether the language features you’re using are supported by the visitor’s*browser.

Access to server-side resources

In the above example, we placed the date according to the web server into the web page. If we had inserted the date using JavaScript, we’d only be able to display the date according to the computer on which the web browser was running. Granted, there are more impressive examples of the exploitation of server-side resources, such as inserting content pulled out of a MySQL database*(hint, hint …).

Reduced load on the client

JavaScript can delay the display of a web page significantly (especially on mobile devices!), as the browser must run the script before it can display the web page. With server-side code this burden is passed to the web server, which you can make as beefy as your application requires (and your wallet can afford).

Basic Syntax and Statements

PHP syntax will be very familiar to anyone with an understanding of JavaScript, C, C++, C#, Objective-C, Java, Perl, or any other C-derived language. But if these languages are unfamiliar to you, or if you’re new to programming in general, there’s no need to worry about it.

A PHP script consists of a series of commands, or statements. Each statement is an instruction that must be followed by the web server before it can proceed to the next instruction. PHP statements, like those in the aforementioned languages, are always terminated by a semicolon (.

This is a typical PHP statement:

echo 'This is a test!';This is an echo statement, which is used to generate content (usually HTML code) to send to the browser. An echo statement simply takes the text it’s given and inserts it into the page’s HTML code at the position of the PHP script where it was contained.

In this case, we’ve supplied a string of text to be output: ‘This is a*test!’. Notice that the string of text contains HTML tags ( and ), which is perfectly acceptable. So, if we take this statement and put it into a complete web page, here’s the resulting code:

Today’s Date

If you place this file on your web server and then request it using a web browser, your browser will receive this HTML code:

Today’s Date This is a test!

The today.php example we looked at earlier contained a slightly more complex echo statement:

echo date('l, F jS Y.');Instead of giving echo a simple string of text to output, this statement invokes a built-in function called date and passes it a string of text: ‘l, F jS Y.’. You can think of built-in functions as tasks that PHP knows how to do without you needing to spell out the details. PHP has many built-in functions that let you do everything, from sending email to working with information stored in various types of databases.

When you invoke a function in PHP—that is, ask it to do its job—you’re said to be calling that function. Most functions return a value when they’re called; PHP then behaves as if you’d actually just typed that returned value instead in your code. In this case, our echo statement contains a call to the date function, which returns the current date as a string of text (the format of which is specified by the text string in the function call). The echo statement therefore outputs the value returned by the function call.

You may wonder why we need to surround the string of text with both parentheses ((…)) and single quotes (‘…’). As in SQL, quotes are used in PHP to mark the beginning and end of strings of text, so it makes sense for them to be there. The parentheses serve two purposes. First, they indicate that date is a function that you want to call. Second, they mark the beginning and end of a list of arguments that you wish to provide, in order to tell the function what you want it to do.

In the case of the date function, you need to provide a string of text that describes the format in which you want the date to appear. Later on, we’ll look at functions that take more than one argument, and we’ll separate those arguments with commas. We’ll also consider functions that take no arguments at all. These functions will still need the parentheses, even though there will be nothing to type between them.

Fantastic, thanks Kevin for sharing that!

The good news is there’s loads more practical and hands-on examples just like this in the 500+ pages of the book. Which covers:

  • Tutorials: Easy-to-follow tutorials with downloadable code examples
  • Installation: Instructions for PHP & MySQL on Windows, Mac OS X, and Linux
  • PHP coding: Learning the correct PHP syntax
  • Database design: Mastering SQL database design
  • Object Oriented Programming (OOP): Crash course in OOP principles.
  • Building a CMS: Step-by-step guide to creating a CMS.
  • Shopping Carts: Developing an e-commerce cart from scratch.
  • Latest technologies: Updated for Windows 7, Mac OS X Lion, and the latest versions of PHP, MySQL, phpMyAdmin, XAMPP, MAMP, and HTML5.
As mentioned at the start of the post, you have the chance to pre-order this book:

If *you place your pre-order before the 30th of April 2012,*you’ll also receive this FREE Early-bird Bonus:

  • 1x Digital PHP poster – a downloadable copy of our brand new PHP reference poster
  • 1x Month Learnable membership – to access their library of online web dev courses
… and if you’re one of the first 999 customers to order the print book + epack bundle*(for $34), we’ll also post you your very own printed copy of the new PHP poster. But hurry, as the posters are disappearing fast.

Quick one before we go. Remember, as this is a pre-order offer: epacks can be downloaded from SitePoint from the 1st May, 2012; printed book + poster will be shipped on the 8th May, 2012; but the bonus digital PHP poster can be downloaded right away.

That’s it. Enjoy, and as always, we’d love to hear all your thoughts and comments.