BASIC PHP EXAMPLES

PHP is a powerful tool for making dynamic and interactive Web pages.

PHP is the widely-used, free, and efficient alternative to competitors such as Microsoft's ASP.

PHP codes need to be inside these
< ?

Your codes goes here

? >

Example 1. Variables
$text="Hello World!";
$x=16;




Example 2. Displaying string
$txt="Hello World";
echo $txt;




Example 3. If else

$day="Friday";

if($day=="Monday"){
echo "God I hate Mondays";
}
elseif($day=="Tuesday"){
echo "I wish it's Friday tomorrow";
}
elseif($day=="Wednesday"){
echo "Man, Wednesday is a busy day";
}
elseif($day=="Thursday"){
echo "Man, what a day, good thing it's Friday tomorrow";
}
elseif($day=="Friday"){
echo "Thank God it's friday";
}
else{
echo "I love weekends";
}




Example 4. Switch

$day="Friday";
switch ($day)
{
case "Monday":
echo "God I hate Mondays";
break;
case "Tuesday":
echo "I wish it's Friday tomorrow";
break;
case "Wednesday":
echo "Man, Wednesday is a busy day";
break;
case "Thursday":
echo "Man, what a day, good thing it's Friday tomorrow";
break;
case "Friday":
echo "Thank God it's friday";
break;
default:
echo "I love weekends";
}




Example 5. Arrays
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
print_r($a);
?>

The output of the code above will be:
Array ( [a] => Dog [b] => Cat [c] => Horse )




Example 6. While loop

$i=1;
while($i<=5)
{
echo "The number is " . $i . "
";
$i++;
}




Example 7. For loop

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




Example 8. Functions

function writeName($fname)
{
echo $fname . "
";
}

echo "My name is ";
writeName("John Doe");
echo "My sister's name is ";
writeName("Helen Doe");
echo "My brother's name is ";
writeName("Steve Doe");




Example 9. Forms

Name this file greeting_form.php

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

Name this file welcome.php

Welcome < ? echo $_POST["fname"]; ?>!

You are < ? echo $_POST["age"]; ?> years old.


Reply

Contributor: Robert Dabu
2009-10-01 10:52:01

FEEDBACK


Robert Dabu
Hello users. I will add more scripts soon.
rene pal
thank you. I will await your contents

Home | About Us | Links | Mission | Forum | Archive | Login | FAQ | Terms | Privacy | Feedback | Chat | Contact Us
Copyright © 2009 by Computer Systems Consultancy Inc. All rights reserved. Revised: 23 July 2009.

This website have been visited 4356 times since 9/28/2009





 
Forum Register FAQ Help