SecurityArena

Guide to Practical Info Security!

Who's Online

We have 1 guest online
Print E-mail
Written by Administrator   
Sunday, 16 August 2009 13:55

PHP Short Tutorial

PHP is a server-side scripting language, like ASP and JSP. PHP is an open source software, free to download and use.

A PHP scripting block always starts with <?php and ends with ?>.
All variables in PHP start with a $ sign symbol.
Each code line in PHP must end with a semicolon.
PHP is a Loosely Typed Language, meaning a variable does not need to be declared before adding a value to it. PHP automatically converts the variable to the correct data type (integer, String etc.), depending on its value.

<html>
<body>
//This is one line comment
<?php
$txt="Hello World"; // an variable declaration
echo $txt;
$var_name = 23; // an variable declaration
?>
/* This is a multi line comment
block. */
</body>
</html>
The concatenation operator (.)  is used to put two string values together.
<?php
$txt1="Hello World!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?>
For a complete String Operators, please refer.
Syntax for other usual programming constructs like functions, If/Else, while, for and switch is similar to other mainstream scripting languages.

The foreach Loop

The foreach loop is used to loop through arrays.
<?php
$x=array("one","two","three");
foreach ($x as $value)
  {   echo $value . "<br />";  }
?>

Arrays in PHP

Numeric Arrays

A numeric array stores each array element with a numeric index.
One Syntax:
$cars=array("Saab","Volvo","BMW","Toyota");
2nd alternative:
<?php
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";
?>

Associative Arrays

An associative array, each ID key is associated with a value.
One Syntax:
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
2nd alternative:
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";

Multidimensional Arrays

In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.

HTML Forms Handling

The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. Any form element in an HTML page will automatically be available to your PHP scripts, when a user submits the form.
An example form:
<html>
<body>
<form action="example.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" /></form>
</body>
</html>
Target php file “example.php”:
<html>
<body>
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>

In case if ‘GET’ method is used in html form then, corresponding php variable would be $_GET.    
The Scope Resolution Operator (::) is a token that allows access to static, constant, and overridden members or methods of a class.

<html>
<body>
//This is one line comment
<?php
$txt="Hello World"; // an variable declaration
echo $txt;
$var_name = 23; // an variable declaration
?>
/* This is a multi line comment
block. */
</body>
</html>
The concatenation operator (.)  is used to put two string values together.
<?php
$txt1="Hello World!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?>
For a complete String Operators reference (http://w3schools.com/php/php_ref_string.asp).
Syntax for other usual programming constructs like functions, If/Else, while, for and switch is similar to other mainstream scripting languages.

The foreach Loop

The foreach loop is used to loop through arrays.
<?php
$x=array("one","two","three");
foreach ($x as $value)
  {   echo $value . "<br />";  }
?>

Arrays in PHP

Numeric Arrays

A numeric array stores each array element with a numeric index.
One Syntax:
$cars=array("Saab","Volvo","BMW","Toyota");
2nd alternative:
<?php
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";
?>

Associative Arrays

An associative array, each ID key is associated with a value.
One Syntax:
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
2nd alternative:
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";

Multidimensional Arrays

In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.

HTML Forms Handling

The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. Any form element in an HTML page will automatically be available to your PHP scripts, when a user submits the form.
An example form:
<html>
<body>
<form action="example.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" /></form>
</body>
</html>
Target php file “example.php”:
<html>
<body>
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>

In case if ‘GET’ method is used in html form then, corresponding php variable would be $_GET.

The Scope Resolution Operator (::) is a token that allows access to static, constant, and overridden members or methods of a class.

This is an attempt to provide one look quick tutorial, for detail tutorials/resources googleIT.

 

Last Updated on Friday, 28 August 2009 05:09
 
Please register or login to add your comments to this article.
 
Joomla 1.5 Templates by Joomlashack