w3schools Logo
Monday 21 May 2012
W3schools.in Developer Network
HTML tutorials
CSS tutorials
php tutorials and scripts
MySql tutorials
Learn C Programming
Operating system

default arguments functions php
From w3schools.in

If you are unaware of default argument in function then you should know that you candefine the function with default arguments in PHP as you can do it C and C++. Providing default arguments in the function can be very useful when you’ve to extend the the functionality of the previously written functions.

Example of using default argument in the function of PHP

Let’s suppose that you’ve already written a function to “calculate the amount collected in day by site”

function getAmountCollected($day)
{
//code for function goes here
}

Assume that this function is called from around 20 places in your project. After sometime, your client asked you to find the ” amount collected by site in that date range”. For this case, you need to pass two parameters(start date and end date) to the function.

Do you write another function or extend the functionality of the same function?

I would use the same function for this purpose to minimize the code. But one major problem, we’ve already called the functions from many places with the single argument. How can we define the same function which can be called both with single and double argument? In this case, default argument comes handy,

function getAmountCollected($day, $end_day=’none’)
{
//code for function goes here
}

As you can see above, we’ve placed the second argument in this function like “$end_day=’none’“, which is called the default argument of the function. You can call this function with both single and double arguments.

And, when the second argument is not supplied while calling the function, the variable “$end_day” contains “none” value in it. i.e when we call this function called like this with single argument

getAmountCollected(’2010-09-14′); //remember single argument

then the second parameter will have “none” value in it in the called function.


This page was last modified on 14 Sep 2011 at 17:09:23.
Gautam Kumar
EDP Manager
http://www.connectsin.com
Related Posts
» Useful functions to tighten the security in PHP
» CodeIgniter A recommendation for PHP Programmer
» PHP Framworks Why when and which
» Solving Floating point number precision lost problem in PHP
» Handling array of HTML Form Elements in JavaScript and PHP
» How to filter user submitted data easily in PHP
» Prevent form post request from another domain in PHP
» Flaw in and or logical operator php
» Default arguments functions php
» Submit Form without Refreshing Page with Jquery
» MySQL Performance Tips
» Free ajax chat applications in PHP
» Return More Than One Value From a Function in PHP
» PHP Optimization Tips
» PHP Error Handling
» Useful PHP Classes and Libraries
» Useful Tools For PHP Developers
» PHP Frameworks
» Web based HTML Editors
» 301 redirect in PHP and .htaccess
» Hiding PHP file extension
» Rredirect Browser HTTPS SSL PHP
» Tighten php security functions
» PHP Framworks