PHP – Lesson 01: Getting Started With PHP



What is PHP? (an overview)

PHP Defined

  1. PHP stands for: Hypertext Preprocessor (originally stood for “Personal Home Page”)
  2. PHP is a server-side scripting language (dynamic language), like Microsoft’s ASP.NET
  3. PHP scripts are executed on the server, unlike Javascript, which is executed on the client’s (user’s) local computer.
  4. PHP supports many databases (we will use MySQL in this course)
  5. PHP is an open source software and is free to download and use

Why use PHP?

  1. Because PHP scripts are executed on the server, users do not necessarily have to have javascript, ActiveX, or other potentially harmful user-end engines to parse scripts. (Note: it is common, however, to use PHP in conjunction with javascript in pages for different reasons).
  2. Because the server is parsing the info, it can access database information and will generate the same output on everyone’s computers.
  3. PHP is free and relatively easy to learn compared to other dynamic languages, like ASP (Active Server Pages — Microsoft), or JSP (Java Server Pages — Sun Microsystems).
  4. PHP is can be run on any computing platform (Mac OS, Windows, Unix, Linux)
  5. Because PHP can substitue variables for text and numbers (“strings” and “integers”), you can design a site with far less repetition than with plain HTML. PHP allows you to make dynamic templates to process user requests in forms, page navigation, and page content.


Your Work Environment

Choosing a good script editor

There are a variety of script editors out there, but it is important to pick one that meets the criteria below, or else you will SUFFER from typos! ….

  • Has a line numbering feature so you can easily find errors
  • Has a “balance braces” feature (for finding open-and-closing braces: { } , ( ), [ ] ) (optional but extremely useful)
  • PHP syntax coloring
  • PHP code hints (optional but extremely useful)

Some recommended script editors

  • Dreamweaver (for purchase with Adobe Creative Cloud or 30-day trial, cross-platform)
  • Komodo Edit (free and cross-platform Windows and Mac)
  • Sublime Text  (OSX, Windows, Linux) evaluation copy free. As of 6/28/15 it does not have a time-out period.
  • Notepad++ (Windows only) – free

Setting up your personal computer as a webserver

Because PHP is a server-side language, testing your webpages is not the same or as easy as testing HTML pages as you work on them. You must have ONE of the following to test dynamic pages:

  • Install a webservice engine (Apache, IIS, etc.) and the PHP parser engine on your local computer. (For simplicity, I recommend installing a developer testing package with a binary installer so you don’t have to climb the web server learning curve to set your service up. I recommend an installation that includes the GD Libraries, which does NOT come with a basic PHP installation. Windows, Mac, and Linux users can install an Apache/MySQL/PHP/PERL package like XAMPP since it is much easier to install and uninstall, rather than dealing with the installation of individual components like Apache, PHP, special PHP libraries, and MySQL.
  • Use a remote testing server via your script editor software

*You can watch my detailed tutorials on how to set your computer up as a server for this course. Please take precautions to back up your files first!


Using PHP and HTML

Not all HTML starts out as HTML

To understand how PHP works you need to know which server engines parse which file extensions. The following things are true about PHP and HTML:

  • PHP code can only be recognized by the server if the file extension ends in “.php
  • HTML code can be recognized in files ending in “.php” or “.html” (as well as others).
  • The server’s web service (Apache, IIS, etc.) handles .html files, and when the web service comes across a .php file, it invokes the PHP engine to parse anything in between the opening and closing PHP tags: <?php and ?>
  • You can mix HTML and PHP code within the same page as long as the file extension is .php because PHP is an “embedded language”
  • You can generate HTML from a PHP script, and all the end user’s browser will see is the digested HTML that your server’s PHP Engine returns to the browser, as follows: php engine parsing php and exhibiting HTML on client side


The Semicolon

Ending your PHP commands

A command (also called a statement) in PHP should end in a semicolon. It helps the server understand that your command is complete and that it is ready for the next thing. If you forget the semicolon, the php page will sometimes return an error in the browser (but not always). Here’s an easy-to-understand example in quasi-code lay terms:

<?php
do something;
now do something else;
?>

Here’s a real example:

<?php
echo "John ";
echo "Doe";
?>

This outputs “John Doe” to the browser.

Commenting Your Notes

What is a comment and why use it?

A comment in PHP is like a comment in HTML in that it is intended to add notes to the programmer or editor of the page for clarity without having the server’s parsing engine display it to the end user. Some specific uses are:

  • Explain what the script does and/or how it works. This is helpful to other’s who may have to work on your code later or even to you when you haven’t looked at the code for a long time.
  • Insert a placeholder for code you will add later.
  • Disable a section of code temporarily for troubleshooting problems.

Three ways to comment

There are three ways to comment in PHP: 2 types of single-line comments, and 1 type of multiline comments:

  • Single line, double forward slashes
    //This is a comment to be ignored by PHP Engine
  • Single line, pound symbol
    #This is a comment to be ignored by PHP Engine
    Example:
    ########################
    ## Navigation Section ##
    ########################
  • Multiline, uses /* something here */
    Example:
    /* This is intended for a much longer
    explanation. It is also the most common
    to use in commenting out troublesome
    blocks of code for troubleshooting. */

Video Version of This Page’s Notes

View in larger window.