PHP is a very useful tool when it comes to writing scripts for handling automation on the web, and it is widely used by Webmasters and web developers. Free as in beer and open source, a scripting language like PHP is worth learning, especially if one is interested in creating web applications.

The purpose of this tutorial, is to give one some basics of PHP and also help them create a script and then run it on their Mac OS X computer through a local web server, such as Apache of AMPPS.

Tools needed to execute a PHP script on local Mac OS X computer

Before going any further, the first step in running code or scripts on a local machine, is the configuration of the environment which helps to use the specific technology one wants to run their code in.

There are two main tools one needs to download and setup on their local Mac OS X machine for running PHP code. The following is the two:

AMPPS is a package for computer programmers, with many tools in it, such as Apache, PHP, Mysql and all the utilities required to build and run applications on a local environment. It is mostly used by those in the web development industry, as before pushing code on production servers, developers code and test it on their local machines first.

What about the Text editor utility? A script is being written in a text editor, so one needs a tool like it to write their computer program. Text editors range from very basic, to totally advanced ones, which only hardcore computer geeks know how to use.

For the purpose of this tutorial, I am going to use a text editor which comes installed by default on my Mac OS X machine. It is highly recommended that for advanced PHP projects, one should setup an advanced PHP coding editor, but for now let’s keep it simple.

A text editor found by default on Mac OS X is more than enough for keeping up with this tutorial.

Now that the identification and explanation of the tools one needs to run PHP code on their machine is done, it is time for their installation. AMPPS requires specific steps to be installed on Mac OS X, which we have provided in one of our previous tutorials. Please make sure to read it and follow every step so you can successfully manage to install and run AMPPS on your local computer. If you encounter any problem during the setup of AMPPS on your machine, please don’t hesitate to leave a comment, we would be happy to help.

Some PHP basics

Before executing a PHP script, it is necessarily for one to have a basic knowledge on what that script does.

The first thing one should learn during their beginning of the computer programming journey, is the variables and their purpose. From what I know, a variable is used to keep track of data so it can be used by the coder in their scripts.

PHP has a very easy to understand syntax for declaring variables. One has to follow the pattern shown below to write a variable in their own PHP scripts.

$a = 13;

The above piece of code, declares the variable a and gives it a value, which in this practical case, is 13.

Note the semi colon being used, every PHP statement should end in a ;. According to some research I did on the Internet, PHP supports the following data types, which can be used with variables.

  • Integers
  • Doubles
  • Booleans
  • NULL
  • Strings
  • Arrays
  • Objects
  • Resources

Another important thing one should master during their PHP coding journey, is the comments. Comments do not get executed as normal code, as their purpose is to help the programmer comment their code, so they can make it easier for themselves and others to understand what their software is doing in specific parts of the project.

The following is a comment in PHP scripting language.

// This is a comment in PHP

There is two main types of comments in PHP programming language, single line comments and multiline comments. The above is a single line comment.

 

A multiline comment is used to give detailed explanations to the projects. The following is the syntax for it.

/*

This is a detailed comment,

it gives one details about the php project

it has many lines, it is totally different

from a single line comment

*/

Another useful PHP tool you should master as a beginner, is the echo builtin statement, which can be used to print on the screen.

echo “Welcome to PHP coding journey”;

When properly executed through a PHP script, the above statement prints a message in the screen, a string.

Write the PHP script

The knowledge presented in this tutorial about some PHP basics, is enough to write a simple script, which serves the main purpose for which this tutorial was written in the first place.

Before moving on, you should keep in mind that a PHP script, should end in the following extension.

.php

The name of the script can be anything. For the purpose of this tutorial, I am going to name mine like shown below.

coderjourney.php

Now open your text editor, open a new file and save it as a .php file.

Open the PHP script with the following syntax.

<?php

// Single line comment

?>

The entire code goes inside the PHP declaring tags.

<?php

// Single line comment

// All code goes inside the tags

?>

The script of this tutorial, is going to greet the user with a message, giving them a warm welcome in their PHP coding journey. The echo statement is what we need to use for achieving the task.

<?php

// Single line comment

// All code goes inside the tags

echo “Welcome to PHP coding journey”;

?>

Make sure to save the file, as the script is finished. Although it is a very basic one, it fits the purpose of the tutorial.

Execute the PHP script

AMPPS can help to run the PHP script, we coded during this tutorial. First, make sure the Apache and Mysql servers is running without errors.

To run a PHP script in your local machine through AMPPS, one needs to place their script in a directory inside the www of AMPPS.

The www directory is usually found in Applications/AMPPS path, like shown below.

Now, create a new folder inside AMPPS/www, name it whatever you like, mine is going to be phpjourney.

Once you have created the folder, place the script in there, the same as shown below.

 Almost done, the hard work is completed. Now visit your localhost, by copying pasting the following url on your web browser.

http://127.0.0.1/

Then visit the directory where you saved your script, click on the script and see for yourself what really happens.

Final thoughts

The main purpose of this tutorial was to help those who is stuck with a PHP script on their machine, without any idea on how to run it. Lately a friend gave me a PHP project, because he had no idea how to execute it. And it inspired me to write this very useful tutorial, especially for those who don’t have a PHP coding background.