A brief overview of PHP

Dan Hamilton
4 min readFeb 19, 2019

--

The History of PHP

PHP is a server side scripting language written in C, originally written by Rasmus Lerdorf. The original purpose was for Lerdorf to monitor the number of viewers of his resume on his personal page (PHP originally stood for Personal Home Page). Version 1.0 was released in 1995, and could be used to build dynamic web pages by handling form submission and rendering html. According to Lerdorf, “there was never any attempt to write a programming language”; it logically progressed until a development team was formed and PHP/FI (Form Interpreter) 2 was released in 1997.

In 1998 PHP 3 was released, and was the first widely used version. PHP 3 saw a total rewrite of the parser by Zeev Suraski and Andi Gutmans, as well as a redefining of the acronym from “Personal Home Page” to “PHP: Hypertext Processor”. PHP 4 was released in 2000, PHP 5 in 2004, and PHP 6 was set to release around 2010, but was scrapped and mostly integrated into PHP 5.3/5.4.

In 2014, a new version of PHP was developed, called PHP 7, despite the fact that PHP 6 had never come out. This was due to the fact that several books and papers referencing PHP 6 had been released, leading to the fear that using version 6 might cause confusion. Because PHP 7 was a new major version, the release was not required to be backwards-compatible with old PHP code, so it presented an opportunity to make many deep changes in the codebase. In addition to major performance upgrades, PHP 7 changed how errors effected running processes, implemented more symbolic operators, and generally improved behavior of many lower level functions.

Running PHP

PHP doesn’t have it’s own built in web server like Rails or Django. Instead, a web server like Apache or Nginx must be used to interpret and run the PHP code on the server side.

Writing PHP/Rendering HTML

PHP scripts are generally placed in a .PHP file so the server knows how to interpret them, but it is possible to configure the web server to parse PHP in other filetypes. Within the file, PHP is wrapped in starting and ending tags

<?php
//code goes here
?>

These tags can be used to wrap HTML and dynamically render elements, similar to how we would do in JSX or Ruby

<?php
$comments=array(
array("id" => 1, "username" => "dan", "title" => "Example Comment 1", "text" => "This is a comment"),
array("id" => 2, "username" => "user 2", "title" => "Example Comment 2", "text" => "This is a second comment")
);foreach ($comments as $comment){
?>
<div id="comment<?php echo($comment["id"]) ?>">
<h1><?php echo($comment["username"])?></h1>
<h2><?php echo($comment["title"])?></h1>
<p><?php echo($comment["text"])?></p>
</div>
<?php
}

would generate the html

<div id="comment1">
<h1>dan</h1>
<h2>Example Comment 1</h1>
<p>This is a comment</p>
</div>
<div id="comment2">
<h1>user 2</h1>
<h2>Example Comment 2</h1>
<p>This is a second comment</p>
</div>

Talking to the Database

Because PHP has no built in ORM, database queries must be written manually. In earlier versions of SQL, this was a huge security problem, but newer versions have added functionality called prepared statements that parses and compiles SQL template statements before the application binds values to the statement and executes it. Once a statement has been bound, you only have to resend the parameters to make another database call, increasing performance.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// prepare and bind
$stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $firstname, $lastname, $email);

// set parameters and execute
$firstname = "John";
$lastname = "Doe";
$email = "john@example.com";
$stmt->execute();

$firstname = "Mary";
$lastname = "Moe";
$email = "mary@example.com";
$stmt->execute();

$firstname = "Julie";
$lastname = "Dooley";
$email = "julie@example.com";
$stmt->execute();

echo "New records created successfully";

$stmt->close();
$conn->close();
?>

https://www.w3schools.com/php/php_mysql_prepared_statements.asp

Frameworks

Like Javascript and Ruby, developers have created framework libraries for PHP to make building MVC web apps more streamlined. They have templating engines, database interfacing, packages similar to gems, security, routing, and other features we’ve come to know and love. The most popular PHP framework is Laravel, but many others exist.

Example of Laravel project structure

Conclusion

PHP has been around for quite a long time, and due to its process of creation and time as a jankey language gets a pretty bad rap among developers. However, much like Javascript, the developers of the language and frameworks build with it are defying expectations around the language and turning it into a modern, well supported language capable of supporting high performance web apps.

--

--