0% found this document useful (0 votes)
47 views17 pages

Web Designs & Languages

This document discusses PHP functions. It explains that PHP has over 700 built-in functions and that functions allow code to be reused by grouping blocks of code that perform a specific task. It provides examples of creating basic functions that write text, functions with parameters to customize output, and functions that return values. Built-in PHP functions like abs(), sqrt(), round(), and date() are demonstrated. The use of the global keyword with functions is also covered.

Uploaded by

Anas A'sarori
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views17 pages

Web Designs & Languages

This document discusses PHP functions. It explains that PHP has over 700 built-in functions and that functions allow code to be reused by grouping blocks of code that perform a specific task. It provides examples of creating basic functions that write text, functions with parameters to customize output, and functions that return values. Built-in PHP functions like abs(), sqrt(), round(), and date() are demonstrated. The use of the global keyword with functions is also covered.

Uploaded by

Anas A'sarori
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

Web Designs & Languages

PHP

Fouad Al Yarimi

PHP Functions

The real power of PHP comes


from its functions.
In PHP - there are more than
700 built-in functions available.

Create a PHP Function

A function is a block of code that can be


executed whenever we need it.
Creating PHP functions:
All functions start with the word "function()"
Name the function - It should be possible to
understand what the function does by its name.
The name can start with a letter or underscore
(not a number)
Add a "{" - The function code starts after the
opening curly brace
Insert the function code
Add a "}" - The function is finished by a closing
curly brace

Example

A simple function that writes my name when it is called:


<html>
<body>
<?php
function writeMyName()
{
echo "Kai Jim Refsnes";
}
writeMyName();
?>
</body>
</html>

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

Use a PHP Function

Now we will use the function in a PHP script:


<html>
<body>
<?php
function writeMyName()
{
echo "Kai Jim Refsnes";
}
echo "Hello world!<br />";
echo "My name is ";
writeMyName();
echo ".<br />That's right, ";
writeMyName();
echo " is my name.";
?>
</body>
</html>

PHP Functions - Adding


parameters

Our first function (writeMyName()) is


a very simple function. It only writes
a static string.
To add more functionality to a
function, we can add parameters. A
parameter is just like a variable.
You may have noticed the
parentheses after the function name,
like: writeMyName(). The parameters
are specified inside the parentheses.

Example 1

The following example will write different first names, but


the same last name:
<html>
<body>
<?php
function writeMyName($fname)
{
echo $fname . " Refsnes.<br />";
}
echo "My name is ";
writeMyName("Kai Jim");
echo "My name is ";
writeMyName("Hege");
echo "My name is ";
writeMyName("Stale");
?>
</body>
</html>

Example2

The following function has two parameters:

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

PHP Functions - Return


values

Functions can also be used to return values.

Example
<html>
<body>
<?php
function add($x,$y)
{
$total = $x + $y;
return $total;
}
echo "1 + 16 = " . add(1,16)
?>
</body>
</html>

More Function Writing

Functions can contain any of


the information we have
learned so far.

More Function Writing

<?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>";
?>

More Function Writing

One new thing you may have


noticed is "GLOBAL". Since the
variable $start is not defined
within the function, we use the
tag "GLOBAL" to let it know
that it needs to use the $start
variable that we have defined
outside of the function.

Functions already in
PHP

A function is something that


performs a specific task.
People write functions if they
plan on doing the same task
over and over again.
This allows you to only write
the code once and save a lot of
time and space.

Functions already in
PHP

All functions in PHP are


phrased as: name(argument).
The name being the name of
the function, and the argument
being the value(s) it is using.

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>";
?>

You might also like