Tuesday, November 9, 2010

Summary of PHP commands (1)



Download PHP
Download PHP for free here:http://www.php.net/downloads.php

Download MySQL for free here:http://www.mysql.com/downloads/

Download Apache for free here:http://httpd.apache.org/download.cgi

1. A PHP scripting block always starts with <?php or <? and end with ?> .All variables in PHP start with a $ sign symbol. A variable does not need to be declared before adding a value to it. PHP sentences end with semicolon;
PHP variable names are case sensitive, but the names of functions are case insensitive. MySQL is not case sensitive by default.
we use // to make a single-line comment or /* and */ to make a large comment block.

PHP array index starts at 0. Example of numeric array:
$cars=array("Saab","Volvo","BMW","Toyota");
$cars[0]="Saab";
Example of associative arrays:
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
$ages['Peter'] = "32";

2. if else statement:
if (condition)
{code to be executed if condition is true;}
else
{code to be executed if condition is false;}

switch statement:
switch (n)
{
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
default:
code to be executed if n is different from both label1 and label2;
}
for loop

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

The foreach loop is used to loop through arrays.

foreach ($array as $value)
{
code to be executed;
}

example:
Code:
<?  $x=array("one","two","three");   foreach ($x as $value)       {   echo $value . " ";  }     ?>
PHP Functions
function functionName()
{
code to be executed;
}

The $_GET Function

The built-in $_GET function is used to collect values from a form sent with
method="get".

The $_POST Function

The built-in $_POST function is used to collect values from a form sent with
method="post".

Example:
Code:
<html> <body>  <form action="welcome.php" method="post">  Name: <input type="text" name="fname" />  Age: <input type="text" name="age" />  <input type="submit" />  </form>  </body>  </html>


welcome.php:

Code:
 <html>  <body>  Welcome <?php echo $_POST["fname"]; ?>!<br />  You are <?php echo $_POST["age"]; ?> years old. </body> </html>

No comments:

Post a Comment