www.hope.ac.uk faculty of sciences and social sciences hope functions in php stewart blakeway fml...
Post on 19-Dec-2015
215 views
TRANSCRIPT
![Page 1: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/1.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Functions in PHP
Stewart BlakewayFML [email protected] 291 3113
![Page 2: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/2.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Session Aims– How to define a function– How to call a function– Passing and Receiving Values to/from
functions– Function Variables and Global
Variables– Giving a function storage– Functions within Functions– Checking a function exists before
calling it
![Page 3: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/3.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
What is a function?
• A function is a self-contained block of code that can be used time and time again.
• Values can be passed to functions.• Functions, once finished executing can pass
values back to the calling code.
![Page 4: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/4.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Types of function
• There are two types of function. You have seen one type already and may or may not of known it was a function.
– Built-in functions– Self defined functions
![Page 5: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/5.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Built-in Functions
echo (“Hello World!”);
• echo is the name of the function that you are calling.
• Hello World! is a string that we are passing to the function.
• Information that you want to pass to the function should be between the parentheses.
How many built in functions are there?
![Page 6: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/6.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Arguments!
• The terminology that explains the data that is to be passed to the function is called an argument. Some functions require more than one argument.
my_function ($an_argument, $another_argument);
![Page 7: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/7.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
date ()
• Date () is another example of a built in function. Lets examine how this works.
$today = date ("l j F" );echo $today;
• date () uses lots of symbols to represent different formats. ie: M,F,m,n,d,j,l,D,w,Y…
![Page 8: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/8.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
date ()
$today = date ("l j F" );echo $today;
• Functions that return a value do not necessarily have to be stored.
echo (date ("l j F" ));
![Page 9: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/9.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
date ()
$today = date ("l j F" );echo $today;
What data type is $today ?
How could we find out?
$today = date ("l j F" );echo $today;
echo (gettype($today));
![Page 10: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/10.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Function function function
echo (gettype(date("l j F" )));
What do you think is echo’d ?
![Page 11: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/11.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Defining a function
• You can define a function by using the function statement
function function_name ($argument1, $argument2 …)
{
// function code goes here
}
![Page 12: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/12.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Defining a function
• What is echoed out?
<?php
function bigfont($txt)
{
echo("<h1>" . $txt . "</h1>");
}
bigfont ("Hello World!");
?>
![Page 13: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/13.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
tt function
• Examine the following code, what does it echo out?
function tt($num)
{
for ( $x=1 ; $x<13 ; $x++ )
{
echo ($x . “*” . $num . “=” . $x*$num .“<br />”);
}
}
tt (6);
![Page 14: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/14.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
More about variables!<html>
<head><title>Local Variables</title></head>
<body>
<?php
$life=42;
function meaningOfLife ()
{
echo (“The meaning of life is”. $life);
}
meaningOfLife();
?>
</body>
</html>
![Page 15: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/15.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
More about variables!<html>
<head><title>Global Variables</title></head>
<body>
<?php
$life=42;
function meaningOfLife (){global $life;echo (“The meaning of life is”. $life);}
meaningOfLife();
?>
</body>
</html>
![Page 16: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/16.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Alternatively<html>
<head><title>Local Variables</title></head>
<body>
<?php
$life=42;
function meaningOfLife ($arg){echo ("The meaning of life is". $arg);}
meaningOfLife($life);
?>
</body>
</html>
![Page 17: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/17.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Question
• Why not always pass variables to functions and do away with global variables?
Sometimes you will want to remember a value of a variable even if a function has finished or manipulate a variable outside of a function
![Page 18: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/18.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Numbered Headings<?php$numOfHeading=1;function numHeading ($heading)
{global $numOfHeading;echo ("<h2>" . $numOfHeading . " . " . $heading . "</h2>"
);$numOfHeading++;}
numHeading ("About Me!");echo ("<p>Stewart Blakeway<br />0151 291 3113</p>");numHeading ("Currently Teaching On");echo ("<ul> <li>Computing Concepts</li><li>Database Technology</li><li>IT Business Applications</li><li>Web Development</li><li>Network & Operating Systems</li></ul>");?>
![Page 19: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/19.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Question
• Although the last example got the point across, Global variables should be avoided and only used as the last resort!
• Why do you think this is?
The beauty of functions is their versatility, take the echo function as an example. This is a function of code that can be used again and again. Global variables hinder the flexibility that functions offer!
![Page 20: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/20.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Numbered Headings Alternative<?php
function numHeading ($heading){static $numOfHeading = 1;echo ("<h2>" . $numOfHeading . " . " . $heading . "</h2>"
);$numOfHeading++;}
numHeading ("About Me!");echo ("<p>Stewart Blakeway<br />0151 291 3113</p>");numHeading ("Currently Teaching On");echo ("<ul> <li>Computing Concepts</li><li>Database Technology</li><li>IT Business Applications</li><li>Web Development</li><li>Network & Operating Systems</li></ul>");?>
![Page 21: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/21.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
More than One Argument
• So far we have seen functions that pass one argument
• PHP can pass as many arguments as you define
• The results of an argument can be returned
![Page 22: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/22.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Returning a result<?phpfunction mySum($num1, $num2) { $total = $num1 + $num2; return $total; }
$myNumber = 0;$myNumber = mySum(3, 4);echo $myNumber;?>
We saw this in slides 7 and 8
![Page 23: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/23.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Passing Two Arguments<?php
function nSFont ($txt, $size) { echo ("<font size=\"$size\" face=\"Helvetica,
Arial, Sans-Serif\“>$txt</font><br />"); }
nSFont ("A heading",12);nSFont ("some body text",3);nSFont ("some more body text",3);
?>
![Page 24: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/24.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
<?php
function nSFont ($txt, $size) { echo ("<font size=\"$size\" face=\"Helvetica,
Arial, Sans-Serif\“>$txt</font><br />"); }
nSFont ("A heading“,12);nSFont ("some body text");nSFont ("some more body text");
?>
What do you suppose would happen if the function was set to accept two arguments and only one was passed?
A headingWarning: Missing argument 2 for nsfont() in C:\Program Files\Apache Group\Apache2\htdocs\test.php on line 6some body text
Warning: Missing argument 2 for nsfont() in C:\Program Files\Apache Group\Apache2\htdocs\test.php on line 6some more body text
![Page 25: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/25.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Default Arguments<html><head><title>Local Variables</title></head><body>
<?php
function nSFont ($txt, $size = 3) { echo ("<font size=\"$size\" face=\"Helvetica, Arial, Sans-
Serif\“> $txt</font><br />"); }
nSFont ("A heading",12);nSFont ("some body text");nSFont ("some more body text");
?></body></html>
![Page 26: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/26.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
What will be echoed out?<html><head><title>Local Variables</title></head><body>
<?php
function squared($num) { $num = ($num * $num); echo ($num."<br />"); }
$num = 30;squared ($num);echo ($num."<br />");
?>
</body></html>
![Page 27: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/27.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
What will be echoed out?<html><head><title>Local Variables</title></head><body><?php
function squared (&$num) { $num = ($num * $num); echo ($num."<br />"); }
$num = 30;squared ($num);echo ($num."<br />");
?></body></html>
![Page 28: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/28.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
What will be echoed out?<html><head><title>Local
Variables</title></head><body><?php
function squared (&$joe) { $joe = ($joe * $joe); echo ($joe."<br>"); }
$num = 30;squared ($num);echo ($num."<br>");
?></body></html>
![Page 29: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/29.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Checking a function exists<?phpfunction mySum($num1, $num2)
{ $total = $num1 + $num2; return $total;
}if (function_exists (mysum))
{$myNumber = mySum(3, 4);echo $myNumber;}
else{echo ("Function not available");}
?>
![Page 30: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/30.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
What next?• Seminar– More exercises, specifically defining functions and
calling functions
• Next Lecture– This is on arrays, associate arrays,
multidimensional arrays, classes, methods.. And more… don’t miss this as its not easy!
![Page 31: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/31.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
tt function – 3 Errors
function tt($num)
for ( $x=1 ; $x<13 ; $x++ )
{
echo (x . “*” . $num . “=” . $x*$num . “<br />”);
}
tt 6;
![Page 32: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/32.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
3 Errors<html><head><title>Global Variables</title></head><body>
<?php
$life=42;
function meaningOfLife ()
{
Global $life;
echo (“The meaning of life is” $life);
}
meaningOfLife();
>
</body></html>
![Page 33: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/33.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Numbered Headings – 3 Errors<?php
function numHeading($heading) { fixed $numOfHeading = 1; echo ("<h2>" . $numOfHeading . " . " . $heading . "</h2>" ); $numOfHeading++; }numHeading ("About Me!");echo ("<p>Stewart Blakeway<br />0151 291 3113</p>");numheading ("Currently Teaching On");echo ("<ul><li>Computing Concepts</li><li>Database Technology</li><li>IT Business Applications</li><li>Web Development</li><li>Network & Operating Systems</li>");?>
![Page 34: Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Functions in PHP Stewart Blakeway FML 213 blakews@hope.ac.uk 0151 291 3113](https://reader035.vdocuments.net/reader035/viewer/2022062714/56649d2e5503460f94a051b8/html5/thumbnails/34.jpg)
www.hope.ac.uk Faculty of Sciences and Social Sciences
HO
PE
Any Questions?