PHP is one of the most popular programming languages in web development. At first, it was known as Personal Home Page but now PHP is known 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 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 you start learning PHP programming from very beginning, you will be able to work various web development platforms so easily.
Prerequisites for PHP Programming
You need a web server, a database server and a PHP compiler to code with PHP. If you buy any domain and hosting from a domain and hosting company like Hostgator or GoDaddy, you will get your web server, database server and PHP compiler ready to work. Don’t be upset if you have no domain and hosting yet. You can turn your PC into a web server with XAMPP package. 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 PC, start Apache and MySQL database from XAMPP control panel. If you install Apache and MySQL in Linux environment, start httpd and mysqld service. Now follow the below steps.
- In windows system, go to C: //xampp/htdocs. Now create a 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 any PHP code.
Now open index.php file with any text editor. You can use Notepad, Notepad++, phpDesigner, Dreamweaver or any other popular code editor as you like. I am using Dreamweaver which has some advantages than any other editor. I hope you will enjoy Dreamweaver than any other editor if you use it.
Now write the following code in 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 below output.
PHP Code Explanation
In the above code, the first line contains <?php which indicates that the PHP code is starting from here and last line contains ?> which indicates that the PHP code is ending here. Generally PHP code starts with <?php token and end with ?> token. When your browser comes to this line, it calls PHP compiler to execute the lines between starting token(<?php) and ending token(?>). Second line contains a PHP built in function echo which 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 which indicates the end point of a line. 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 are now able to run any PHP code successfully. If you face any confusion to run your first 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. Spend some times to study that article and clear your concept about variable and data types in PHP programming.