Web Server PHP
practically works with almost all types of web servers. If your web host supports PHP, the server will automatically parse the scripts placed in your web directory. You can turn your computer into a web server by installing a web server program. The most commonly used is the Apache server. You can download it for free at this website: https://httpd.apache.org/download.cgi. Database
Virtually any type of database software can be used with PHP. This includes Sybase, Oracle, and MySQL. The most popular open-source RDBMS today is MySQL. For purposes of this training, you can download it for free at
You will need a PHP parser to process scripts. A parser generates HTML output that will be sent to the browser. You can download it for free at the PHP website.
XAMPP is a beginner-friendly open source cross-platform distribution by Apache. It contains the important tools you need to set up a web server for testing purposes: Apache (server application), MySQL (database application), and PHP (scripting language). The X in the acronym stands for cross-platform. This indicates that XAMPP can be used equally well on Windows, Mac, or Linux.
Since XAMPP’s components are also widely used in actual web server deployment, you will find it extremely easy to switch to a live server. XAMPP includes the following key components:
To download an executable file that you can extract to set up XAMPP on your computer, you will need to select a version on the download page of Apache friends: https://www.apachefriends.org/download.html
For this tutorial, you may download the version of 7.1.1 / PHP 7.1.1. Simply click on the executable file to start the installation process. The setup wizard will display the different components that will be installed which consist of the following:
FileZilla FTP Server
Mercury Mail Server
Except for Apache and PHP, you can uncheck components that you don’t intend to use. This tutorial will accept the default components and install everything. The next dialogue will prompt you to provide the directory in which you want
XAMPP to be installed. The default installation folder is C:\xampp. Once the installation is done, you will be prompted with an option to start the Control Panel. Select that checkbox to begin working with XAMPP.
XAMPP Control Panel
XAMPP’s control panel allows you to control all installed modules. You can use it to start or stop a module, access the Windows Explorer, and view all running operations.
Testing XAMPP Installation
You can check if XAMPP had been successfully installed on your computer by launching Apache web server and writing/running a simple PHP script. First, click on the XAMPP icon. You will be asked to choose the language and save your preference. You will then be taken to the Control Panel. To start the Apache module, you can click on Start beneath the Actions caption. Next, open the web browser then type
Another option is to type
You will see the dashboard and the message that you have successfully installed XAMPP on the system and that you can now start using its components.
To check if PHP was installed successfully, you can write a simple PHP script using Notepad. Type the following:
<?php echo 'Hello, World!'; ?>
You can save this document as try.php in the htdocs folder within the xampp installation. For example, if xampp was installed in drive c, you will save your new file in c:\xampp\htdocs. Save the file as Al Files. Next, type
on your browser. You should see the following:
You have just created your first ever PHP program using a local Apache webserver. You can start learning more complex and exciting web apps with the succeeding articles of this site web.
While PHP can be forgiving, learning its syntax will ensure that your code will run as expected. This chapter will discuss the important aspects of PHP syntax. First of all, you will need to tell the PHP parsing engine that it is dealing with a PHP element. There are different ways of doing this.
The Basic Syntax
You can place a PHP script virtually anywhere in a web document. The most universally accepted tag style for introducing a PHP script is by enclosing the PHP element with the start tag
Variables and Data Types
To start our learning with PHP, we must first look at the core building blocks that will be used to build every project. In our applications, we will always need a way to store our data temporarily (in our case, we call the storage methods variables).
Variables are defined as follows:
$VARIABLENAME = “VALUE”;
As you can see in the preceding example, variables start off using the $ symbol, followed by the name, and the value is assigned using the assignment operator.
Here, we have a variable named VARIABLENAME, with a string value of VALUE.
Variable names cannot start with numbers or special symbols, besides the $ sign, used to define the variable itself.
PHP is one of the few languages that doesn’t require you to declare a data type before assigning a value.
Working with Variables
In this section, we will illustrate a real-world example of using variables in a program. We will start off by creating a variable to store a user’s name:
- Open your code editor
- Create a new file and name it variables.php
- Enter the following, and save your document :
<?php $name = "John Doe"; $age = 25; $hourlyRate = 10.50; $hours = 40; echo $name . " is " . $age . " years old.\n"; echo $name . " makes $" . $hourlyRate . " an hour. \n"; echo $name . " worked " . $hours . " this week. \n"; ?>
4. Open your working directory in the Terminal
5. Enter the following command, and then press Enter
We will now have a look at the various operators that are available in PHP.
In the section on variables, we saw the = symbol, which, in PHP, is known as an assignment operator. This operator does exactly what the name implies, allowing you to give a variable value. The first operators are known as comparison operators. Comparison operators allow you to compare two values within a given conditional case.
Inside of the set of comparison, operators are the equal, identical, not equal, not identical, less than, and greater than operators.
Open your working directory in the Terminal
Enter the following command, and then press Enter
Up next are logical operators. Logical operators are used to checking for multiple cases at one time. The set of logical operators gives you the NOT, AND, and OR operators.
In your program, you will sometimes need to do a little math; this is where mathematical operators come in. They give you the ability to add, subtract, multiply,
divide, and get the remainder of two divided numbers
Let’s try to use these operators in PHP
Combining Variables and Operators
In this section, we will be extending our previous example to calculate the annual salary of our users. Here we go with the steps
1- Open your code editor.
2- Create a new file and name it operators.php.
3- To get started, copy the contents from our variables.php document.
4- Now, we will add an additional variable to the document, which will hold the number of weeks: $weeks = 52;
5- Next, we will use the multiplication operator to calculate our weekly pay and assign it to a new variable:
$weeklyPay = $hourlyRate * $hours;
6- Now, with our weekly pay rate, we can calculate our salary:
$salary = $weeks * $weeklyPay;
7- Our last step is to display our final calculations:
echo $name . ” will make $” . $salary . ” this year.\n”;
Your final document should look like the following:
<?php $name = "John Doe"; $age = 25; $hourlyRate = 10.50; $hours = 40; echo $name . " is " . $age . " years 01d.\n"; echo $name . " makes $" . $hourlyRate . " an hour. \n"; echo $name . " worked " . $hours . " this week.\n"; $weeks = 52; $weeklypay = $hourlyRate * $hours; $salary = $weeks * $weeklyPay; echo $name . " will make $" . $salary . "this year"; ?>
8- Next, we’ll open our directory in our Terminal and run the following command:
9- We should now see our data being displayed
Now that we have a foundation for operators, we can start to use them in what is known as conditionals. Conditionals allow you to control the ﬂow of your program, and they come in the form of if statements.
A basic if a statement is represented as follows:
Inside of the parentheses, you will hold the condition that is required to activate the code within the curly braces.
Additionally, you can add an else statement, which will allow you to give alternate code to run if the condition isn’t met:
A helpful function to use with conditionals is the empty function.
The empty function is used to check whether a variable is empty