Throughout this php tutorial for beginners by example, we'll learn to build web applications with PHP 7 for server and MySQL for database access and storage. We'll also see detailed comparisons with other popular solutions for web development such as Python with Django or Ruby with Ruby on Rails. In this tutorial, we'll create a CRUD (Create, Read, Update and Delete) example application with a MySQL database.
PHP stands for Hypertext Preprocessor. PHP is a the most popular programming language for building web applications. It's an open source scripting language that runs on the server. PHP is free to download and use and easy to learn.
PHP 7 is the latest version of PHP which brings many new features to the language:
- Speed: PHP 7 has increased performance and better memory consuption
- Type declarations
- Return type declarations
- Better error handling
- Throwable interface
To complete this tutorial and build a web application you need to have some basic understanding of the following technologies:
Throughout this php tutorial we assume you have a development machine with these requirements installed:
- A web server: you can install either Apache or nginx (You can also use the built-in PHP 7 server for development).
- PHP 7
- MySQL database system
PHP scripts use the
- PHP is available on popular platforms such as Windows, Linux and Mac etc.
- PHP can be used with most popular servers such as Apache and IIS etc.
- PHP works with most popular database system but commonly used with MySQL.
- PHP is free and open source.
Before adding PHP code inside a script file that ends with
.php you need to start your code with
<?php tag and ends it with
For example, this is a file that displays a Hello World! string:
<!DOCTYPE html> <html> <body> <h1>PHP Example</h1> <?php echo "Hello World!"; ?> </body> </html>
The script contains an HTML document with php code. The server processes this script without touching the HTML code but only the PHP code between
?>. In this example, the php code use the
echo function to output the Hello World! string.
echo "Hello World!"; is a php statement. It ends with a semicolon
You can also add comments alongside with your php code using
# for single-line comments and
/* */ for multi-line comments.
PHP vs. Python & Django
PHP is a programming languages which has a sole purpose to create back-end web applications while Python is a general purpose programming language that can be used for web development and other fields such as data science and scientific calculations so our comparison will be between PHP and Python equipped with a web framework. The most popular web frameworks for Python are Django and Flask with Django being more popular than Flask.
In order to compare PHP with Django we need to consider many factors such as:
- Are your a beginner or experienced developer?
- Are looking for quick insertion in the job market? etc.
More experienced developers have more potential to quickly learn a new programming language than beginners
Both PHP and Python are popular languages. They are both extremely popular among web developers and power most of the websites on the web today.
Let's take a look at these three factors:
- Popularity of PHP and Python with Django for web development
- The learning curve for Python, Django and PHP
- The available libraries and packages, learning resources and the community
Both PHP (dominates 80% of the market)and Python are popular languages, but for web development PHP is more popular than the most popular framework for web development in Python which is Django.
Popular websites like Facebook and Wikipedia are built in PHP.
Also many popular website and apps that you use daily are using Python. For example, YouTube, Reddit, Pinterest and Instagram etc.
A learning curve describes how easy or difficult the programming language is? Which simply means how easy to become familiar with the programming syntax and to start implementing requirements using the language.
Python is a lot easier than PHP since it has clear and readable syntax so for a beginner developer it would be easier to learn. Many universities in the world are using Python as the first programming language for their students.
On the other hand, PHP has a less readable and confusing syntax which makes the learning process for a beginner developer more difficult, but to be fair, once your learn and become familiar with the syntax you can start creating websites with the same ease.
Batteries and libraries
PHP has many libraries, frameworks and CMSs than Python. For example WordPress, the most popular CMS platfrom which everyone is using create a website is built in PHP. Also popular eCommerce solutions like Magento and WooCommerce are developed in PHP. Python with Django also offers many libraries a quite a few CMSs but not as equal to PHP.
Now let's see a list of pros and cons for both Python (and as a result Django) and PHP:
Let's start with Python pros and cons:
- Python is a popular general purpose programing language,
- Python is considered one of the most liked programming language by developers,
- Easy to learn,
- Has very readable syntax,
- Web apps created in Python are safer and more scalable
The pros and cons of PHP:
- PHP is the most popular programming language designed only for creating server web applications. 80% of websites in the Internet are powered by PHP
- PHP is the most hated programming language by developers
- Easier to learn but has confusing syntax for beginners
- PHP is less secure than other programming languages designed for web development
The best recommendation, for beginner developers is to try out both languages and then choose the one they are more comfortable with. But you need also to consider the job market and learning resources. Python is easier to learn while PHP offers you a better chance for quickly getting a job and has more learning resources around the world.