PHP Overview

Photo by Joshua Sortino on Unsplash
Photo by Joshua Sortino on Unsplash

Introduction to PHP

PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language. PHP is one of the most popular languages for web development because it is free software available by default on most web servers. There are a large number of web template systems, web content management systems, and web frameworks that are built with PHP.

PHP code is usually processed by a PHP interpreter implemented as a module in the web server or as a Common Gateway Interface (CGI) executable. The web server combines the results of the interpreted and executed PHP code, which may be any type of data, including images, with the generated web page. The standard PHP interpreter, powered by the Zend Engine, is free software released under the PHP License. PHP has been widely ported and can be deployed on most web servers on almost every operating system and platform, free of charge.

History

Originally created by Rasmus Lerdorf in 1994, the PHP reference implementation is now produced by The PHP Group. PHP originally stood for Personal Home Page, but it now stands for the recursive acronym PHP: Hypertext Preprocessor.

During the 2010s there have been increased efforts towards standardisation and code sharing in PHP applications by projects such as PHP-FIG in the form of PSR-initiatives as well as Composer dependency manager and the Packagist repository. The PHP language evolved without a written formal specification or standard until 2014, leaving the canonical PHP interpreter as a de facto standard. Since 2014 work has gone on to create a formal PHP specification.

PHP Resources

Quick Reference – PHP

Here is a quick reference for the basics of PHP…

Hello World in PHP

<?php if($_SERVER["REQUEST_METHOD"] == "POST") { ?>
    Hello <?php echo $_POST["name"]; ?>! 
<?php } else { ?> 
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
    Name: <input name="name" type="text" />
    <input type="submit" />
    </form>
<?php }  ?> 

PHP: Basic Syntax

Variables & Data Types

  variable names need to start with $

Branching

if (condition) {
  // do something
} elseif (condition) { 
  // do something else
} else { 
  // do something else 
}

Arrays

$primeNumbers = array(2, 3, 5, 7, 11);

Loops

You can loop through all the items in an array:

$myList = array("one", "two", "three", "four", "five");

foreach ($myList as $item) {
  echo $item;
}

You can loop a fixed number of times:

for ($i = 0; $i < 5; $i++) {
  echo $i;
}

You can loop while a condition is true:

while (true) {
  // do something that may change the condition
}

You can loop until a condition is false (always loops once):

do {
  // do something that may change the condition
} while (true);

PHP: Object-Oriented Syntax

Classes

class Animal {
  public $species;
  public $name;
  public $sound;
}

Objects

$myPet = new Animal();

$myPet->species = "dog";
$myPet->name = "Fido";
$myPet->sound = "Bow-wow";

Methods

class MyCalculator {
  function add($num1, $num2) {
    return $num1 + $num2;
  }
}

Constructors

class Animal {
  public $species;
  public $name;
  public $sound;

  function __construct($species, $name, $sound) {
    $this->species = $species;
    $this->name = $name;
    $this->sound = $sound;
  }
}

$myPet = new Animal("dog", "Fido", "Bow-wow");
$yourPet = new Animal("cat", "Garfield", "Screech! Hiss!");

Books on PHP