Have you been looking for a way to learn PHP? If yes, this our PHP tutorial for beginners will teach you PHP language and you will be able to write PHP code, run PHP code by yourself after going through this our PHP tutorial for beginners. However, we would make this PHP tutorial for beginners to be simple and easy for beginners. So, open your PC and read the entire chapters of this PHP tutorial for beginners. Well, this PHP tutorial for beginners is fragmented into three parts. However, after completing the three parts you are expected to write your first PHP application. If possible you can send a copy to us…Cheers!
Table Of Contents
3.0 Installation Of XAMPP And Sublime Text.
3.1 Introduction To PHP.
3.2 Variables, Data Types, and Operators.
3.3 Require, Include, and Comments.
3.4 PHP Arrays.
3.5 PHP loop (Foreach, For, Do while, While).
3.6 Conditioning In PHP (switch cases and if else).
3.7 PHP strings.
3.8 PHP Functions.
3.9 PHP Forms.
3.10 File Processing.
3.11 Cookies And Sessions.
3.12 Error Handling.
3.13 Advance PHP(Database handling, OOPS, PHP date, Mail, XML, DOM, Parsers).
3.14 PHP Project.
3.15 Reference & Resources.
Installation Of Xampp And Sublime Text
For PHP to run effectively on your local machine, you will need to install and configure Apache, PHP, and MYSQL if you will need to use a database. This may be too complicated for beginners and that is why we recommend XAMPP. Though there are other alternatives like WAMP, LAMP e.t.c XAMPP is easier and less complicated for users. XAMPP is known to be open source and the cross-platform web server. It comprises the Perl package, PHP package, and MySQL database which is powered by the apache. We would like to know more about Xampp isn’t it? Why Is Apache necessary? We will answer these questions with a proper explanation. Just calm down and get your PC ready for installation. Well, you don’t have to learn much about Apache before you become a PHP developer! As I have said earlier, XAMPP is a cross-platform web server– it can work on the Windows server and Unix server.
Apache, Apache Tomcat, And Nginx
Apache is known to be a web server that connects the server and the user’s browser. When you access a web page from your browser, there is a web server that renders the response to users from the server after sending a request(we can say it is a middleman between the users’ browser and the server). However, Apache is not only a webserver. We also have another web server such as Nginx and Apache Tomcat. No doubt, Apache is being used on 40% of the internet website in the world. But there is an issue with Apache HTTP server that birthed Nginx. Even though Apache is the most commonly used web server, you can’t use it to handle 10,000 user requests at once which is called c10k issue. Nginx has come to resolve this problem, so if you are creating a web project having huge traffic, Apache won’t give you the best performance in this regard. Obviously, that is the reason many top-class websites use Nginx.
Tomcat is another web server owned by Apache software foundation. It is mainly developed to powers the Java applications, Java pages, WebSocket. However, we are only using Apache and Xampp can serve the purpose of Apache.
How To Download And Install Xampp
Visit http://www.apachefriends.org/en/xampp-windows.html to download the Xampp installer.

Run the Xampp set up after downloading the installer. You will see a dialog window usually “warning message” appears.


For those using Windows 7 or window Vista, you will have to deactivate the User Account Control feature. You can simply do this by following the steps: navigate to your Control Panel –> User Accounts –> Change User Access Control settings.

Immediately you deactivate User Account Control, you can now click the OK button on the warning message.

After that, click the next button on the next screen.
You will be taken to the next screen, however, change the installation path to your preferred location and click the next button.

Select the Mysql and Apache. If you will need to use FTP client simply select FileZilla and click the Install button.
After the complete installation, you will see the finish button. Click it!

By now you have successfully installed XAMPP on your local machine. We have to understand how it works, and also configure the Apache. Let us assume that you installed the Xampp on your window local drive(C:). So, you will see the xampp folder inside the local drive. Open the Xampp folder, we are focusing on three main folders inside Xampp which are:

Htdocs: For online server, the primary webroot is public_html but for Xampp on your computer the webroot is htdocs. So, inside your htdocs, you will have your PHP files.
MySQL: Inside this folder, you have all the files relating to the MySQL database engine, the default port is 3306.
PHP: Inside this folder, you have the PHP installation files such as php.ini. This is used where you can configure your PHP behaviour on the server.
Considering your port is very important! The default port for Apache is 80, so make sure that the port is not in use by other programs.

You will also have to launch the Xampp where you will be taken to the control panel. Start the Apache and the Mysql. The fade green background colour indicates that Apache and Mysql are running. And the green tick inside the box shows that Apache and Mysql are installed as service. We have successfully installed Xampp on our window. So, let us install Sublime Text 3– with the sublime text we can write and edit our PHP code. Well, there are other alternatives like Notepad++, Netbeans PHP Idle, Visual Studio editor, e.t.c but we are using sublime text 3 in this tutorial.
How To Install Sublime Text 3
We are going to teach you how to install sublime text on your window and other operating systems in this tutorial.
Installation Of Sublime Text 3 On Windows
Step 1 − Visit https://www.sublimetext.com/3 to download the .exe package.
Step 2 − The next thing is to run the .exe file as you prepare to install your sublime text. Click the Next button.
Step 3 − Select the destination path, this is where to install Sublime Text3. Click on the Next button.
Step 4 − Check properly to verify the destination pathway. Click on the Install button.
Step 5 − Click the Finish button as the installation is completed.
Step 6 − Open the sublime text 3 after a successful installation.
Installation Of Sublime Text 3 On Linux
The above procedures are only meant for Windows, but we are also going to teach you how to install it on Linux.
Step 1 − We are going to use the command line terminal, to install the packages for Sublime Text editor, open the command line terminal and input the command below:
sudo add-apt-repository ppa:webupd8team/Sublime-Text-3
Step 2 − Make use of this command line below to update the package:
sudo apt-get update
Step 3 − After you have run the above commands, you will have to use the below command to install the Sublime Text repository:
sudo apt-get install Sublime-Text
You have successfully installed the Sublime Text 3 on your Linux and it ready to be launched.
Installation Of Sublime Text 3 On OSX
If you are using OSX operating system other than Windows and Linux, this simple procedure will get your Sublime Text ready in 2 mins.
You only have to download Sublime Text 3 .dmg file and open the file. Drag and drop the application into the folder you will like to be seeing it. Finally, Launch your Sublime Text 3 editor.
Introduction To PHP
PHP which means HyperText Pre-processor is one of the most used web programming languages. It is a server-side language powering almost 75% of websites on the internet(According to Wikipedia). However, there are many frameworks built with PHP language which include Laravel, Symphony, Codeigniter, Yii2, and a lot more. Well, it is good to learn any of the frameworks but you have to understand PHP very well in which we are going to teach in this crash course. There are some basic facts you should know before we proceed.
- PHP is a server-side scripting language that can be interpreted by the server.
- The PHP file is usually saved with .php extension, for example, new.php is a PHP file that contains the PHP codings.
- You can also use HTML tags and JavaScript inside your PHP as it will work perfectly.
- PHP can easily run on almost all web hosting, unlike JSP and ASP.
- PHP has a robust community in which you can seek help concerning PHP.
- PHP can be used with different databases like Postgres and MS SQL, but it is specifically built to support MYSQL. Using PHP with MYSQL is easy and effective.
- PHP is a case sensitive programming language which means “ECHO” is not the same with “echo” we usually make use of lower case(small letters). Although, the tags can be in upper case but I will advise you to make your coding and tags be in lower case.
- PHP is a free open source language I.e you don’t have to pay before using it and you can modify and use it for commercial purposes.
- PHP uses the tag, and before you start writing PHP language you will have to declare it using .
- Finally, after each line of PHP code(statements), it is necessary to end it with a semi-colon(;).
Writing Your First PHP Code
Open your XAMPP control panel, and start the Apache & MYSQL.
Open Your Sublime Text 3, On the top menu bar, Click File and open the folder of Xampp –> htdocs –> Myfirstcode.
Note: You must have created a new folder inside your htdocs.
After you have opened the folder inside the sublime Text, then you can write your first PHP code inside the editor. We are writing Hello World! Placed this code inside the editor:
<?php echo "Hello world"; ?>
Save the code as a file inside your folder “Myfirstcode”. You can do this by pressing CTRL + S or you can navigate to the top menu bar, click the File button and scroll down to Save the file. Put your file name as index.php and hit the save button.
Open your browser preferably Chrome or Firefox, and in the web address bar; enter your localhost address pointing to the file you created. ” localhost/myfirstcode/index.php”
You should get the result as below:
Hello world
If you have done this perfectly you can move to the next chapter. Make sure you get the result as above before you continue. However, you can ask your question using the comment box to ask your question if you need any help.
Variables, Data Types, and Operators.
We are going to talk about data types, variables, and PHP operators. We are starting with the data types:
Strings: the strings are the alpha-numerical characters.
Integers: the integers are the whole numbers. Example 3, 18, 45, 76, e.t.c.
Boolean: the boolean is false or true value.
Floating point: the floating point numbers having decimal points.
Variables and Data
Variables are mainly used to store data that can be used at any time. We have the local variable(a variable that can only be used within the script which it was stated) and global variable(a variable that can be used across the scripts of the applications. If you are defining any variable you must start with the dollar sign ($), and variables are case sensitive which means that the variable written with the lower case is not the same with the one written with uppercase even if they have the same letters. Another thing you must know about the variable is:
Variable must start with letters after the dollar sign, e.g $dev1(correct), $1dev(incorrect).
You must avoid spacing between variables instead use an underscore(_) to fill the space, e.g $my_dev(correct), $my dev(incorrect).
Now let us use a variable to define our data;
<?php $my_dev = 54; echo $my_dev; ?>
Result:
54
<?php $x = 3; $y = 3.5; $z = $x + $y; $z = $x + (int) $y; echo $z; ?>
Result:
6
The echo is used to print the result of $z. The data are stored as a variable $x as 3, $y as 3.5. It can also store data such as strings. Example,
<?php $dev ="I am a developer"; echo $dev; ?>
Result:
I am a developer
We also have some variable constant whose value can’t be edited or changed. This constant is called the PHP constants, for example. When you are defining your constant, it should begin with underscore or letter without ($) sign.
<?php define("NAME", "Firstdevlab"); echo NAME; ?>
With the above script, you have defined constant variable for NAME and it can’t be changed across the entire script because constant is always global and it can also be an array.
PHP Operators
We usually use operators while coding, some of these operators are arithmetic operators, logical operators, assignment operators, and comparison operators.
Arithmetic Operators
Arithmetic Operators includes (+, -, *, %, /, ++, –)
+ (addition), -(subtraction), *(multiplication), %(percentage), /(division), ++(plus), –(substract).
<html> <head> <title>Arithmetical Operators</title> </head> <body> <?php $x = 22; $y = 10; $z = $x + $y; echo "Addtion Result: $z <br/>"; $z = $a - $b; echo "Substraction Result: $z <br/>"; $z = $a * $b; echo "Multiplication Result: $z <br/>"; $z = $a / $b; echo "Division Result: $z <br/>"; $z = $a % $b; echo "Modulus Operation Result: $z <br/>"; $z = $a++; echo "Increment Result: $z <br/>"; $z = $a--; echo "Decrement Result: $z <br/>"; ?> </body> </html>
Result:
Addtion Operation Result: 32
Substraction Operation Result: 12
Multiplication Operation Result: 220
Division Operation Result: 2.2
Modulus Operation Result: 2
Increment Operation Result: 42
Decrement Operation Result: 43
Comparison Operators
Comparison operators include the following: (<, >, ==, !=, >=, <= ).
<(less than), >(greater than), ==(equal to), !=(not equal to), >=(greater than and equal to), <=(less than equal to).
<html> <head> <title>Comparison Operators</title> </head> <body> <?php $x = 22; $y = 10; if( $x == $y ) { echo "DEV1 : x is equal to y<br/>"; }else { echo "DEV1 : x is not equal to y<br/>"; } if( $x > $y ) { echo "DEV2 : x is greater than y<br/>"; }else { echo "DEV2 : x is not greater than y<br/>"; } if( $x < $y ) { echo "DEV3 : x is less than y<br/>"; }else { echo "DEV3 : x is not less than y<br/>"; } if( $x != $y ) { echo "DEV4 : x is not equal to y<br/>"; }else { echo "DEV4 : x is equal to y<br/>"; } if( $x >= $y ) { echo "DEV5 : x is either greater than or equal to y<br/>"; }else { echo "DEV5 : x is neither greater than nor equal to y<br/>"; } if( $x <= $y ) { echo "DEV6 : x is either less than or equal to y<br/>"; }else { echo "DEV6 : x is neither less than nor equal to y<br/>"; } ?> </body> </html>
Result:
DEV1 : x is not equal to y DEV2 : x is greater than y DEV3 : x is not less than y DEV4 : x is not equal to y DEV5 : x is either greater than or equal to y DEV6 : x is neither less than nor equal to y
Logical Operators
Logical operators include the following: (And, Or, !, &&, ||)
And: It is an operator and logical use to make a condition to be true when the two operands are true.
Or: It is an operator and logical which condition is true when any of the operands is not 0.
! This is logical NOT operator only use to reverse the state of the operand. However, making the condition that is true to be false.
&& This is a Logical AND operator which makes a condition to be true when the two operands are not zero.
|| This is a Logical OR operator which makes a condition to be true when any of the two operands are not 0.
<html> <head> <title>Logical Operators</title> </head> <body> <?php $x = 42; $y = 0; if( $x && $y ) { echo "DEV1 : Both x and y are true<br/>"; }else{ echo "DEV1 : Either x or y is false<br/>"; } if( $x and $y ) { echo "DEV2 : Both x and y are true<br/>"; }else{ echo "DEV1 : Either x or y is false<br/>"; } if( $x || $y ) { echo "DEV3 : Either x or y is true<br/>"; }else{ echo "DEV3 : Both x and y are false<br/>"; } if( $x or $y ) { echo "DEV4 : Either x or y is true<br/>"; }else { echo "DEV4 : Both x and y are false<br/>"; } $x = 10; $y = 20; if( $x ) { echo "DEV5 : x is true <br/>"; }else { echo "DEV5 : x is false<br/>"; } if( $y ) { echo "DEV6 : y is true <br/>"; }else { echo "DEV6 : y is false<br/>"; } if( !$x ) { echo "DEV7 : x is true <br/>"; }else { echo "DEV7 : x is false<br/>"; } if( !$y ) { echo "DEV8 : y is true <br/>"; }else { echo "DEV8 : y is false<br/>"; } ?> </body> </html>
Result:
DEV1 : Either x or y is false DEV2 : Either x or y is false DEV3 : Either x or y is true DEV4 : Either x or y is true DEV5 : x is true DEV6 : y is true DEV7 : x is false DEV8 : y is false
Assignment Operators
We also have assignment operators supported by PHP. However, we would only list them and show you how it can be used in a script. Assignment operators include: (=, -=, +=, /=, %=, ×=)
There is right and left operands, these operators will carry out their respective arithmetic operation AND the assignment operation which assigns the arithmetic result to the left operand. E.g $a = 2; $b = 3; $a += $b; ( This means 3 + 2 which is 5 And the result is equivalent to $a if you echo $a it will give you 5 as result) See full operations below:
<html> <head> <title>Assignment Operators</title> </head> <body> <?php $x = 42; $y = 20; $z = $a + $b; echo "Addtion Operation Result: $z <br/>"; $z += $x; echo "Add AND Assigment Operation Result: $z <br/>"; $z -= $x; echo "Subtract AND Assignment Operation Result: $z <br/>"; $z *= $x; echo "Multiply AND Assignment Operation Result: $z <br/>"; $z /= $x; echo "Division AND Assignment Operation Result: $z <br/>"; $z %= $x; echo "Modulus AND Assignment Operation Result: $z <br/>"; ?> </body> </html>
Addtion Operation Result: 62
Add AND Assigment Operation Result: 104
Subtract AND Assignment Operation Result: 62
Multiply AND Assignment Operation Result: 2604
Division AND Assignment Operation Result: 62
Modulus AND Assignment Operation Result: 20
Result From The Above Script
READ MORE: HOW TO CREATE AN E-COMMERCE WEBSITE USING WORDPRESS
Require, Include, and Comments
In PHP, we use a comment to describe or explain what the particular code is all about for better understanding. It also makes your code to be clean and easy to read for other developers going through your script. Before you describe or explain your code using the human language using the comment tags ( // ) after the two strokes you can write a single line description. For multiple lines ( /*…….*/ ).
//This is my first comment /* I am a boy I am a developer PHP is cool */
Include and Require have two variations (include and include_once) and (require and require_once). include or include_once can be used to include other PHP files inside another PHP file. Similarly, require or require_once can also be used to include a file into the PHP file. The main difference between the two is: include always throws a warning if there is an error while require doesn’t throw a warning. Require script will stop executing when there is an error while the includes script will continue with the execusion. This is the reason most developers prefer using require or require_once for config file having the database connection files. You may ask how include and require are being used in PHP. Take, for example, you want to create a website and you want to include the same head navigation in all web pages. All you need to do is to create a PHP file named header.php in which inside the file you will put your common file that will appear in all your pages. Obviously, there are common files that you will like to show on all pages like contact us, about us, Home, e.t.c. You can save yourself stress by using the include or require statement instead of writing code for each page header.
header.php
<html> <head> <a href="/index.php">Home</a> <a href="/aboutus.php">About us</a> <a href="/services.php">Services</a> <a href="/contactus.php">Contact Us</a> </head> </html>
Now use include statement to include the above file script inside index.php
<?php include 'header.php'; ?>
Config.php
<?php $config['host'] = 'localhost'; $config['db'] = 'my_database'; $config['uid'] = 'root'; $config['password'] = ''; ?>
Using the require statement to include the config.php in the model.php
<?php require 'config.php'; //require the config file //other code for connecting to the database ?>
You can see how we use the comment ( // ) to describe the functions of the script. It makes our code to be easy to understand.
PHP Arrays
An array is a variable that can be used to store more than one values as a single variable. Take, for instance, if you want to store 50 numbers as a variable, you may want to define the whole numbers as different variables like $num1 = 1; $num2 = 2;…..$num50 = 50; Array will save you this stress, the whole 50 numbers can have just one variable using array.
However, we have 3 different types of arrays which are explained below:
- Numeric array − This is an array having a numeric index.
- Associative array − This is an array having strings as an index.
- Multidimensional array − This is an array that is having one or more arrays.
We can use array() to create an array function. Below are examples of the array.
<html> <body> <?php /* First method to create array. */ $numbers = array( 1, 2, 3, 4, 5); foreach( $numbers as $value ) { echo "Number is $value <br />"; } /* Second method to create array. */ $numbers[0] = "one"; $numbers[1] = "two"; $numbers[2] = "three"; $numbers[3] = "four"; $numbers[4] = "five"; foreach( $numbers as $value ) { echo "Number is $value <br />"; } ?> </body> </html>
Result:
Number is 1 Number is 2 Number is 3 Number is 4 Number is 5 Number is one Number is two Number is three Number is four Number is five
<html> <body> <?php /* First method to associate create array. */ $salaries = array("john" => 2000, "josua" => 1000, "james" => 500); echo "The salary of John is ". $salaries['john'] . "<br />"; echo "The salary of Joshua is ". $salaries['joshua']. "<br />"; echo "The salary of James is ". $salaries['james']. "<br />"; /* Second method to create array. */ $salaries['john'] = "high"; $salaries['joshua'] = "medium"; $salaries['james'] = "low"; echo "The salary of John is ". $salaries['john'] . "<br />"; echo "The salary of Joshua is ". $salaries['joshua']. "<br />"; echo "The salary of James is ". $salaries['james']. "<br />"; ?> </body> </html>
Result:
The salary of John is 2000 The salary of Joshua is 1000 The salary of James is 500 The salary of John is high The salary of Joshua is medium The salary of James is low
<html> <body> <?php $marks = array( "John" => array ( "physics" => 25, "maths" => 30, "chemistry" => 39 ), "Joshua" => array ( "physics" => 30, "maths" => 15, "chemistry" => 29 ), "James" => array ( "physics" => 31, "maths" => 22, "chemistry" => 29 ) ); /* Accessing multi-dimensional array values */ echo "Marks for John in physics : " ; echo $marks['John']['physics'] . "<br />"; echo "Marks for Joshua in maths : "; echo $marks['Joshua']['maths'] . "<br />"; echo "Marks for James in chemistry : " ; echo $marks['James']['chemistry'] . "<br />"; ?> </body> </html>
Result:
Marks for John in physics : 25 Marks for Joshua in maths : 15 Marks for James in chemistry : 29
PHP loop (Foreach, For, Do while, While)
PHP tutorial for beginners is meant to teach beginners all they should know. Obviously, that is why we have to touch this area. You may want a block of code to run many times if the condition is true without repeating the same code line in the script. The best way to do this Is to use the PHP loop. We have different types of PHP loop which is as below:
For: This will loop the block of code for a number of times which can be specified. It will run the block of code for a particular number of times.
While: This has to do with condition! It will loop a block of code only when the specified condition is true.
Foreach: It will loop the block of code to render each elements in the array.
do…while: It will loop the block of code for just one time and it will repeat it only if the condition is true.
Examples are below:
For:
<?php for ($i = 0; $i < 5; $i++){ $books = 10 * $i; echo "The Number of 10 * $i books is $books <br/>"; } ?>
Result:
The number of 10 x 0 books is 0
The number of 10 x 1 books is 10
The number of 10 x 2 books is 20
The number of 10 x 3 books is 30
The number of 10 x 4 books is 40
The number of 10 x 5 books is 50
foreach:
<?php $animals = array("Tiger","Wolf","Goat","Leopard","Monkey"); foreach($animals as $animal){ echo $animal . "<br>"; } ?>
Result:
Tiger
Wolf
Goat
Leopard
Monkey
While:
<?php $i = 0; while ($i < 5){ echo $i + 1 . "<br>"; $i++; }
Result:
1
2
3
4
5
do…while:
<?php $i = 7; do{ echo "$i is"." <br>"; } while($i < 7); ?>
Result:
7
Conditioning In PHP (switch cases and if else)
In programming, we often execute some functions conditionally. PHP has made it easy to condition our statement by using “if, if…else, if…elseif…else, switch”
if
: It will run code when a specified condition is trueif...else
: It will run code when a specified condition is true. It will also run another code if the condition is also false- if…elseif…else: It will run different codes if there are many conditions that are more than two.
a switch
: The switch case is used to selects a particular block of code to be run or executed out of other blocks of code.
Examples are below: We are going to use time system to assigned greetings
If:
<?php $t = date("H"); if ($t < "12") { echo "Good morning!"; } ?>
If…else:
<?php $t = date("H"); if ($t < "12") { echo "Good morning!"; } else { echo "Good Afternoon!"; } ?>
if…elseif…else:
<?php $t = date("H"); if ($t < "10") { echo "Good morning!"; } elseif ($t < "20") { echo "Good Afternoon!"; } else { echo "Good night!"; } ?>
Switch:
<?php $favcolor = "red"; switch ($favcolor) { case "red": echo "Your favorite color is red!"; break; case "blue": echo "Your favorite color is blue!"; break; case "green": echo "Your favorite color is green!"; break; default: echo "Your favorite color is neither red, blue, nor green!"; } ?>
Conclusion
We have just finished part one of the PHP tutorial for beginners. In the part two, we will cover the PHP strings, PHP Functions, PHP Forms, File Processing, Cookies And Sessions, Error Handling.