PHP Basic Program with Example

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.

First Program in PHP
First Program in PHP

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.

Hello world!

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.

I am a system administrator and like to share knowledge that I am learning from my daily experience. I usually work on MikroTik, Redhat/CentOS Linux, Windows Server, physical server and storage, virtual technology and other system related topics. Web development is also my favorite topic and love to work with PHP, HTML, CSS, Tailwindcss, Bootstrap, JavaScript, jQuery, Vue.js, WordPress and Laravel framework. Follow Me: Facebook, Twitter and Linkedin.

Related Posts

PHP Add Element to Array with Built-in Functions

Array plays an important role in PHP programming. It is one of the most used data structures in PHP programming. So, it is better to have a…

PHP Array Explanation

Array is one of the most important and basic terms in PHP Language. Almost all the PHP program uses array data structure. So, those who want to…

Function Call by Value and Call by Reference in PHP

More than thousands of built-in functions proves the power of PHP Language. Besides built-in function PHP allows to create user defined function. How to create user defined…

PHP Variable Usage inside of Function

Besides more than thousands of built-in functions PHP provides facility to define custom/user defined function. How to define and call a user defined function was discussed in…

PHP Function Explanation

Real power of a programming language comes from its functions. More than thousands of built-in functions defined in PHP language proves the power of PHP language. Besides…

PHP break, continue and goto Statements

PHP break and continue statements are used to dominate or control PHP loop statements and PHP goto statement is used to jump one execution label to another execution label….

This Post Has 4 Comments

  1. I’d like to thank you for the efforts you’ve put in penning this blog.
    I’m hoping to view the same high-grade content from you in the future as well.
    In truth, your creative writing abilities
    has inspired me to get my own, personal website now 😉

Leave a Reply

Your email address will not be published.

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.