Tuesday 7 August 2012

PHP Basic

Basic PHP Syntax

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

A PHP file must have a .php extension.

<html>
<body>

<?php

echo "Hello World";

//This is a comment

/*
This is
a comment
block
*/
?>

</body>
</html> 

 

PHP Variables

<?php
$txt="Hello World!";

$x=16;

echo $x;

echo $txt;
?>

 

PHP Variables

PHP has four different variable scopes:
  • local
  • global
  • static
  • parameter
<?php
$a = 5;
$b = 10;

function myTest()
{
global $a, $b;                                                             output=15
$b = $a + $b;
} 

myTest();
echo $b;
?>

 

PHP If...Else Statement

<html>
<body>

<?php
$d=date("D");
if ($d=="Fri")
  {
  echo "Have a nice weekend!";
  }
elseif ($d=="Sun")
  {
  echo "Have a nice Sunday!";
  }
else
  {
  echo "Have a nice day!";
  }
?>

</body>
</html>

 

PHP Switch Statement

<html>
<body>

<?php
$x=1;
switch ($x)
{
case 1:
  echo "Number 1";
  break;
case 2:
  echo "Number 2";
  break;
case 3:
  echo "Number 3";
  break;
default:
  echo "No number between 1 and 3";
}
?>

</body>
</html> 

 

PHP Arrays

<?php
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota"; 
echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";
?>

 

PHP While Loop

<html>
<body>

<?php
$i=1;
while($i<=5)
  {

  echo "The number is " . $i . "<br />";                    output:The number is 1,

                                                                       2,3,4,5

 $i++;

  }
?>

</body>
</html>

 

PHP do..While Loop

<html>
<body>
                                                  output:The number is 2

                                                                      3,4,5,6.
<?php
$i=1;
do
  {
  $i++;
  echo "The number is " . $i . "<br />";
  }
while ($i<=5);
?>

</body>
</html>

 

PHP For Loop

<html>
<body>

<?php
for ($i=1; $i<=5; $i++)
  {
  echo "The number is " . $i . "<br />";
  }
?>

</body>
</html>

 

PHP For each Loop

<html>
<body>

<?php
$x=array("one","two","three");
foreach ($x as $value)
  {
  echo $value . "<br />";
  }
?>

</body>
</html> 

Output:one
two
three


PHP Functions

1)<html>
<body>

<?php
function writeName($fname)
{
echo $fname . " Refsnes.<br />";
}

echo "My name is ";
writeName("Kai Jim");
echo "My sister's name is ";
writeName("Hege");
echo "My brother's name is ";
writeName("Stale");
?>

</body>
</html>

output:

My name is Kai Jim Refsnes.
My sister's name is Hege Refsnes.
My brother's name is Stale Refsnes.

 

2)<html>
<body>

<?php
function add($x,$y)
{
$total=$x+$y;
return $total;
}

echo "1 + 16 = " . add(1,16);
?>

</body>
</html>

output:

1 + 16 = 17


PHP Form Handling

index.php

<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
<html>
<body>

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

</body>
</html>

Output:
Welcome Asanhussain!
You are 22 years old.

 
PHP GET Function

The predefined $_GET variable is used to collect values in a form with method="get".

Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and has limits on the amount of information to send.

index.php

<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>

When the user clicks the "Submit" button, the URL sent to the server could look something like this:
 
http://localhost/hussain/welcome.php?fname=Peter&age=37

The "welcome.php" file can now use the $_GET variable to collect form data (the names of the form fields will automatically be the keys in the $_GET array):

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

Note: 1)This method should not be used when sending passwords or other sensitive information!
         2)The get method is not suitable for very large variable values. 
            It should not be used with values  exceeding 2000 characters.

PHP POST Function

The predefined $_POST variable is used to collect values from a form sent with method="post".

Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.

However, because the variables are not displayed in the URL, it is not possible to bookmark the page.

PHP REQUEST Function

The predefined $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE.

The $_REQUEST variable can be used to collect form data sent with both the GET and POST methods.

Welcome <?php echo $_REQUEST["fname"]; ?>!<br />
You are <?php echo $_REQUEST["age"]; ?> years old.



No comments:

Post a Comment