How to Write PHP Code with Examples for the Beginners

PHP programming is popular because it is easy to learn and development is faster. Even though there are many limitations, it is still the most popular programming language for web development. In this topic, I will show a simple example of writing PHP code within html for beginners. I will assume you have a basic programming concept.

Before working on the PHP program, please make sure the PHP environment is set up in your system, i.e., XAMPP or WAMP is installed. They provide the web server, PHP and MySQL database for you. You can go through the topic How to install XAMPP on Windows for PHP Development

Watch YouTube Video

How to learn PHP codeAssumption

  1. You must have basic knowledge of HTML, CSS.
  2. You should have some basic programming knowledge.
  3. This topic is to show how to start writing a PHP program with HTML and CSS. If you want to learn PHP, visit W3Schools.com.

php for beginnersProgram Examples

Choose a PHP Editor

You need a good editor to write a PHP program. Notepad++ and Sublime Text editors are popular for writing PHP programs. You can download and install Sublime after searching in Google. It is a quick download and installation. You can also install it from Download Sublime.

You can also use Visual Studio Code. It provides a rich text editor and a better development environment by providing many other features for faster development. You can download it from https://code.visualstudio.com/download.

PHP program runs on the server. You need a web server to run PHP on the server. In our case, we are using the XAMPP server on Windows, it comes with Apache web server and MySQL. We will use the localhost server on the local computer to run our PHP program.

With this let us write a couple of simple PHP programs.

PHP code Example 1 - Display your name

Let us take a look at the below PHP code:

myprog1.php


<?php 
$name = "John";
echo "This is my first PHP program
"; echo "My name is ". $name; ?>

PHP scripts starts with <?php and ends with ?>. Variable names start with a '$'. $name is a variable that holds the value "John".

"Echo" is used to display some text or value. In the second echo we are concatenating $name with a string. “.” (dot) is used for concatenation.

Now, create a folder named "php" under your xampp/htdocs folder, copy and paste the above PHP code into a file myprog1.php and save it in your xampp/htdocs/php folder. For a PHP program, the file name should have a "php" extension. For my case it is shown below:

php code examples

Now, let us run the program. Make sure XAMPP control is running and Apache service is on. Run localhost/php/myprog1.php in the browser. It should display the below screen:

php code examples for beginners

You can see, the two echo statements are displaying the desired output. If you can see the above screen without any error, then you are successful in writing your first PHP program.

Example2 - Display all even numbers from 1 to 20

Now, let us write a PHP program to display all even numbers from 1 to 20. Many of you already have written such programs in C or Java. So, you know the logic. If you divide a number by 2 and the remainder is zero then we call the number an even number. We will write this logic in our program. Also, in this case, let us add some CSS so that the output looks a little better. See below program:

myprog2.php


<html>
  <head>
	<title>My Second PHP program</title>
	  <style>
		h1,h2{
			text-align: center;
			}
		p {
		   text-align: center;
		   color: blue;
		   font-size: 25px;
			}
	  </style>
  </head>
  <body>
	<h1>This is my second PHP Program</h1>
	<h2>Even Numbers from 1 to 20 are dispalyed below</h2>
	<p>
		<?php
		/* write a program to display all even numbers between 1 and 20.
		display these even numbers separated by comma. */ 
		for ($i = 1;$i<=20;$i++)  // for loop from 1 to 20
		{
			if ($i%2 == 0)  // if remainder is zero it is even
			if ($i == 20)
				echo $i;
			else
				echo $i.",";
		}
		?>
	</p>
  </body>
</html>

In the html document, we are writing PHP code. We have used some styles for h1, h2 and p tags. In the PHP code, we use a FOR loop that iterates from 1 to 20 to check if the number is even. If the number is even, the remainder will be zero, so display it.

Please note the comments, you can use "/*" to start comments and "*/" to end comments. You can also use "//" for commenting out a single line of code. We are displaying even numbers in a <p> tag with the color blue.

PHP syntax is almost similar to any standard procedural programming language. Though you can write object-oriented programming using PHP, for our initial learning purposes, we are using the procedural PHP program.

Save this program as myprog2.php in xampp/htdocs/php. If you run it in browser as localhost/php/myprog2.php, you will see the below output:

php for beginners

It is really easy, isn't it? Now modify the above program to display even or odd against each number from 1 to 20. Like below:

1-odd
2-even
3-odd
....

Many topics are written on this website for PHP development, you can go through them, understand and post your comments. Also, think about how you can enhance them or improve them. If you are a beginner, I hope this will be helpful to you.

php code in htmlConclusion

In this topic, we have written a simple PHP program to display the name. Then we wrote another program to display the even numbers with HTML and CSS. If you are a beginner, I hope you will find it useful.

Post a Comment

Save my Name and Email id for future comments