PHP Tutorial – Introduction

What is php

  • PHP acronym for PHP: Hypertext Preprocessor.
  • PHP is a server scripting language means code does not need to be compiled before it gets used.
  • PHP is a widely-used open source general-purpose language.

To learn PHP you should know basics of HTML(Hypertext Language).
PHP is extremely easy for a newcomer and offers very advanced features to a professional programmer.

Where PHP Use:

PHP can be used on the various operating system(Microsoft Windows, Unix variants, Linux, etc.).
PHP also supports almost all servers(Apache, IIS and many others).
Also, PHP supports the wide range of databases(DBA, CUBRID, DB++, Mongo, MYSQL, etc.).
This is the strongest feature in PHP.
Therefore, you can choose any operating system and any server with PHP.
PHP script is used in following 3 main areas:

  1. Server-side scripting
  2. Commond-line scripting
  3. Desktop application

PHP Overview

  • The PHP code is enclosed in start() processing instructions.
  • Each separate instruction must end with a semicolon (;).
  • echo is same as printf() in C language which uses for printing outputs.
  • Whatever you want to print on the screen of a web browser insert it in quotation marks(” “).
  • In PHP we don’t have to give data type of variable, PHP automatically converts it.
  • In other languages like C, C++, Java, etc. it is necessary to give data type of variable.
Help others by sharing the content!

Multidimensional array

In associative array, we can store one value with only one key.
But if we want to store one or more keys for one value.
This can be done using multi-dimensional arrays.
A multi-dimensional array can be two-dimensional or three-dimensional array depending on indices.

A two-dimensional array is known as array of arrays.
A three-dimensional array is known as array of arrays of arrays.

 

Using multi-dimensional array you can store data in table form.
Let’s see following example:

Subject Marks Grade
Mathematics 23 D
Science 45 C
English 65 B
Drawing 80 A

Now this above table can be stored using multi-dimensional array as.

Output:

You can print this table using for loop.

Output:

Help others by sharing the content!

Associative array

Associative array is an second type of array.
You can define associative array in following two ways:

OR

 

Example

Output:

 

Output:

Help others by sharing the content!

Indexed array

PHP indexed array is a simple type of array.
An indexed array use number as access keys.
You can define indexed array in following two ways:

OR

Example

Output:

Output:

Here, $country is a name of variable.
0,1,2 are the index numbers of the elements.
India, Australia, Switzerland are the values assigned to array elements.

Help others by sharing the content!

foreach loop

PHP foreach loop works only on arrays and objects.
Array pointer is always incremented by one.

There are two syntaxes for foreach loop:

Syntax of PHP foreach loop

OR

 

Example using value

Output:

 

Example using key

Output:

 

Example

Output:

Help others by sharing the content!

for loop

PHP for loop is the most complex loops in PHP. They behave like their C counterparts.

Syntax of PHP for loop:

Following is given a general syntax of for loop in PHP:

In the beginning, loop condition is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed.
If it evaluates to FALSE, the execution of the loop ends.

All three expression, initialization, condition, increment/decrement can be empty or multiple.
If the condition is empty then loop will execute infinite considering it is true.
If the expressions are multiple then they are separated by commas.
 

Flowchart of PHP for loop

php-for-loop
 

Example
Consider the following examples.

Output:

Help others by sharing the content!

do-while

PHP do-while loop are very similar to while loop.
Only one difference in do-while, loop condition is checked at end of each iteration.
Whereas, in while loop condition is checked at beginning of loop.
Also, in do-while, loop is execute at onces without checking condition.
Output of do-while loop and while loop is same.

 

Syntax of PHP do-while

Following is given a general syntax of do-while loop in PHP:

 

Flowchart of PHP do-while loop

php do-while loop
 

Example

Output:

Help others by sharing the content!

while loop

PHP while loop execute a multiple statements repeatedly until while expression is true.

 

Syntax of while loop:

Following is given a general syntax of while loop in PHP:

The multiple statements inside while loop get executed only when expression is true.
These multiple statements enclosed within the curly braces or using the alternate syntax:
Initially if while condition is true then only while statements will execute otherwise they will not execute at even once.

 

Flowchart of PHP while loop

PHP-while-loop
 

Example

Output:

Or you can use while loop as following:
It will print same output as above example only we use endwhile instead curly braces { }.

Help others by sharing the content!

switch

switch is similar with multiple if structure.
Sometimes you want to compare same expression with different multiple values, and execute a different statements depending on value. This can be done using switch statement.

How switch work

A switch structure execute line by line. If the case value matches the value of switch expression then only the statements will execute.
In a switch statement, the condition is evaluated only once and the result is compared to each case statement.

Syntax of switch

 

Flowchart of switch

flowchart-of-switch
 

Example of switch –

Output

PHP continues to execute the statements until the end of the switch block, or it sees a break statement.
If there is no break keyword at end of case’s statements, PHP will execute the statements of the following case.
For example:

Output

 

Switch case can be a string. For example

 

case statement can be empty, which simply passes control to next case statement.
For example:

 

Default case is a special case. If any case value wasn’t matched with switch expression result then default case will execute.

Help others by sharing the content!

else if/ elseif

This keyword uses both keywords if and else.
In this structure, in case the first if expression evaluates to FALSE.
It will execute alternative elseif expression.
else if is same as elseif.

Syntax of elseif

 

Flowchart of else if

flowchart-of-else-if
 

There may be several elseifs within the same if statement. The first elseif expression (if any) that evaluates to TRUE would be executed. In PHP, you can also write ‘else if’ (in two words) and the behavior would be identical to the one of ‘elseif’ (in a single word). The syntactic meaning is slightly different (if you’re familiar with C, this is the same behavior) but the bottom line is that both would result in exactly the same behavior.

The elseif statement is only executed if the preceding if expression and any preceding elseif expressions evaluated to FALSE, and the current elseif expression evaluated to TRUE.

Example of elseif-

There may be several elseif within the same if statement

Help others by sharing the content!