Categories
PHP

Hello PHP!

PHP is a great language to develop server-side scripts for data-centric Web applications. PHP can run on all computers and most if not all commercial Web hosting services offer it as part of their platforms.

When studying any coding language, the first thing to learn is the correct syntax to write correct programs in this language. Are instructions separated by newlines or semicolumns? How do we declare variables? How do we set and get values from them? Let’s get started with PHP.

First things first, the development environment is important. Since PHP is a scripting language, we can use any text editor that has proper syntax highlighting for PHP to develop with this language. I like Sublime Text, but there are many editors that provide a great development environment for PHP.

Also part of the development environment is the runtime environment. To this effect, I suggest downloading and installing XAMPP.

Let’s get started

Now that you’ve installed XAMPP (I will assume a default installation on the C: drive of your Windows-run computer) and your favorite text editor, we will write a simple Hello World! program.

First, navigate to C:\xampp\htdocs. This is the root folder where the Apache server instance we will later run will fetch your Web resources. Select all files preinstalled in this folder and delete them. Now create a new file: index.php. In this file, paste the following code:

  1. <?php
  2.    echo 'Hello World!';
  3. ?>

Save your file and now, we will run this script.

Now, let’s start XAMPP: Go to the C:\xampp folder and run the xampp-control.exe program. On the line labeled Apache, click the “Start” button. The Apache label should turn to light green. If it does not, then you are likely to have a port conflict preventing Apache from starting. To find out which program this is, click on the Netstat button on the right.

Assuming Apache is running correctly, we will now run our Hello World! program by starting our favorite browser ant directing it to http://localhost/. You should see “Hello World!” displayed in the browser window.

To explain what’s going on, we have <?php and ?> tags delimiting the PHP code. The echo instruction will output the strings given to it as a parameter. In this case, we are providing the 'Hello World?' string literal.

By Michel

My name is Michel Paquette. I currently teach my students how to create data-driven Web applications at Vanier College, in Montreal.

My GitHub page contains a few examples of Web applications. Also consult my YouTube channels: @CSTutoringDotCa and @MichelPaquette.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.