0% found this document useful (0 votes)
3 views20 pages

Lec # 21 PHP - IV

Uploaded by

mnsuol500
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views20 pages

Lec # 21 PHP - IV

Uploaded by

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

Web Engineering

Lecture 21
User-Defined Functions
(PHP - IV)

1
FUNCTIONS
• In any programming language, it is ability to define
and use functions. The code you want to reuse.

• Format of functions:

function name($arguments) {
statements;
}
• We are talking about User-Defined functions
are functions we define ourselves.

• There are function which is not defined by the


user in fact PHP has made them already.

• We have seen some of those:


• E.g.
str_replace(“quick”, “super-fast”, $thirdString);
EXAMPLE
• str_replace(“quick”, “super-fast”, $thirdString);

• Or if we want to define ourselves the same


function then it would be like this:

• function str_replace($search, $replace, $subject)


{
statement;
statement;
statement;
}
NAME OF FUNCTION
• The name of the function can be just about
what we want, it is similar to the name of
strings.

• It can start from letter, numbers, dashes


and underscore.
EXAMPLE
• <?php
function say_hello() {
echo “Hello World! <br />”;
}
say_hello(); // calling the function
?>
EXAMPLE
• <?php
function say_hello2($word) {
echo “Hello {$word}! <br />”;
}
say_hello2(“Every one”);
?>

• The reason we use argument, we want to


give our function a flexibility. Let say we
again call function say_hello2(“world”);
NOTE
• Function must be defined. If you call a
function which is not defined, it gives an
error.

• Function can not defined more than once.


EXAMPLE
• Passing variable as argument. A variable
just reference to a string.
• <?php
function say_hello2($word) {
echo “Hello {$word}! <br/> ”;
}
?>
$name = “John Doe”;
say_hello2($name);
EXAMPLE
• You can make a function more flexible by using
multiple argument.

• <?php
function say_hello($greeting, $target, $punct);
echo $greeting . “, ” . $target . $punct . “<br/>”;
$name = “John Doe”;
say_hello(“Greetings”, $name, “!”)
?>
EXAMPLE
• <?php
function addition($val1, $val2) {
$sum = $val1 + $val2;
}
echo addition(3, 4);
?>
Return Values
• We call the function, return the $sum value
in new variable.
• <?php
function addition($val1, $val2) {
$sum = $val1 + $val2;
return $sum;
}
$new_val = addition(3, 4);
echo $new_val;
?>
EXAMPLE
• <?php
function addition($val1, $val2) {
$sum = $val1 + $val2;
return $sum;
}
$new_val = addition(3, 4);
echo $new_val;
if (addition(5, 6) == 11) {
echo “Yes”;
}
?>
NOTE
• Make sure you are doing something with
return values.

• Return value return only one value.


EXAMPLE
• Returning more than one value.
• <?php
function add_subt($val1, $val2) {
$add = $val1 + $val2;
$subt = $val1 - $val2;
$result = array($add, $subt);
return $result;
}
$result_array = add_subt(10, 5);
echo “Add ” . $result_array[0] . “<br/>”;
echo “Subt ” . $result_array[1] . “<br/>”;
?>
GLOBAL
• <?php
$bar = “outside”;
function foo() {
$bar = “inside”;
}
foo();
echo $bar . “<br />”;
?>
• Output: outside
EXAMPLE
• Using local variables, arguments and return values
• <?php
$bar = “outside”;
function foo2($var) { // outside
$var = “inside”;
return $var;
}
$bar = foo2($bar); // outside
echo $bar . “<br />”;
?>
• Output: inside
USING GLOBAL
• <?php
$bar = “outside”; // global variable
function foo() {
global $bar;
$bar = “inside”; // local
variable
Go outside the function,
} grasp the global variable
bar, and pulled that in.
foo(); so I can use it inside the
function.
echo $bar . “<br/>”;
?>
DEFAULT VALUE
• <?php
function paint($color = “red”) {
echo “The color of the room is
{$color}.”;
}
paint();
?>
EXAMPLE
• <?php
function paint($room=“office”, $color = “red”) {
echo “The color of the {$room} is {$color}.”;
}
paint(“bedroom”, “blue”);
?>

• Output: The color of the bedroom is blue.

You might also like