Introduction to PHP

PHP stands for  Hypertext Preprocessor.It is a server-side scripting language.It is an open source software and free to download and use.

PHP files have a file extension of ".php", ".php3", or ".phtml".

 

PHP script:

PHP scripting block always starts with

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.

variables :

All variables will start with $ symbol.

Simple example

$var_name = value;

 

condition statement:

All the condition statement syntax are same as in C language.Example if..else,switch.

Array:

Array is a homogenious collection of elements.

$arr[0]="12345";

echo $arr[0];

 

There are 3 types of arrays

1)Numerical arrays

2)Associative array

3)Multi dimensional arrays

 

Numerical arrays:

A numeric array stores each array element with a numeric index.

$type=array("cat","dog","rat");

Internally it will store as

$type[0]="cat";
$type[1]="dog";
$type[2]="rat";

 

Example

$type[0]="cat";
$type[1]="dog";
$type[2]="rat";
echo $type[0] . " kills " . $type[2];
?>

 

Associative array:

In associative array the index is replaced with a value.

$names = array("ravi"=>40, "rani"=>45, "ram"=>50);

$names['ravi']="40";

$names['rani']="45";

$names['ram']="50";

example:

$names['ravi']="40";

$names['rani']="45";

$names['ram']="50";


echo "marks of ravi,rani,ram are". $names['ravi'] .$names['rani']. $names['ram'];
?>


Loops:

All the loop statement syntax also similar as in C language.

example:

WHILE

while (condition)
{
code to be executed;
}

 

example:

$temp=0;

while($temp<=10)

{

echo "The number is" . $temp ;

$temp++;

}

?>

 

DO-WHILE

do
{
code to be executed;
}
while (condition);

example:

$temp=0;

do

{

echo "The number is" . $temp ;

$temp++;

}while($temp<=10)

?>

FOR

for (init; condition; increment)
{
code to be executed;
}

example:

for($temp=0;$temp<=10;$temp++;)

{

echo "The number is" . $temp ;

}

?>

 

 

 

 


 

 

 

 


Like it on Facebook, +1 on Google, Tweet it or share this article on other bookmarking websites.

Comments (0)

There are no comments posted here yet