PHP Tutorial: Part 1 – An Introduction to PHP

September 21, 2021 / Web Development

This tutorial will provide an overview of what PHP is and will show you how to write and execute your first PHP script.

Introduction

The development of advanced and easy to use website creation platforms, like WordPress, Drupal, Joomla and Magento, mean that anyone wanting to build their own website today doesn’t need to learn how to do coding or use computer scripts. Unless you are a web developer, the chief reason to learn to code is if you want to customise the way a website works or looks and there isn’t a customisation option built-in or plugin that will do the job for you. In these situations, the alternative is to recode a website and with most CMS website types, the scripting language you will need to learn is PHP.

What is PHP?

PHP stands for Hypertext Pre-Processor. It is a widely-used, general-purpose scripting language that, because it can be embedded into HTML, makes it perfectly suited for developing websites. It is also a very versatile language that can work on all the major operating systems, e.g., Linux, Windows, Unix and Mac OS, and on many types of web server. Another benefit is that, as a server-side language, the script runs on the web server, rather than on the user’s browser, preventing any compatibility issues.

Why use PHP?

The primary reason for using PHP is that the bulk of the world’s websites are built using it and the chances are, if you are tweaking the code of an existing website in order to customise it, this is the script you will need to use. If you are going to build a website from scratch, there are alternatives like JavaScript, Python and Ruby and you may find these easier to learn.

The other reason to choose PHP is that, because of its wide use, the internet is full of resources to help you learn the code and show you how to use it. You can even download code that has already been written and insert it into your own scripts to save you time and effort. This can be especially helpful when you consider that one small error with the coding can stop your website from working or create a vulnerability that cybercriminals could exploit.

Originally, people used PHP to add new features to their websites, like feedback forms, guestbooks, message boards and counters. Today, however, there are tens of thousands of free themes, plugins and add-ons that do all these things for you, so, for most website users, there is little point in spending time creating your own. Unless you are a web developer, the chief reason for using PHP is to tweak existing code in order to customise your software.   

What do you need to use PHP?

To use PHP on your website, you’ll need to have it set up on your server. This shouldn’t be an issue as most managed hosting solutions come with PHP already set up. PHP is upgraded with new versions every two years and you’ll need to make sure you are using a stable and up-to-date version that is still supported. You can find out the current version of PHP that your site uses in your control panel and can update the version from there if you need to. 

Writing PHP

While the language itself might initially be challenging to write, you won’t need any special software to create it. You can do this with a simple text editor, like Notepad.

Declaring PHP

To let your server understand that your script is written in PHP, you need to declare it. For this reason, PHP scripts always begin and end with a PHP tag that tells the server to parse the information between them as PHP. There are two different types of tag you can use:

Either

PHP Code In Here
?>

Or

PHP Code In Here
php?>

Both these tags fulfil the same function and you can use either. However, you must start and end your code with the same tag, you cannot start with one and end with the other.

Writing your first PHP script – an example

Some PHP scripts are very easy to create. The example below is an instruction to your server to print out all the information it has about its PHP installation. To create it, simply type the following code into your text editor:

PHP Code In Here

?>

phpinfo();
?>

This short piece of code contains a standard PHP function called phpinfo. This tells the server to print out a standard table of information about the PHP installation on your server.

When it comes to understanding how to create scripts, it is important to note here that the line ends with a semicolon (;). Ending lines with a semicolon is a standard feature of PHP and many other scripting languages. Without the semicolon, the script will produce an error, so it is vital that you check line endings carefully.

Finishing and testing your script

When you have finished your script, save the file as phpinfo.php and upload it to your server. Once this is done, type the script’s URL in your browser (e.g., mywebsite.com/phpinfo.php). If your script is accurate and you have PHP installed on your server, you should see a table of information appear on your screen about your server’s PHP installation.

Conclusion

This tutorial will have given you an understanding of why you might want to create PHP scripts, what you need to create them and how to make a start with writing PHP.

For more information about our hosting solutions for PHP based platforms, like WordPress, visit eUKhost.

Spread the love