PHP is one of the most popular programming languages in web development. At begining, PHP was known as Personal Home Page. But later it was renamed as Hypertext Preprocessor because PHP processes hypertext (text, image, audio and video) at server side first and then sends to client side to show result.
It is an open secret that more than 80% websites are built with PHP now. So, the people who are interested to work on web development should get started with PHP programming.
There are various popular web development frameworks such as Laravel, Zend Framework, CakePHP, CodeIgniter etc are built with PHP language. Some most popular CMS (Content Management System) like WordPress, Joomla, OpenCart etc are also built with PHP language. So, if we start learning PHP programming from very beginning, we will be able to work various web development platforms so easily.
PHP is such a programming language which is popular from begining to till now and its demand is growing day by day. Learning PHP is easy but starting PHP from begining is not so easy. A proper guide is required to start PHP programing. So, this article is designed to show how to start PHP programming from very begining with a basic PHP program.
Prerequisites for PHP Programming
PHP compiler is required to compile PHP code. As PHP is mainly used to develop web application, a web server is also required. PHP can smartly store data to database, so database server is also required when PHP deals with data.
If we buy hosting from any hosting company like Hostgator or GoDaddy, we will get our web server, database server and PHP compiler ready to work. But PHP developers are usually used to work on local sever environment where we can turn our PC into a web server, a database server and can inatalll PHP compiler to compile PHP code.
Rather installing web server, database server and PHP compiler, it is better to use a package application which provides these environment as a bundle. Popular package applications those are PHP developers use are XAMPP and WAMP.
XAMPP is so popular and used by most of the web developer. It is also so easy to use. Just download XAMPP and install it to your system. XAMPP will provide you an Apache web server, a MySQL database server and a PHP compiler to work with PHP language. If you use Linux Operating System, you can install an Apache web server package, a MySQL database package and a PHP package on your Linux system and start coding with PHP language.
First Program in PHP
If you install XAMPP package in your Windows PC, search and run XAMPP Control Panel and then start Apache and MySQL database server from XAMPP Control Panel. If you install Apache and MySQL in Linux environment, start httpd and mysqld service. Now follow the following steps.
- In windows system, go to C: //xampp/htdocs. Now create a new directory and rename it as you like. In my system, I have created a directory named php. Now create an index.php file into this directory.
- In Linux system, Go to /var/www/html directory and create an index.php file here. Also you should run following command to compile PHP code successfully.
chown -R apache:apache /var/www
I have created index.php file into C://xampp/htdocs/php location. Notice carefully the PHP file extension (.php). Every PHP file must have this extension otherwise PHP compiler will not compile the PHP code.
Now open index.php file with any text editor. You can use Notepad++, VSCode, VSCodium or any other popular code editor as you like. Now write the following code in this index.php file.
<?php echo "Hello World!"; ?>
This is the minimum code in PHP programming. Now run the above code with your favorite browser typing http://localhost/php in address bar and then hit enter key. If everything is OK, you will get the following output.
PHP Basic Code Explanation
From the above PHP code we can see that the program starts with <?php that indicates PHP code starting and at the end we can also see ?> that indicates end of the PHP coding. Generally PHP code starts with <?php token and end with ?> token. When web server comes to this line, it calls PHP compiler to execute the lines between the starting token(<?php) and the ending token(?>).
Withing these starting and ending token we can see a PHP built in function named echo that prints any string between double quotations or between single quotations written in it. As a result, we can see the string Hello world! to the screen. A semicolon is used at the end of this line that indicates the end point of a statement. It is better to use semicolon at the end of any line to remove coding complexity.
First program in PHP has been explained in this article with example. I hope, you will now be able to run any PHP code successfully. However, If you face any confusion to run your first PHP program, feel free to discuss in comment or contact me from Contact us page. I will try my best to stay with you.
In the next article, PHP Variable and Data Types will be explained from very beginning. It is the most badic terms to understand PHP programming. So, spend times to study that article and clear the concept on Variable and Data Types in PHP programming.