How to write PHP code with examples for 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. I will assume you have basic programming concept.

Before working on PHP program, please make sure PHP environment is setup 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 for Windows with setup 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 knowledge about programming.
  3. Your computer is ready to run PHP program . If not please read the topics How to install xampp for Windows with setup for PHP development
  4. This topic is not to teach you PHP, this is just to show you how you can start writing a PHP program along with HTML and CSS. If you want to learn PHP you can always refer to a learning website, for example W3Schools.com.

php for beginnersProgram Examples

Choose a PHP Editor

You need a good editor to write PHP program. Notepad++ and Sublime Text editors are popular to write 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.

Php program runs on the server. You need a webserver to run PHP in the server. In our case we are using XAMPP server, it has Apache web server. So, we will use localhost server to run our PHP program.

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

PHP code Example1 - Print your name

Let us take a look at the below PHP code:

myprog1.php

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

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

Now, create a folder named "php" under your xampp/htdocs folder, copy and paste the above php code in a file myprog1.php and save it in your xampp/htdocs/php folder. For a PHP program, file 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. Open the browser and type localhost/php/myprog1.php and enter. 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 - Print all even numbers from 1 to 20

Now, let us write a PHP program to print 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 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 output looks little better. See below program:

myprog2.php

Here we are using a standard html document and in this html code we are writing PHP code. We have used some style for h1, h2 and p tags. In the PHP code we are using a for-loop from 1 to 20 to check if number is even. If the number is even, remainder will be zero, so print 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 or putting comments within your code. We are printing even numbers in a <p> tag with color blue.

PHP syntax are almost similar to any standard procedural programming language. Though you can write Object Oriented programming using PHP, for our initial learning purpose we are using procedural PHP program.

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

php for beginners

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

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

There are many topics written in this website, you can go through them, understand and think how you can enhance them or improve them. If you are a beginner, I hope this will be helpful for you.

Watch YouTube Video

how to write a php codeDownload Source Code

I have put all source codes in a zip file. You can download it by clicking on the Download button below. You do not need to register yourself to download it. You can directly use the codes or you can modify them as per your requirements.

php code in htmlConclusion

Learning PHP is the easiest thing you can start with your web development. I thought this would be helpful for you to begin with. Hope you find it useful.