Published in


A Super-Simple PHP Tutorial for Beginning to Code — Part 1

Photo by KOBU Agency on Unsplash

Draft Outline for what I consider a “Good Tutorial”

  • Start with a clear purpose statement for the tutorial — why would one want to take it?
  • Identify the absolute minimum prerequisites, provide a link to a super-simple tutorial on those subjects.
  • Do a quick check that things are set up.
  • List and clearly state a short list of key principles or conceptual distinctions that one needs to understand to produce something useful, with a link to the page in the official manual with more details.
  • Include the simplest possible examples of code — free from dependencies or extraneous styling.
  • Provide a “problem set” in order to learn by doing.
  • Keep the tutorial open for comments and improvements! So — please let me know if this one could be better.

Purpose of THIS tutorial

  • To provide bright beginners the basic knowledge to create their first dynamic web applications in PHP (Part 1), how to get pages to communicate with each other (Part 2) and how to get your pages to carry out database queries (Part 3).
  • To focus on one, simple way to code. Even in PHP there are LOTS of ways to do things. There are entirely different paradigms of coding that you’ve probably heard people argue about. This tutorial uses the “Procedural” and “Functional” paradigms because I think they are simpler to learn — as distinct from the OOP: the Object-Oriented paradigm. You’ll see a bit of OOP when we refer to databases.

Minimum Prerequisites

  • A basic understanding of HTML. Here is a great 30-minute YouTube intro, and here my favorite written reference.
  • A text editor — and preferably one that can automatically check your code. I recommend the free and open source Visual Studio Code.
  • The command-line version of PHP. If you’re not clear if you have it, go to your command line and ask it what version is installed:
php -v
PHP 7.4.18 (cli) (built: May 3 2021 11:59:44) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.18, Copyright (c), by Zend Technologies
  • If none are installed, on windows click here. On chromebook/debian linux:
sudo apt install php7
  • Create a text file in that area named index.php (the default home page for any webserver, an html file with one line of php in it. (The special <?php … ?> tags are how php gets embedded into HTML.)
<?php echo "Hello World! Today is ".date('l'); ?>
  • Start the local web server that’s built into php
php -S localhost:8080
  • Point your browser to localhost:8080 and see if it says Hello World and the day of the week. Boom — your first dynamic webpage! To stop your local server from running, just hit ctrl-C at the command line.

First Principles and Definitions

Note: there are LOTS more, but if you understand these you can get started. At the end I’ll mention

  • Code is text in a particular programming language that tells a computer what to do.
  • PHP is “a widely-used, open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.”
  • Basic Syntax: To get started, you really only need to know that statements end with a semicolon, case matters ($hello is not the same as $HELLO), and that php gets embedded in html in <?php … ?> tags. It’s also nice to put comments in your code — that is, explanations for humans not the computer. Comments inside php code can be added following // for the rest of a line or a block of comments can be surrounded by /* … */
  • Program/Script/Application (“app”): these terms all describe the collection of code that actually does something. It may be stored in one file or in hundreds of files.
  • Client/Server: There are three basic ways to build applications: stand-alone apps like Microsoft Word written in C++ that you download and run, server-side web applications like Medium or Facebook where a remote server does most of the work and sends it to your browser (the “client”) — many of these are written in PHP, or client-side web applications where your browser does the work itself, nearly always written in javascript. These days, most major apps are a combination but this tutorial will focus ONLY on server-side.
  • Variables: Nearly all languages are basically algebra. x=3; y=4; x=x+y; (now x equals 7). In PHP, all variable start with $ and should have names that clearly communicate what they mean. eg: $name=”John”;
  • Constants: Just like it sounds. pi is a constant. You cannot write pi=7; Once a constant is defined, it cannot be changed.
  • Types: Variables can be of different types, eg: integer, floating point, text string. In the point above, we had examples that were integers and one that was a string. In some languages, you must define what type a variable is — in PHP it will dynamically adopt whatever type seems obvious.
  • Operators: these include the arithmetic operators like +, -, * (for multiply) — logical: and (&&), or (||) — string concatenation “.” For example:
$fullname=$first_name." ".$last_name;
  • Expressions are bits of code that has a value, and might get assigned to a variable.
substr("Hello World",0,5);
  • Statements are single “sentences” of code that do one thing. The most famous statement is shown in the featured image: echo “Hello World!”;
  • Block: a group of statements treated as a unit. In PHP, a block is surrounded by {} and blocks are vital when you deal with the next point.
  • Conditions and iterations: What makes program clever, is that blocks of code can be conditionally run, or looped, depending on the value of expressions. (Important note! In PHP you assign a value to
if ($name=='Bob') {echo "Hi Bob!;} else {echo "Who is $name?";}
for ($i=1;$i<4;$i++) {echo Testing $i;}
  • Arrays are sets of values with one name, that can be accessed by their index which starts from 0 (not 1) or as associative arrays with keys. Examples:
echo $fibo[0]; // this would display "1"
$capitals=['MO'=>'Jefferson City','IL'=>'Springfield','VT'=>'Montpilier];
  • Functions are a chunk of code that is given a name. You will want to write any non-trivial app as a bunch of meaningfully named functions. Functions can either return a value or just do something. For example, the main body of a PHP app might simply call a series of functions, which are then defined before or after.
// Main program
echo($name." attends ".$school);
// Functions
function getUserName() {
return 'Sally Ride';
// This has a bit of database code in that we'll cover in Part 3
function fetchSchoolName($fullname) {
$schools=['Sally Ride'=>'Stanford','Joy Reid'=>'Harvard'];
return $schools[$fullname];
// Note - this last one doesn't have a return value.
function sayGoodBye($audience) {
echo("Goodbye, $audience");
  • Built-in functions: There are hundreds of functions already defined in PHP, particularly for doing all sorts of things with strings and arrays. You will use these manual pages a lot!
  • Parameters: The example fetchSchoolName() passes one parameter —name of a person who’s school we we want to lookup. As you’ll see in the manual pages for built-in functions, default values can be defined so that you don’t have to fill in all the parameters.
  • Scope: This is a tricky part, and different languages handle it differently. When you define a variable, in what parts of the code does it exist? In PHP, variables defined in the main program are not accessible within the main program are not normally accessible within functions and vice versa. There are ways around this, but you really don’t want to try it.
  • Good Practice. Coding can get complicated, and there are lots of “mantras” the coders tell themselves. Here is a super short list of them, and here is a longer one (many of which will be over your head at this point.

Problem set for Part 1!

Other things you might want to export

  • Repositories: Store, manage versions and collaborate in the cloud by setting up a github account. Github works nicely with VSCode or you can edit in it directly from the website.

Click here for Part 2



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
John Coonrod

John Coonrod

A guy committed to human dignity for all.