6 Functions
Hiren Joshi
Function is used to reuse the code. One of the goals of the software engineering is to reuse the code. Reusing the code has many benefits like Improve consistency and reduces cost.
Some In-Built functions
PHP has many in-built functions which help developers a lot. A function returns a value when it is used. A function can take one or more parameters or it can be parameter-less. To use the function write function name and pass necessary parameters.
- Date:
- General syntax for using date function is date($format)
- $format is a format to display date in particular format.
For example, to display current date type date($format) function.
<?php
$date = date(‘d/m/y’);
echo “Today is $date”;
?>
Then, the output will be,
Today is 08/05/15.
To display the month name as three character month name, use M.
<?php
$d = date(‘d/M/y’);
echo “<h1> Today is $d”;
?>
Then, the output will be,
Today is 08/May/15.
You can use –(hyphen) to differentiate among date, month and year.
<?php
$d = date(‘d/M/y’);
echo “<h1> Today is $d”;?>
Then, the output will be,
Today is 08 –May-15
In format, Y shows year as four digit.
<?php
$d = date(‘d-M-Y’);
echo “<h1> Today is $d”;
?>
Then, the output will be,
Today is 08 –May-2015
D displays day name in three characters.
<?php
$d = date(‘D-M-Y’);
echo “<h1> Today is $d”;
?>
Then, the output will be,
Today is Fri –May-2015
There are lot more options available for format in date function. These format will be explain in the topic when we discuss in detail about string, number and date.
- isset($var)
To test the status of variable, PHP provides many functions.
To test that a variable is set or not isset or empty function is used.
isset($var) takes one parameter – name of variable.
isset function return true if variable is set and the assign value is not null else it will return null value.
For example:
<?php
$d = date(‘D-M-Y’);
echo “<br> $d “;
if(isset($d))
{
echo “<br> Date is defined”;
}
else
{
echo “<br> Date is not defined”;
}
?>
Then the output will be
Today is Fri –May-2015
Date is defined
If you check for following code:
<?php
$c;
if(isset($c))
{
echo “<br> Variable is set”;
}
else
{
echo “<br> Variable is not set”;
}
?>
Then the output will be:
Variable is not set
However, any value is assigned to variable then, it is treated as variable is set.
For example:
<?php
$c = 3;
if(isset($c))
{
echo “<br> Variable is set”;
}
else
{
echo “<br> Variable is not set”;
}
?>
Then the output will be:
Variable is set
- empty($var)
Empty function works same as is set. Only difference is that empty return true when variable is not set and returns false when variable is set. Empty is negating of isset.
<?php
$c;
if(empty($c))
{
echo “<br> Variable is empty”;
}
else
{
echo “<br> Variable is not empty”;
}
?>
Then the output will be
Variable is empty.
But if the variable $c has assign some value, and then it will be treated as not empty.
- is_numeric($var)
The is_numeric($variable) function return true if the variable value is numeric otherwise it will return false.
For example:
<?php
$c = 3;
if(is_numeric($c))
{
echo “<br> Variable has numeric value”;
}
else
{
echo “<br> Variable has non numeric value”;
}
?>
The output of the above code is:Variable has numeric value
In case $var has floating-point value, then is_numeric($var) function is also return true.
The is_numeric($var) function also return true if the $var value is a string which can be converted into a number.
For example:
<?php
$c = ‘55.37’;
if(is_numeric($c))
{
echo “<br> Variable has numeric value”;
}
else
{
echo “<br> Variable has non numeric value”;
}
?>
Then the output will be
Variable has numeric value
User Defined Functions
A function is a collection of statements which can be reused in a program. PHP has many built-in functions. PHP also allows creating user defined function as well. The syntax to create user defined function is:
function <functionname>([$param1, $param2,…])
{\\ statements [return [$value]]; }
The function begins with the keyword function. Then code the function name, set of parenthesis and write statements of function within the function block. Within set of parenthesis , parameters for functions is required to write. A function can have zero or more parameters.
Function name must begin with either letter or underscore followed by letters, numbers or underscores. Within the function block, statements for function are written. These statements can include return statement. As the function reaches to the return statement, it stops executing and returns the value specified by the return statement. If the function does not specify any return value, it will return null value. In case, within a function return statement is not written, then the function will return null value.
When a function is defined, the variables that represent the values which will be passed to the function are known as parameters. While when a function is called at that time the values passed to the function is known as arguments. Arguments and parameters words are used interchangeably in this content.
To call a function type the function name. When a function is called, at that time the arguments in the argument list must be in the same order as the parameters in the parameter list defined by the function and must have compatible data type.
Function naming rules are same as variable naming rule.
Function name must be unique.
Let us write a simple function which is not taking any arguments and not returning any value.
function msg()
{
echo “<h1> This is a message from function </h1>”;
}
To call the function, you just need to type the function name with necessary argument in case it is required.
//call a function;
msg();
The output will be :
This is a message from function
Using Parameters
In PHP, the function parameters can be passed by value or by reference.
By default, the parameters are passed as by value.
When the parameters are passed by value, that time the value of parameter is copied and passed to the function. Hence the function cannot be changed the original parameter.
If the parameters are passed by reference that means PHP sends a pointer to the original parameter instead of sending the copy of parameter value. To pass the parameter by reference, prefix &(ampersand) symbol to the parameter.
A single function can have parameters which can be passed by value and passed by reference simultaneously.
To understand the difference between passing parameters by value and by reference, let’s write examples.
The following code is shown an example in which parameter is passed by value.
// pass the parameters by value function addvalue($value)
{
$value += 10;
echo ‘<br> Value = ‘.$value;
}
$number = 7;
echo ‘<br> The value of $number before calling function is: ‘.$number; addvalue($number);
echo ‘<br> The value $number after calling function is: ‘.$number; The output of the above code is:
The value of $number before calling function is: 7
Value = 17
The value $number after calling function is: 7
The original value is not changed when the parameter is passed by value
// pass the parameters by reference function addvalue(&$value)
{
$value += 10;
echo ‘<br> Value = ‘.$value;
}
$number = 7;
echo ‘<br> The value of $number before calling function is: ‘.$number; addvalue($number);
echo ‘<br> The value $number after calling function is: ‘.$number; The output of the above code is:
The value of $number before calling function is: 7
Value = 17
The value $number after calling function is: 17
The original value is changed when the parameter is passed by reference.
In PHP a function can contain parameters passed by value and pass by reference simultaneously. For instance , the function simulates can have 5 parameters that having parameters pass by value and pass by reference simultaneously.
function simultest(&$v1, $v2, $v3, &$v4, $v5)
By using arguments passing by reference technique a function can return multiple values.
// function return multiple values
function multiplevaluesreturn($num, &$sum,&$product, &$average)
{
$sum = $num + 10;
$product = $num * 10;
$average = $num/10;
}
$num = 10;
multiplevaluesreturn($num,$s,$p,$a);
echo ‘<br> sum = ‘.$s. ‘<t> product = ‘. $p . ‘<t> average = ‘.$a;
Then the output is:
sum = 20 product = 100 average = 1
you can view video on Functions |
6. References:
- Luke Welling, Laura Thomson: PHP and MySQL Web Development, Pearson,
- W. Jason Gilmore: Beginning PHP and MySQL 5 From Novice to Professional,Apress
- Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K.Glass: Beginning PHP5, Apache, and MySQL Web Development, Wrox,
- Robin Nixon: Learning PHP, MySQL, and JavaScript, O’Reilly Media
- Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove:Professional PHP 5, Wrox
- Tim Converse, Joyce Park, Clark Morgan: PHP5 and MySQL Bible
- Joel Murach, Ray Harris: Murach’s PHP and MySQL, Shroff/Murach
- Ivan Bayross, Web Enabled Commercial Application Development Using
- HTML/Javascript/DHTML/PHP , BPB Publications
- Julie C. Meloni, Sams Teach Yourself PHP, MySQL and Apache All in One, Sams
- Larry Ullman, PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide,Pearson Education.
- http://www.php.net
- http://www.w3schools.com/
- http://www.tutorialspoint.com/