Writing your first PHP Script and Executing It On Your Local Machine

By David |


26th Mar, 2019

Click here to share

In the previous tutorial, we studied how you can install PHP on your local machine.

We talked about the best method to have PHP running on your local machine without many configurations.

In case you missed the lesson before coming here, you can take a look at how to install PHP and XAMPP on your Windows computer so that you Cn follow along as we progress.

In this lesson, we will discuss how you can use the XAMPP installed in the previous tutorial to run your PHP script. But before them, we will first learn some basic syntax of PHP so that we will know what we are doing.


NB: It is expected that you already have your preferred editor for PHP otherwise you can read here to choose the best IDE for PHP.

Syntax in a programming language is the predefined method of combining characters to make meanings in different languages. Therefore every programming language has its own syntax.

Php syntax simply means how you combine different keyboard characters ton make meaning in PHP programming language.

A PHP script (a file containing a PHP syntax) is executed on the server sous as the one we installed before. In this case xampp.

To begin writing a PHP language you must initialize it with <?php the four characters initializes a PHP language. Therefore every other thing after the initialization will be executed as a PHP script.

But to close it you have to use this ?>. this now tells the server that the PHP script has been close. 

So everything outside of it may be plain HTML which will render accordingly.

Now let's write a simple PHP script.
using your best IDE for web development you can type the below code together.

<?php echo “Hello world!” ?>

Hint: always type the code by yourself because doing copy and paste will delay the time taken for you to master the course.

The above script will print out hello world! On the screen for the users to see.

Now that we have seen together the basics of PHP syntax in that we can open and close a PHP script lets go ahead and run the script in the next section.


To run the above scripts, first of all, save the file in the htdocs of your xampp installation sub folder. Save it with .php extension. It’s the file extension used to identify PHP files before it will be executed as a PHP file.

If you shave followed along with my previous tutorial we installed xampp at c directory of our computer. So if that is the case with you navigate to c/xampp/htdocs and save the file.

Hint: it's important that you create a new folder or directory in the htdocs root so that you will always all your files in that directory without interfering with some of the xampp follow come files.

Having done that let us now execute the file and know if we are correct.


Click the windows button on the left-hand side of your keyboard and type XAMPP.

Click on XAMPP application and it will start jus as shown below.

Click on Apache and MySQL to start the server. It will show the light green colour as shown below.

Once it is like the above image navigate to any browser on your pc and type http://localhost.com to confirm that you have started the server successfully.
Now type http://localhost.com/file.php
Assuming that you saved the above file in the c:/xampp/htdocs/file.php directory. Otherwise, follow the step and navigate the folder you saved the file under htdocs and pick the file you saved.

Now refresh you're and you will see hello world.

Incorporating PHP Within HTML

As we have learnt from the above section, PHP documents end with the .php extension. When a web server encounters this extension in a requested file, it automatically passes it to the PHP processor. Of

course, web servers are highly configurable.

Your PHP program is responsible for passing back a clean file suitable for display in a web browser. At its very simplest, a PHP document will output only HTML. To prove this, you can take any normal HTML document, such as an index.html file, and save it as index.php; it will display identically to the original.

Its assumed that you have studied HTML before coming to PHP, otherwise see first of all see our tutorials on HTML.


There are two ways in which you can add comments to your PHP code. The first approach turns ‘a single line into a comment by preceding it with a pair of forward // slashes, like this:
// This line is a comment

This version of the comment feature is a great way to temporarily remove a line of code from a program that is giving you errors. For example, you could use such a comment to hide a debugging line of code until you need it, like this:

// $x = ‘a string to be used later’;

You can also use this type of comment directly after a line of code to describe its action, like this:


$x = array(‘a’=>1,’b’=>2); // this is example of an array in php.


Single line commenting is very handy in coding so you will get to be using it a lot of times.

The next one is multiple lines commenting.

In multiple lines commenting you will be able to grab a block of code and wrap it using a commenting syntax.

Let us look at an example below


/* Any block

of code or article inside this section will

not be executed.

It can span multiple lines and it can be only one line.



Most, if not all, programmers use this construct

to temporarily comment out entire sections of code that do not work or that, for one

reason or another, they do not wish to be interpreted.

This type of multiple line commenting is also used to add class and function comments. You will learn how to make class and functions in the later tutorials.

Properties of comments in php

You can’t nest comments inside another comment. The below code will throw an error.


/*comment /*another comment*/ */


This is because the PHP interpreter won’t know where a comment ends and will display an error message.

However, if you use a program editor or IDE with syntax highlighting, this type of error is easier to spot.


Basic Syntax

Having known a lot about how to write a few things in php, the following are the things you should know very well about php syntax.

Php syntax is very easy to understand, but there are a few rules that you need to learn about its syntax and structure.


You may have noticed in the previous examples that the PHP commands ended with a semicolon, like this:


$x = ‘my name is’;

echo $x.’ ’.’Worldtok!’; //my name is Worldtok!


Probably the most common cause of errors you will encounter with PHP is forgetting this semicolon, which causes PHP to treat multiple statements like one statement, which it is unable to understand. This leads to a “Parse error” message.

The $ symbol
The $ symbol has come to be used in many different ways by 
different programming languages. For example, if you have ever written in the
BASIC language, you will have used the $ to terminate variable names to
denote them as strings.

In PHP, however, you must place a $ in front of all variables. This is required to make the PHP parser faster, as it instantly knows whenever it comes across a variable.

Whether your variables are numbers, strings, or arrays, they should all look something this


$myinteger = 1;

$astring = "Hello";

$anarray = array("One", "Two", "Three");


And really, that’s pretty much all the syntax that you have to remember.

Just one thing with the $ sign is that you cannot use it to precede an integer. This is because php variable cannot start with an integer.


Unlike languages such as Python, which is very strict about code indentation,

PHP leaves you completely free to use all the indenting and spacing you like.

In fact, sensible use of what is called whitespace is generally encouraged (along with comprehensive commenting) to help you understand your code when you come back to it.It also helps other programmers when they have to maintain your code.

That is it for this tutorial and I’m pretty sure that you understand all the basics of php syntax and how you can write it now.

Please if you have understood anything in this tutorial support us by sharing it with your friends who might need it, and if you have any question please add it to the comment box below and I will be glad to reply you.

Leave a Reply


Total of 0 Comment