Beamtic's logo

Share via:

PHP Tutorials

PHP tutorials from A to Z; these tutorials should cover most things that you will ever need to know about PHP. Help spread the word by linking and sharing!

94 views

Edited: 2020-09-24 20:13

About PHP

PHP is a general-purpose scripting language that is mostly used to create the server-sided parts of web-based applications. It was originally designed by a Danish programmer named Rasmus Lerdorf in 1995, and has since evolved into one of the preferred scripting languages among web developers.

While PHP can be used to create text-output in the form of HTML and XML, it can also be used to manipulate video and images — many people do not realize how powerful PHP can be as a web development tool. In addition, PHP can also be used to stream video files, to learn how to implement a video streaming feature you should read this tutorial: Streaming With PHP

PHP will also serve as a solid CLI scripting language, and in Linux environments you can even use it instead of Bash; all this requires is that PHP is installed on the system.

How long will it take to learn PHP

While there is no definite answer to this, since it depends largely on the individual, PHP is generally a very easy language to learn. Learning PHP before learning C++ and other compiled languages will sometimes be a easier route to take if you plan on learning a compiled programming language.

Learning PHP does not have any prerequisites, but it is certainly an advantage to know a little HTML and CSS if you want to create more than just CLI applications. If you have not yet learned HTML or CSS, then it is important that you look at these more like "one standard" that you need to learn rather than two separate things; this is because HTML and CSS is used so closely together today that it is hard to avoid using one without the other when designing web pages.

It is also important to remember that you do not need to memorize everything; you just need to get a basic understanding in order to use the relevant references when working on a project – memorization will come with time, use, and repetition. If you use a modern web development tool like Visual Studio Code, then there will also be build-in references when you hover the mouse over PHP functions.

Getting started

1. To use PHP you usually install a HTTP web server; Apache is free and remains one of the most popular web servers in use on the internet. For Debian/Ubuntu users, Apache may be installed easily from terminal:

apt install apache2

Windows users should download the Windows version from The Apache Foundations website: Downloading the Apache HTTP Server (httpd.apache.org)

Note. It is recommended you try setting up a Linux development environment, as it might be easier in the longer term, and it is actually not much different than working on Windows thanks to improvements to Linux GUI environments. There are also tiny differences between Windows and Linux that might cause problems, such as how Windows creates line breaks in text files, and how capitalization is handled in URLs and file system paths.

2. It is now time to install PHP on the system. Again, if using a Debian or Ubuntu based system, you probably just need to type the following command in a terminal:

apt install php7.4

If you are on a Windows system, you should find the Windows version of PHP; this can be found at: https://windows.php.net/download

3. Now that you got both Apache and PHP installed, you just need to connect Apache with PHP. On a Linux system you got a couple of choices — you can either enable Apache's mod_php, or you can setup PHP-FPM. For simplicity, let us stick with Apache for this part:

a2enmod php7.4

You should now be able to start coding web-application in PHP. For Linux, the website root is often located at /var/www/. You can create a file in this directory to see if everything was setup correctly, so let's create the /var/www/phpinfo.php file:

<?php
phpinfo();

We may open this file by typing in http://localhost/ in a browser's address bar. When opened in a browser, this file will display various information about your server, which is useful for testing and debugging.

  1. How to create a custom error handler for PHP that handles non-fetal errors.
  2. We can access other classes properties (variables) and methods (functions) by injecting the class object in the class that needs it.
  3. Setting custom HTTP Headers with cURL is useful when changing User Agent or Cookies. Headers can be changed two ways, both using the curl_setopt function.
  4. Should you use file_get_contnts or cURL when performing HTTP requests from PHP? It does not matter; but regardless of which you use, you should still handle errors properly!
  5. When using file_get_contents to perform HTTP requests, the server response headers is stored in a reserved variable after each successful request; we can iterate over this when we need to access individual response headers.

More in: PHP Tutorials