PHP Tutorials

PHP Tutorials

PHP Introduction:

PHP is a powerful server-side scripting language for creating dynamic and interactive websites.

PHP is the widely-used, free, and efficient alternative to competitors such as Microsoft’s ASP. PHP is perfectly suited for Web development and can be embedded directly into the HTML code

The PHP syntax is very similar to Perl and C. PHP is often used together with Apache (web server) on various operating systems. It also supports ISAPI and can be used with Microsoft’s IIS on Windows.

A PHP file may contain text, HTML tags and scripts. Scripts in a PHP file are executed on the server. Here are some more points about PHP

  • PHP can be installed on any web server: Apache, IIS, Netscape, etc
  • PHP can be installed on any OS: Unix, Linux, Windows, MacOS, etc
  • XAMPP is a free and open source cross-platform web server package, consisting mainly of the
    Apache HTTP Server, MySQL database, and interpreters for scripts written in the PHP and Perl
    programming languages.
  • It is used as a development tool, to allow website designers and programmers to test their work
    on their own computers without any access to the Internet.
  • XAMPP’s name is an acronym for
    X (meaning cross-platform)
    Apache HTTP Server
  • LAMP package is used for Linux OS
  • WAMP package is used for Windows OS

PHP File:

  • .PHP files may contain text, HTML tags and scripts
  • .PHP files are returned to the browser as plain HTML
  • .PHP files have a file extension of “.php”, “.php3”, or “.phtml”

What is PHP?

PHP is a server side scripting language which stands for PHP Hypertext Preprocessor. PHP is more popular for its HTML embedded feature. Originally created by Rasmus lerdorf in 1993-94, the first name of php is Personal Home Page.

PHP used in many websites as programming language over the world and there are many reasons behind this. For e.g. It is platform independent, allow to go for less expensive hosting, Open Source,large community. PHP supports large number of CMS like WordPress, Magento, Joomla as well as frameworks like zend, larvel, codeigniter, cakephp. So Many famous website is developed in PHP like facebook, yahoo, wikipedia, etc. According to survey happened in may 2015, 81.9% websites used PHP as their server side programming language. See here : Server-side Programming Language Survey

Why PHP ?

  • PHP runs on different platforms (Windows, Linux, Unix, etc.)
  • PHP is compatible with almost all servers used today (Apache, IIS, etc.)
  • PHP is FREE to download from the official PHP resource:
  • PHP is easy to learn and runs efficiently on the server side

Below is Top 5 Advantages of using php

1. Open Source:

Now you know that PHP is open source means it is a free software to use. You just have to download the package, install it and use it according to your requirement. One another benefit with PHP is tools required to develop Website also an open source for e.g Apache Server, and most of Text editors notepad++, Netbeans etc. if you choose another language to develop website, it would be costly to you.

2. Platform independent:

PHP is platform independent or you can say cross platform it means php can run on any operating system. eg. Linux, Unix, Mac OS X, Windows.

So you can run your Web App on any operating system.

3. Large Community:

The Huge Advantage of PHP is it offers large community. If you are looking for PHP jobs, ready Made code, or solutions of your problems then there are many PHP devlopers, contributors, users and employers that may help you.

4. Support Multiple Database:

One of the reason behind popularity of PHP is it supports all major databases. it supports database like CUBRID, MongoDB, Mssql, MySql, PostgreSQL, SQLite, Sybase etc.

5. PHP Frameworks:

PHP Has lots of Good quality of frameworks available that may help developers to get Job. Below are the popular list of frameworks.

  • Larvel
  • Yii
  • CakePhp
  • Zend Framework
  • CodeIgniter

If You are a beginners in PHP Frameworks, i would like to recommend you begin with Codeigniter because it is easy to learn and set up.

  • Codeigniter is easy to install, understand, fast, lightweight and more capable PHP Framework.
  • Helpful user guide, which enables PHP Developers to understand use of whole Framework.

You can Download Codeigniter 


In PHP there are some popular CMS available like Magneto, Joomla, WordPress, Drupal. If we want to make shopping site we can develop using Magneto, Joomla. for blogging we used WordPress and informational site, web application Drupal is normally used. Now a days WordPress is widely used Content Management System. you can learn wordpress from here

  • PHP stands for PHP: Hypertext Preprocessor
  • PHP is a server-side scripting language, like ASP
  • PHP scripts are executed on the server
  • PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, Postgre SQL, Generic ODBC, etc.)
  • PHP is an open source software (OSS)
  • PHP is free to download and use
  • Created in 1994 by rasmus lerdorf
  • Syntax inherited from C, Java and Perl
  • Powerful, yet easy to learn

PHP Syntax:

A PHP Scripting block always starts with <?php and ends with ?>. A PHP scripting block can be placed anywhere in the document.

On servers with shorthand supported ebabled you can start a scripting block with <? and end with ?>.

However, for maximum compatibility, we recommend that you use the standard form (<?php) rather than the shorthand form.

A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code.

Here is the example of a simple PHP script which sends the text ” Hello World” to the browser.

Each code line in PHP must end with a semicolon. The semicolon is a separator and is used to distinguish one set of instructions from another.

There are two basic statements to output text with PHP. echo and print.

In the example above we have used the echo statement to output the text “Hello World”.

PHP Functions:

In this section, I am going to explain to you what is functions and types of functions in PHP.

what is Function?

Functions are blocks of codes to execute a specific task or
we can say A Function is a reusable piece of code. You will write it once and you can use it many times.

For example, if we compare programming functions with our real-life function like ring ceremony function. In-ring ceremony function we perform the only specific task that boy and girl will exchange ring same for birthday function we perform the specific function that birthday boy/girl cut the cake. same in our programming world functions perform the specific task.

let’s assume that we make a function to perform addition of two numbers so whenever you will call this function with two number it will add those number and return the result to you.

Below is the List of PHP function types:


User-defined function:

The user-defined function defined by the user to perform their specific operations. A function may be defined using syntax such as the following:

Function names follow the same rules as other labels in PHP. A valid function name starts with a letter or underscores, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: [a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*.

Functions need not be defined before they are referenced, except when a function is conditionally defined as shown in the two examples below.

When a function is defined in a conditional manner such as the two examples shown. Its definition must be processed prior to being called.

Function arguments:

Information may be passed to functions via the argument list, which is a comma-delimited list of expressions. The arguments are evaluated from left to right.

Example: passing a parameter to the function

PHP supports passing arguments by value (the default), passing by reference , and default argument values. Variable-length argument lists are also supported.

Returning value function:

Value is returned back by using the return statement. the function can return any type of value array, objects.This causes the function to end its execution immediately and pass control back to the line from which it was called.

A function can not return multiple values, but similar results can be obtained by returning an array.

Variable Function:

PHP supports the concept of variable functions. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callback, function tables, and so forth.

Example variable function

Internal Function:

PHP comes standard with many functions and constructs. There are also functions that require specific PHP extensions compiled in, otherwise fatal “undefined function” errors will appear.

For Example: to use mysqli_connect() PHP must be compiled with MySqli support.

Anonymous function:

Anonymous functions, also known as closures, allow the creation of functions which have no specified name. They are most useful as the value of callback parameters, but they have many other uses.

Closures can also be used as the values of variables; PHP automatically converts such expressions into instances of the Closure internal class.Assigning a closure to a variable uses the same syntax as any other assignment, including the trailing semicolon.

PHP Array:

PHP Array is a collection of data. in other words, we can say the array is a special variable that stores multiple values at a time.

For e.g, In your database, there is table name student if you fetch all student name from the database you need a variable to store all student name.

Instead of creating the separate variable for each student name we can store in the special variable and that special variable we can call array.

There are three types of the array in PHP:

  • Indexed Array: Array with the numeric id
  • Associative Array: Array with keys
  • Multidimensional Array: Array contains one or more array

An array can be created using keyword array(). it takes the number of comma-separated value or key=>value pairs as an argument.

Indexed Array or Numeric Array:

You can create the indexed array using two ways.

Note: Array always start with 0
To print all elements of the array you can use PHP function print_r();

Associative Array:

In the associative array, keys are associated with its value. so it means if you have to store any value in the associative array then you have to assign key to that value.

Multidimensional Array:

Multidimensional Array means array inside array.

PHP Loops:

PHP Loops provide a way to repeat commands and control how many times they are repeated.

Looping statements in PHP are used to execute the same block of code a specified number of times.

Very often when you write code, you want the same block of code to run a number of times. You can use looping statements in your code to perform this.

Why do we want loops in our code?

  • Do something for a given number of times or for every object in a collection of objects.
  • For every radio button in a form, see if it is checked.
  • For every month of the year, charge $100 against the balance.
  • Calculate the sum of all the numbers in a list.

In PHP we have the following looping statements:

  • While: loops through a block of code if and as long as a specified condition is true.
  • Do While: loops through a block of code once, and then repeats the loop as long as a special condition is true.
  • For loop: loops through a block of code a specified number of times.
  • Foreach: loops through a block of code for each element in an array.

While Loop:

The while loop will execute a block of code if and as long as a condition is true.


Example of while loop:

Do while Loop:

The do…while loop will execute a block of code at least once-it then will repeat the loop as long as a condition is true.


Example of do while loop:


For Loop:

The for loop is used when you know how many times you want to execute a statement or a list of statements.


The for statement has three parameters. The first parameter initializes variables, the second parameter holds the condition, and the third parameter contains the increments required to implement the loop.

If more than one variable is included in the initialization or the increment parameter, they should be separated by commas. The condition must evaluate to true or false.

Example of For loop:

Foreach Loop:

The for each loop is used to loop through arrays.

For every loop, the value of the current array element is assigned to $value (and the array pointer is moved by one) -so on the next loop, you’ll be looking at the next element.


Example of For each loop:

if else Conditions in PHP:

In this section we are going to show you how to put conditions in PHP.

If we have to check which number is big we can achive this using if else condtions.

Let’s look below how can we use if else conditions in PHP

If & else Statements:

The if, else if and else statements in PHP are used to perform different actions based on different conditions.

Conditional Statements:

  • Very often when you write code, you want to perform different actions for different decisions.
  • You can use conditional statements in your code to do this.
  • if…else statement uses this statement if you want to execute a set of code when a condition is true and another
    if the condition is not true.
  • elseif statement is used with the if…else statement to execute a set of code if one of several conditions are true

 The if else Statement Syntax:

Example If Else:

If and Elseif Statements:

If you want to execute some code if one of several conditions is true use the else if statement

The If Elseif Statement Syntax:

Example if else if:

Switch Statement in PHP


PHP Switch Statement:

  • The Switch statement in PHP is used to perform one of several different actions based on one of several different conditions.
  • If you want to select one of many blocks of code to be executed, use the Switch statement.
  • The switch statement is used to avoid long blocks of if..elseif..else code


How It Works:

  • A single expression (most often a variable) is evaluated once.
  • The value of the expressi on is compared with the values for each case in the structure.
  • If there is a match, the code associated with that case is executed.
  • After a code is executed, break is used to stop the code from running into the next case.
  • The default statement is used if none of the cases are true


Please enter your comment!
Please enter your name here