PHP and MySQL are two acronyms we see a lot when reading about web development, but it’s not that easy for non-developers to understand the differences between them and how they work together. That’s why I wrote this article, even if you are new to this you’ll finally understand how much they differ.
PHP and MySQL are two languages used in web development. While PHP is generally used to add dynamic features to web pages, MySQL is the engine where all the data is stored, in a database. PHP can use MySQL to do requests in the database and display the information on the website.
I have been a web developer for 20 years, but I’ll try to keep things simple enough for everyone to understand.
I will start by explaining each concept and will conclude with the main differences between them.
What is PHP?
PHP is a general-purpose scripting language especially suited to web developmentWikipedia
Ok, thanks Wikipedia, but it doesn’t tell much about it. PHP stands for “PHP Hypertext Preprocessor”. It has been created in 1994 but took a few more years to become widely used on the web.
What is PHP used for?
A static website is generally written in HTML. HTML is a markup language, offering tags to format text information (size, bold, underline, pictures, etc.). Before the 2000s, most of the websites were built with HTML only.
Imagine an e-commerce website, it will require to almost manually create one page for each product with the information about this product. When there is a price change, stock shortages or anything else, someone has to update the webpage.
PHP is a language that can be used a layer above HTML, to dynamically generate the HTML code depending on various information, like other files on the servers, database, visitor information, time of day, etc. Developers can create on PHP page that will display different information depending on these factors.
Example of PHP usage
Let’s take an example to make this even more clear. This website is built with PHP, using WordPress, a content management system written in PHP.
Depending on the URL of the page, it will get the correct article in the database and display it. I don’t have to create a different file for each article. I write it in the backend and WordPress (with PHP) will do the rest.
Is PHP still used?
I think the peak of PHP users was around 2000, it was a revolution for web developers, and almost any website was built with it at the time. Almost all developers have to learn it to keep their job.
Since then, the interest for PHP is coming down. According to Google Trends, PHP is five times less searched on Google in 2021 than in 2004, so it’s probably less and less used by web developers.
PHP is still very common on various websites (79% of the websites are still using PHP), especially by using system like WordPress or frameworks like Symfony, Cake and others. But other languages like Node, Python or Ruby are also more and more popular.
What is MySQL?
MySQL is an open-source relational database management system. Its name is a combination of “My”, the name of co-founder Michael Widenius’s daughter, and “SQL”, the abbreviation for Structured Query Language.Wikipedia
If you are entirely new to this, this definition won’t help much. A database is a computing method to store data on a server. You can see it as a folder on your computer. Each folder contains different files (named tables in a database), that can be used to store various types of information (articles, products, customers, etc.).
Why is MySQL used?
As the web was becoming more and more dynamic in the 2000s, developers needed a way to store data safely and display it fast on the visitor’s web browser. MySQL has been created with web usage in mind, so it was a natural choice for web developers.
MySQL has been integrated with the most common languages (like PHP) and it was easy to use it with them, that’s why it has been so popular.
MySQL usage example
I already gave you a few clues in the definition paragraph, but here is a more complete example.
Imagine an e-commerce website. They need a way to have a good deal of information available for their customers: products, stocks, prices, delivery and payment methods, customers addresses, etc. Everything will be stored in a giant database.
As the main usage of these data will be on the website, MySQL and PHP will be a natural choice in most cases.
The trends for MySQL popularity is almost the same as the one we have seen for PHP. It has been created at the same period, and was a revolution too. PHP and MySQL have been used on the same projects over the years, and it’s not a surprise to see that alternatives are now available for both of them.
PHP and MySQL work well together, but for projects choosing another language than PHP, they are more natural options available. For example: Python projects will often be based on Django, Node with MongoDB, etc.
Differences between PHP and MySQL
The main difference between PHP and MySQL is they don’t have the same purpose. PHP is a scripting language, that add dynamic content for HTML pages whereas MySQL a database management system, to store and access information in real-time.
So, even if they work together most of the time, that’s not something we can compare point per point. It’s like comparing a fridge and a dishwasher, they don’t have the same goals at all (I hope!).
Remember that PHP is the smart language, with all the usual features in programming (variables, conditions, loops, etc.) and MySQL is a service that will keep all the information safe, so that PHP can use it in the source code. MySQL also has a mini-language, SQL, that is used to do requests in the database (like retuning all articles from the technology category on AllDifferences.com).