01 What is PHP?
PHP is a server-side programming language that you can use to make websites, apps, APIs, command line scripts and more.
It’s definitely a beginner friendly language as it forgives you for lots of mistakes you might make when writing PHP and you can use a little or a lot depending on your website or app’s needs.
You may have come across WordPress, the popular blogging platform. This is actually coded in PHP.
There are also lots of popular open-source packages that power millions of websites on the Internet including e-commerce sites built on the popular Magento Platform (which again is written in PHP).
By the end of this introduction to Learning PHP, you will get a good understanding of what PHP is and why you should care about it!
What is PHP?
PHP, which stands for “PHP: Hypertext Preprocessor” (this is what’s known as a recursive acronym!), is a programming language designed specifically for web development. It’s been around since 1994 and has become one of the most widely used languages for creating dynamic web pages although it can be used in other ways too.
Why Should New Developers Care About PHP?
It’s Everywhere: PHP powers a huge portion of the web. Popular platforms like WordPress, Facebook, and Wikipedia all use PHP.
Easy to Learn: PHP is known for its gentle learning curve, making it a great first language for aspiring web developers. It’s quite forgiving when you make a mistake with your PHP code!
Versatile: From simple scripts to complex applications, PHP can handle a wide range of web development tasks and it can be used to create a simple contact form on your site or a full-blown e-commerce solution.
Free and Open Source: You don’t need to pay anything to start using PHP, and there’s a large community of developers ready to help you out.
Widely Supported: Pretty much every shared hosting platform will allow you to run PHP (they kinda have to because the need it for WordPress to run!) and it’s not too hard to get setup on your own private server if you want more flexibility over your PHP configuration.
What Are Some Of The Key Things New Developers Should Know About PHP?
1. Server-Side Language
PHP doesn’t run directly in a user’s web browser (unlike HTML, CSS, and JavaScript which can be loaded in to Chrome, Firefox or pretty much any other web browser)
PHP runs on a web server so you need some kind of rented server or shared hosting package in order to run PHP code.
This means a user needs to make a request (usually by typing an adress into their web browser) for a PHP file which then runs and evaluates the PHP script on the server and returns the result to the user. At a minimum this repsonse will contain HTML but could be a complete web page with HTML, CSS and JavaScript or alternatively, data like JSON might be returned from the request.
The advantage of this is it means PHP has access to other resources available on the server where it is hosted. For example it can interact with databases, files, and other server resources.
These are things the browser can’t access with things like HTML, CSS and JavaScript alone.
2. Easy to Embed in HTML
PHP code can be easily mixed with HTML, allowing you to create dynamic web pages. For example, let’s say you have a <h1>
tag on each of the pages of your web pages.
If you had multiple pages on your site, all with separate HTML files like index.html
, contact.html
, about.html
etc. then you would need to manage the text in those headings in each file.
<!-- index.html -->
<h1>Home</h1>
<!-- contact.html -->
<h1>Contact Us</h1>
<!-- about.html -->
<h1>About Us</h1>
Which can be a pain to maintain, especially when you have hundreds of pages to deal with on your site.
With PHP, you can use variables to dynamically change the text inside the <h1>
tag on each page before it is sent to the user’s browser.
<h1><?php echo $page_title; ?></h1>
This makes it far easier to maintain these pages as you simply have one HTML template with PHP embedded and use this to change the title of the page, most likely by pulling a list of page titles from a database and selecting the one that matches the current URL requested by the user.
Sounds confusing? We’ll be looking at some examples throughout this tutorial so don’t worry, it’ll all make sense soon!
And of course, there’s lots more you can embed into your HTML templates. Anything information you want to retrieve from your database (like a list of e-commerce products for example) can be returned from your PHP code.
3. Large Standard Library
PHP comes with a ton of built-in functions for common tasks like working with strings, arrays, files, and databases. This means you can do a lot without having to rely on external libraries.
With PHP being an established language there is a lot of support for doing common jobs as these have been requested and introduced to the language over the years.
A quick example is the PHP trim function
<?php>
$text = " this is some text with space before and after ";
$trimmed = trim($text);
echo $text; // Outputs ->> "this is some text with space before and after"
?>
There are a loads of these PHP functions available on a standard install of PHP which will make doing these kinds of tasks easy.
4. Database Integration
You may or may not know that a database is a piece of software that allows us to store some information.
Whether it’s contact names and numbers for our friends, orders customers are making or the contents of blog posts written, all this information needs to be stored in a database.
There is loads of free and paid for database software and PHP works seamlessly with lots of these.
Databases, especially MySQL are commonly used with PHP applications to offer a level of persistence. In other words, when you close the browser after updating your friend’s phone number in your Contact List app, the data isn’t lost, it’s stored (or persisted) into the database.
Generally, databases will be protected to avoid users who are not authorized to update information in them so that’s why they’re are used server-side in the same way PHP is.
5. Framework Options
A PHP framework is a set of PHP functions, libraries and other tools that all come together to make writing a PHP application quicker, easier and safer.
As you grow as a developer, you’ll find many PHP frameworks like Laravel, Symfony, and CodeIgniter which will help you to write PHP applications more easily.
By following the standards of the framework you are creating your PHP apps with, you will not only create better applications but you will also be gaining additional skills which will be useful for your career.
But we’re getting ahead of ourselves. We’ll get on to PHP frameworks at a later point.
You’re probably itching to get started with PHP so let’s write your very first PHP script!
Getting Started with PHP
You can setup a local PHP development environment which will cover in a later tutorial but for now we’re going to use an online tool to run some PHP code.
This is the easiest way to run PHP quickly and to get a feel for the language.
Open up PHP Sandbox and type in the following block of code (you can literally just copy and paste the PHP code example).
<?php
echo "Hello, World!";
?>
If you’re wondering how this works, we’re not running PHP in the browser as that doesn’t work as mentioned above! The site we’re using will run the PHP code provided in the text box on it’s own server and then show us the result in the browser.
You can see in the output in the Result window that the PHP code you pasted into the text area has been compiled and run (again, this will have happend on the PHP Sandbox server) and the result is displayed to you.
Congratulations! 🎉 You’ve just run your first PHP program!
You can take a bit of time and play around with the example code in the PHP sandbox. Experiment with different text, try some HTML tags out.
The result that you see will be exactly what is returned to the user’s browser if they made a request for this PHP script on your own website.
The next step will be to setup a local PHP development environment so you can start learning all aspects of PHP but before we do that, let’s take a look at the history of PHP.