0% found this document useful (0 votes)
22 views

PHP Lecture Notes Day 9

PHP functions allow code reusability. Functions can take input arguments and return values. User-defined functions in PHP are defined using the function keyword and syntax. Functions can pass arguments by value or reference. Default argument values and variable length argument lists are also supported. Functions can return values. Parameterized functions accept parameters which act as variables inside the function.

Uploaded by

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

PHP Lecture Notes Day 9

PHP functions allow code reusability. Functions can take input arguments and return values. User-defined functions in PHP are defined using the function keyword and syntax. Functions can pass arguments by value or reference. Default argument values and variable length argument lists are also supported. Functions can return values. Parameterized functions accept parameters which act as variables inside the function.

Uploaded by

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

PHP LECTURE NOTES DAY 9

PHP Functions

PHP function is a piece of code that can be reused many times. It can take input as argument list and
return value. There are thousands of built-in functions in PHP.

In PHP, we can define Conditional function, Function within Function and Recursive
function also.

Advantage of PHP Functions

Code Reusability: PHP functions are defined only once and can be invoked many times, like in other
programming languages.

Less Code: It saves a lot of code because you don't need to write the logic many times. By the use of
function, you can write the logic only once and reuse it.

Easy to understand: PHP functions separate the programming logic. So it is easier to understand the
flow of the application because every logic is divided in the form of functions.

PHP User-defined Functions

We can declare and call user-defined functions easily. Let's see the syntax to declare user-defined
functions.

Syntax

function functionname(){
//code to be executed
}
Note: Function name must be start with letter and underscore only like other labels in PHP. It can't be
start with numbers or special symbols.

PHP Functions Example

<?php
function sayHello(){
echo "Hello PHP Function";
}
sayHello();//calling function
?>

Output:

PHP Function Arguments

We can pass the information in PHP function through arguments which is separated by comma.

PHP supports Call by Value (default), Call by Reference, Default argument values and Variable-
length argument list.

Let's see the example to pass single argument in PHP function.

<?php
function sayHello($name){
echo "Hello $name<br/>";
}
sayHello("Sonoo");
sayHello("Vimal");
sayHello("John");
?>

Output:
PHP Call By Reference

Value passed to the function doesn't modify the actual value by default (call by value). But we can do
so by passing value as a reference.

By default, value passed to the function is call by value. To pass value as a reference, you need to use
ampersand (&) symbol before the argument name.
Let's see a simple example of call by reference in PHP.

<?php
function adder(&$str2)
{
$str2 .= 'Call By Reference';
}
$str = 'Hello ';
adder($str);
echo $str;
?>

Output:

PHP Function: Default Argument Value

We can specify a default argument value in function. While calling PHP function if you don't specify
any argument, it will take the default argument. Let's see a simple example of using default argument
value in PHP function.

<?php
function sayHello($name="Sonoo"){
echo "Hello $name<br/>";
}
sayHello("Rajesh");
sayHello();//passing no value
sayHello("John");
?>

Output:
PHP Function: Returning Value

Let's see an example of PHP function that returns value.

<?php
function cube($n){
return $n*$n*$n;
}
echo "Cube of 3 is: ".cube(3);
?>

Output:

PHP Parameterized Function

PHP Parameterized functions are the functions with parameters. You can pass any number of
parameters inside a function. These passed parameters act as variables inside your function.

They are specified inside the parentheses, after the function name.

The output depends upon the dynamic values passed as the parameters into the function.
PHP Parameterized Example 1

Addition and Subtraction

In this example, we have passed two parameters $x and $y inside two functions add() and sub().

<!DOCTYPE html>
<html>
<head>
<title>Parameter Addition and Subtraction Example</title>
</head>
<body>
<?php
//Adding two numbers
function add($x, $y) {
$sum = $x + $y;
echo "Sum of two numbers is = $sum <br><br>";
}
add(467, 943);

//Subtracting two numbers


function sub($x, $y) {
$diff = $x - $y;
echo "Difference between two numbers is = $diff";
}
sub(943, 467);
?>
</body>
</html>

Output:

PHP Parameterized Example 2

Addition and Subtraction with Dynamic number

In this example, we have passed two parameters $x and $y inside two functions add() and sub().

<?php
//add() function with two parameter
function add($x,$y)
{
$sum=$x+$y;
echo "Sum = $sum <br><br>";
}
//sub() function with two parameter
function sub($x,$y)
{
$sub=$x-$y;
echo "Diff = $sub <br><br>";
}
//call function, get two argument through input box and click on add or sub button
if(isset($_POST['add']))
{
//call add() function
add($_POST['first'],$_POST['second']);
}
if(isset($_POST['sub']))
{
//call add() function
sub($_POST['first'],$_POST['second']);
}
?>
<form method="post">
Enter first number: <input type="number" name="first"/><br><br>
Enter second number: <input type="number" name="second"/><br><br>
<input type="submit" name="add" value="ADDITION"/>
<input type="submit" name="sub" value="SUBTRACTION"/>
</form>

Output:

You might also like