How to write first program in PHP

How to write first program in PHP

Published at - Aug 13, 2021

This post will cover basic things required to get started with PHP and write our first program and compile it on the server and get output data on our web browser. To start code we required to install PHP in our system to compile code and covert our system into the server. I am another article for installation in all operating system (Windows, Linux (Ubuntu) and Mac Os X) cleck here.

I am assuming that php server is installed and running. So I am going to start here with code level things not going to cover any configuration level.

To write out first code in PHP we need to create .php file in the root of our server’s root directory (different for every server). I am using LAMP on ubuntu system and root for my system is /var/www/html so I will create a new folder inside /var/www/html with name tutorial1 and our first file with the name index.php

Now open your file with your favourite text editor and type the following code and save the file.

Hello World

  echo "Hello World";

Output - Hello World

After saving file open your browser and type the following http://localhost/tutorial1/ and if everything works fine then we will get hello world as an output on our browser.

Code explanation- There is a total of 3 lines in our code in line 1 and 3 we are opening and closing the PHP syntax which means that the code b/w this syntax in PHP code to execute. In line 2 I used native keyword echo which is used to print string.

Other examples –

Sum of two numbers

 $a = 5;
 $b = 6;
 $sum = ($a + $b);
 echo = "The sum of $a and $b is - $sum";

Output - The sum of 5 and 6 is - 11

Condiation if & else

 $a = 5;
 if($a > 2){
  echo '$a is grater than 2';
  echo '$a is not grater than 2';

Output - $a is grater than 2

These programs are just to getting started with PHP and try to understand how to create file and folders and get the output of our code on the browser side. For more any issue regarding please feel free to comment I will try to reply as soon as possible with a better solution.

About author

Harendra Kanojiya

Hello, I am Harendra Kumar Kanojiya - Owner of this website and a Fullstack web developer. I have expertise in full-stack web development using Angular, PHP, Node JS, Python, Laravel, Codeigniter and, Other web technologies. I also love to write blogs on the latest web technology to keep me and others updated. Thank you for reading the articles.

Follow Us

Follow us on facebook Click Here

Facebook QR
Scan from mobile
Join our telegram channel Click Here
Telegram QR
Scan from mobile