Your First PHP Program

We know that you cannot wait anymore to create your own first PHP script. And we don’t like you to wait anymore. In this article, we’ll walk you through the process of PHP enabled page. We’ll also how PHP scripts are composed and what they mean.

In the introductory article, you learnt what PHP programming is and its applications. We also covered the essentials before beginning PHP programming. If you don’t know what the pre-requisites to enable PHP on your local computer, then we suggest you go to the introductory article first which discusses how you can turn your local computer into a virtual server as PHP only runs on the server.

If you have an Windows operating system, then you just need to download Wampserver and install it to run Apache, PHP, and MySQL on your local system. Otherwise, you’ll need to buy a Web space from a hosting company to test your PHP scripts.

So, before getting started with your first PHP script, we assume you’ve already installed Wampserver. We also assume you know how to save and launch php files. With that said, let’s move on to program your first PHP program.

Your First PHP Script

Here’s a simple example of a PHP enabled script that you can create within minutes.

<html>
<head>
<title>My First PHP Script</title>
</head>
<body>
<h1><?php echo <body> “Hello World! This is my first PHP script!”; ?></h1>
</body>
</html>

Pretty straight-forward, I guess. This is an example of a simple HTML page that contains php tags – ‘<?php’ and ‘?>’.

<?php echo “Hello World! This is my first PHP script!”; ?>

Notice that PHP script begins and end with these two tags.

These two php tags inside the HTML document tells the Web server to treat everything inside it as PHP script, while everything outside these two php tags will be treated as plain HTML tags.

The code is pretty simple. The built-in function, echo, is used to print text inside the quotation marks – “Hello World! This is my first PHP script!” You can also use another built-in function, print, instead of using echo command.

PHP is an open source programming language, meaning that you can modify and extend already available functions to built feature-rich applications.

Let’s get back to the code explanation. Did you notice a semicolon after double quotation mark? Well, semicolon is used at the end of each line of code. It’s just the coding convention in PHP. But it’s optional for single line of code.

Creating PHP Script

You will require a ASCII text editor – Notepad for Windows, TextEdit for Mac Users, and vi or Emacs for Linux users, almost all of them already installed along with your operating system.

Open up Notepad, if you’re using Windows, and copy paste the above line of code into it. And then, save it as helloWorld.php inside the root folder – WWW folder – on your hard drive.

The root folder are located at these location on your hard disk on different OS.

  • Window – C:/Program Files/Wamp/htdocs/
  • Linux – /opt/Wamp/htdocs/
  • Mac OS X  – /Applications/Wamp/htdocs/

However, if you want to test your PHP script directly on your Hosting account, then you’ll need transfer the PHP file using free FTP client – software that transfers files to and fro from hard drive to the remote web server.

Check out our article regarding how to get started with Web hosting account.

After you’ve save the file, you’ll need to test it on the browser.

Testing PHP Script

Open up your Web browser, and then type in the URL in the address field – ‘localhost’ followed by the location of the file if you’re testing on local machine, or ‘your domain name’ plus the location of the file if you’re testing on remote server.

For example,

http://localhost/helloWorld.php

or

http://www.yourdomain.com/helloWorld.php

We assume that you’ve save ‘helloWorld.php’ file on the root folder. If not, then just add the folder name where it is saved. That’s it.

If nothing’s wrong, then your browser will display a message, like this.

Hello World! This is my first PHP script!

Troubleshooting PHP Script

If you don’t see anything displayed on the browser, then PHP isn’t properly installed. Otherwise, PHP will indicate where you’ve gone wrong indicating the line number. Notice the line number, and go back to your ‘helloWorld.php’ file and try to see if you’ve missed something – a semicolon, perhaps, or a missing php closing tag.

Summary of Your First PHP Program

Well, in this article we showed you how to create a simple php file that displays a message. While doing so, we explained the basic PHP syntax – opening and ending PHP tags, print function, double quotation marks enclosing text messages.

We also covered how to save the PHP file on the root folder, and how to launch using Web browsers.

Well, in this article we showed you how to create a simple php file that displays a message. While doing so, we explained the basic PHP syntax – opening and ending PHP tags, print function, double quotation marks enclosing text messages.

We also covered how to save the PHP file on the root folder, and how to launch using Web browsers.

Leave a Reply

Your email address will not be published. Required fields are marked *