PHP Basics

2 Replies • Leave your reply


Learn the basics you need to know about PHP when setting up a server for the first time. This tutorial covers everything you need to know about the initial setup and usage of PHP, to guide you through the first steps.


  • A Cloud Server with PHP installed and running.

Note: PHP is installed and running on a Standard Linux installation by default. If your server was created with a Minimal installation, you will need to install and configure PHP before you proceed.

Using PHP

Although it is possible to use PHP from the command line, typically it is rendered and displayed in web pages. To do this, PHP scripts can be uploaded either as stand-alone pages, or PHP code can be embedded in an HTML page.

To use a PHP script, upload the PHP pages to your website's document root. Then view the file in a browser.

By default, a PHP page will need to have the .php file extension in order to work properly. To run PHP from a file with the .html or .htm extension, you will need to edit the Apache configurations as follows:

Ubuntu 16.04

Open the /etc/apache2/mods-enabled/php7.0.conf file for editing:

sudo nano /etc/apache2/mods-enabled/php7.0.conf

Add the following to the top of the file:

<FilesMatch ".+\.html$">
    SetHandler application/x-httpd-php

Save and exit the file. Restart Apache for the changes to take effect:

sudo systemctl restart apache2

CentOS 7

Open the /etc/httpd/conf/httpd.conf file for editing:

sudo nano /etc/httpd/conf/httpd.conf

Scroll down to the AddType block:

# If the AddEncoding directives above are commented-out, then you
# probably should define those extensions to indicate media types:
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz

Add the following two lines to this block:

AddType application/x-httpd-php .htm 
AddType application/x-httpd-php .html

Save and exit the file. Restart Apache for the changes to take effect:

sudo systemctl restart httpd

Changing PHP Configurations

For information on how to view the existing PHP configurations, and how to change the PHP configurations by editing the php.ini file, see our article Manage PHP on a Cloud Server with Linux.

Writing PHP Code

PHP is a fast and easy way to render code in a browser. PHP is also designed to integrate well with MySQL/MariaDB, which makes it a natural choice for managing the interaction between a website and a MySQL/MariaDB database.

For more information, see our beginner's tutorials on using PHP to interact with MySQL/MariaDB:

PHP can also be used to include files in an HTML file. For more information, see our article Use PHP Includes to Streamline Website Design.

The official PHP website also offers a series of excellent tutorials for beginning PHP users.

PHP Security

Because it is a scripting language, PHP is vulnerable to attack. Most PHP vulnerabilities involve "tricking" PHP into running malicious code. Cross-Site Scripting (XSS) and session hijacking are the two biggest concerns when it comes to PHP vulnerability.

Learn more about how to keep PHP secure in our related article Basic PHP Security.