Web Designs & Languages
Web Designs & Languages
PHP
Fouad Al Yarimi
PHP Functions
Example
Example
<?php
function examplefunction ()
{
print "Hi, I'm a Function <br>";
}
function sqr( $num )
{
$NumSqr = $num * $num;
return $NumSqr;
}
Print "Sample Line 1 <br>";
examplefunction();
Print "Sample Line 3 <br>";
$a = 9;
$b = sqr( $a );
Print $a . "^2 = " . $b;
?>
Example 1
Example2
<html>
<body>
<?php
function writeMyName($fname,$punctuation)
{
echo $fname . " Refsnes" . $punctuation . "<br
/>";
}
echo "My name is ";
writeMyName("Kai Jim",".");
echo "My name is ";
writeMyName("Hege","!");
echo "My name is ";
writeMyName("Stle","...");
?>
</body>
</html>
Example
<html>
<body>
<?php
function add($x,$y)
{
$total = $x + $y;
return $total;
}
echo "1 + 16 = " . add(1,16)
?>
</body>
</html>
<?php
function mul()
{
global $start;
print "<tr>";
for ($num=1; $num <= 10; $num++ )
{
$cell = $num * $start;
print "<td> " . $cell . " </td>";
}
print "</tr>";
}
$start = 0;
print "<table border=1 cellpadding=3>";
while ( $start <=10 )
{
mul();
$start++;
}
print "</table>";
?>
Functions already in
PHP
Functions already in
PHP
some examples of
functions already in
<?php
PHP
$a = abs(-.43);
$b = sqrt(16);
$c = round(12.3);
print "The absolute value of -.43 is " .
$a . "<br>";
print "The square root of 16 is " . $b .
"<br>";
print "12.3 rounded is " . $c . " and
12.5 rounded is " . round(12.5);
?>
some examples of
functions already in
PHP
<?php
$b = time ();
print date("m/d/y",$b) . "<br>";
print date("D, F jS",$b) . "<br>";
print date("l, F jS Y",$b) . "<br>";
print date("g:i A",$b) . "<br>";
print date("r",$b) . "<br>";
print date("g:i:s A D, F jS Y",$b) .
"<br>";
?>