note 91863 added to function.func-num-args
| From: | DennisRobinsonfrombasnetworksdotnet at osu1 dot php dot net | Date: | Tue, 30 Jun 2009 04:29:25 +0000 |
| Subject: | note 91863 added to function.func-num-args | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-157683@lists.php.net to get a copy of this message | ||
This function comes in handy, and I believe is the only solution, when you have an optional
parameter that can take any type of data.
For example:
<?php
// $data can be of any type, including null
function my_function($name, $data = null)
{
if ($data !== null)
{
// Do something with $data
// If you call my_function('something'), this WILL NOT be reached
// If you call my_function('something', null), this WILL NOT be reached
}
}
?>
The problem with the above function is that you will never be able to use null as the value for
$data. To fix this, use func_num_args() like so:
<?php
// $data can be of any type, including null
function my_function($name, $data = null)
{
if (func_num_args() >= 2)
{
// Do something with $data
// If you call my_function('something'), this WILL NOT be reached
// If you call my_function('something', null), this WILL be reached
}
}
?>
This solution works because func_num_args() reports exactly how many arguments were passed when the
function was called. It does not take into account when default argument values are used.
----
Server IP: 209.41.74.194
Probable Submitter: 99.246.63.51
----
Manual Page -- http://www.php.net/manual/en/function.func-num-args.php
Edit -- https://master.php.net/note/edit/91863
Del: integrated -- https://master.php.net/note/delete/91863/integrated
Del: useless -- https://master.php.net/note/delete/91863/useless
Del: bad code -- https://master.php.net/note/delete/91863/bad+code
Del: spam -- https://master.php.net/note/delete/91863/spam
Del: non-english -- https://master.php.net/note/delete/91863/non-english
Del: in docs -- https://master.php.net/note/delete/91863/in+docs
Del: other reasons-- https://master.php.net/note/delete/91863
Reject -- https://master.php.net/note/reject/91863
Search -- https://master.php.net/manage/user-notes.php