Fred Brack   Fred Brack  
Raleigh, NC
Picture of the Cape Hatteras lighthouse

Introduction to PHP
by Fred Brack
Updated

It's February 2021, and I need to learn a little about PHP, the server-side scripting language that runs on most servers supporting web pages.  My motivation for learning PHP was the need to create an HTML form from which I would gather visitor input and then send a message to myself.  An example of this would be a comment form on a website.  I document this as Using PHP to Send an Email from an HTML Form.

I'm documenting what I learn here for both myself and anyone else who might want an introduction to the language.  That's just what I do ...

PHP is an acronym that is oddly self-inclusive, as it stands for "PHP: Hypertext Preprocessor."  It runs on the server that supports your web page.  Well, it can run there; you'll have to test it or ask to see if it is available to you, but it probably is.  Test your own system for PHP support by clicking here.

The Basics

Variables

Variable Scope

Variable Data Types

PHP supports the following data types:

Echo and Print

The echo and print keywords are nearly identical.  They both output data to the screen (e.g., typically in the middle your HTML screen presentation).  Because of the similarity, we will only discuss echo.

There's not much to discuss!  The operand of echo can be straight text, text with HTML tags, or a calculation or other PHP statement.  Parentheses are optional.  Examples:

Note the use of the concatenation character (a period surrounded by blanks).  Also note our use of "<br>".  Without it, no new line break would occur between uses of echo.

String Functions

There are, of course, lots of built-in string functions.  You can consult this PHP String Reference to see all of them.  Here are some common examples:

There are many interesting string functions, including ones that:

Date and Time

PHP offers extensive date and time functions, so we are only going to give one example of the most common format here, and you can consult the w3schools Date and Time Functions Reference page for details.  Be sure to click on the date() function by itself on that page to see the myriad of options (like the l M j below, which don't exactly jump out in their meaning)!

  date_default_timezone_set("America/New_York"); // this sets your local time zone
  date("l M j\, Y") . " at " . date("h:i a")     // = (for example) "Friday Feb 12, 2021 at 11:00 am"

It is worth noting that PHP, like JavaScript and other languages, has an "escape" character, the backslash \, which means, "take the following character literally."  So the comma in the operand to the date function is not separating two operands but is rather to be written out as a comma, which you see above after the 12.  There is no confusion about the colon in the second date function, however, so it doesn't need the escape character.

Math Functions

PHP offers a variety of math functions.  You can view the complete list at PHP Math Reference.  They  include functions like:

Miscellaneous Functions

Some of the miscellaneous functions available are as follows.  See the complete list at Miscellaneous PHP Functions.

PHP Mail

You can use PHP to initiate an email to yourself or anyone else, with or without a copy list or blind copy list.  Typically this would be done after a user fills out a form with requested data.  For example, you might wish to use a form for user requests for information from you, to assure yourself of gathering required data.  To learn how to do this, see Using PHP to Send an Email from an HTML Form.

Resources

And Now ...

I will be adding to this document after I have reason to learn more about PHP myself or I just decide to continue my own learning process, which I will share with you!

Fred