Member preview

Introduction to PHP for web developers 2018

I recently stumbled upon a guide for coding PHP the right way. On this site they go through most aspects of the PHP language and history, from basic syntax all the way through testing and deployments. This tutorial is an introduction for programmers that might not be familiar with modern PHP.

At the simplest level we can run a PHP script by starting a web server.

php -S localhost:8000

This assumes that you have already installed PHP or have a version pre-installed on your machine. One of the latest versions is 7.2, 5.4 is another popular version. There is no PHP version 6.

Create a PHP file touch index.php with the contents:

<?php
print('hello world');

Congratulations! You started a local PHP server and printed out some contents. You can view this text on http://localhost:8000. What’s nifty about PHP is that it was built for the web and has html in mind. Add HTML to your PHP file and watch it render properly in the browser.

<?php
$title = 'My first PHP website';
$whereItsGood = 'Hood';
?>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1>Welcome!</h1>
<p></p>
<ul>
<li>All</li>
<li>Good</li>
<li>In</li>
<li>The</li>
<li><?php echo $whereItsGood; ?></li>
</ul>
</body>
</html>

PHP was built for the web and this will totally work. In practice when building a PHP application it’s best to use a proven framework like Laravel or Symphony to handle server side set up and database connections. The above code will soon become a huge mess, especially if you add multiple pages and client side Javascript. Separate your PHP files from your client side templates!

Composer is the primary package manager for PHP.

Modern PHP frameworks manage packages with Composer and use templating engines like Blade. PHP supports Object Oriented and Functional programming.

To start a project from scratch you can run composer init which will create a composer.json file. Most developers choose to use a PHP Framework when building web applications. Personally I recommend building with Laravel!

Below are some community resources of popular PHP packages:

Happy coding!

Update 04/01/18: This is a great intro to building PHP without a framework