TalkPHP
 
 
Account Login
Latest Articles
» How to keep your forms from double posting data
» cURL Basics
» Securing your PHP applications Part 1
» The way the function rolls
» Database Abstraction with Zend_Db - Part 2
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Display Modes
Old 06-15-2008, 02:26 PM   #1 (permalink)
The Contributor
 
Yoosha's Avatar
 
Join Date: May 2008
Posts: 36
Thanks: 24
Yoosha is on a distinguished road
Help Define optional argument for function

Hi,
How to define optional argument for function?
__________________
Y.P.Y
Yoosha is offline  
Reply With Quote
Old 06-15-2008, 03:42 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 622
Thanks: 2
Salathe is on a distinguished road
Default

PHP Code:
function myfunction($myarg 'default')
{
    echo 
'You specified, '$myarg'<br>';
}

myfunction('a custom value');
myfunction(); 
__________________
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Yoosha (06-17-2008)
Old 06-15-2008, 04:14 PM   #3 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Location: On your Hard Drive, hiding like a Virus
Posts: 774
Thanks: 154
Orc is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
PHP Code:
function myfunction($myarg 'default')
{
    echo 
'You specified, '$myarg'<br>';
}

myfunction('a custom value');
myfunction(); 
That would output a custom value on the first function return, and default on the second return.
__________________
Wax on, Wax off
Orc is offline  
Reply With Quote
Old 06-15-2008, 08:23 PM   #4 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 368
Thanks: 44
Aaron is on a distinguished road
Default

Why can't we just put optional parentheses in brackets like php.net?

myfunction(Taco,[$beans],[$rice])


mm I'm hungry now.


To answer your question
make it equal something in the function call like demonstrated above.

Code:
function myfunction($myarg = 'default')
If you specify a parameter, it will overrule what you put in for defaults.
__________________

I feel better hating IE.
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 06-15-2008, 09:10 PM   #5 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 622
Thanks: 2
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Aaron View Post
Why can't we just put optional parentheses in brackets like php.net?

myfunction(Taco,[$beans],[$rice])
That's simply a notation style used by the PHP Manual, nothing to do with actual PHP code.
__________________
Salathe is offline  
Reply With Quote
Old 06-15-2008, 09:12 PM   #6 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 368
Thanks: 44
Aaron is on a distinguished road
Default

Thats why I posted that.
__________________

I feel better hating IE.
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
The Following User Says Thank You to Aaron For This Useful Post:
Yoosha (06-17-2008)
Old 06-15-2008, 11:09 PM   #7 (permalink)
The Gregarious
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas
Posts: 651
Thanks: 24
delayedinsanity is on a distinguished road
Default

Technically you could have an unlimited amount of optional arguments. The first way, and probably the least aesthetically pleasing would be to define a single argument and check if it's an array or not;

PHP Code:
function snuffleluffagus ($mArgs) {

    if (
is_array($mArgs)) {
        
$intNumArgs count($mArgs);
        
// ..
    
} else {
        
// ..
    
}


..and the second would use PHP's built in functionality, with func_num_args() and func_get_args().

PHP Code:
// example borrowed directly from PHP.net

function foo () {

    
$numargs func_num_args();
    echo 
"Number of arguments: $numargs<br />\n";
    if (
$numargs >= 2) {
        echo 
"Second argument is: " func_get_arg(1) . "<br />\n";
    }
    
$arg_list func_get_args();
    for (
$i 0$i $numargs$i++) {
        echo 
"Argument $i is: " $arg_list[$i] . "<br />\n";
    }


-m
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
Yoosha (06-17-2008)
Old 06-16-2008, 09:05 AM   #8 (permalink)
The Wanderer
 
Join Date: May 2008
Posts: 10
Thanks: 0
Folio is on a distinguished road
Default

remember, you could set the arg to NULL - then if it exists when the function is called, the value will be set - if not, it will be NULL

function myFunction($arg = NULL) {}
Folio is offline  
Reply With Quote
The Following User Says Thank You to Folio For This Useful Post:
Yoosha (06-17-2008)
Old 06-17-2008, 07:47 AM   #9 (permalink)
The Contributor
 
Yoosha's Avatar
 
Join Date: May 2008
Posts: 36
Thanks: 24
Yoosha is on a distinguished road
Default

This is right(I think):

myfunction($Taco, [$beans], [$rice])
...
__________________
Y.P.Y
Yoosha is offline  
Reply With Quote
Old 06-17-2008, 09:04 PM   #10 (permalink)
The Addict
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 316
Thanks: 3
xenon is on a distinguished road
Default

Quote:
Originally Posted by Yoosha View Post
This is right(I think):

myfunction($Taco, [$beans], [$rice])
...
That isn't even possible not kewl man, not kewl
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 06-17-2008, 09:08 PM   #11 (permalink)
The Gregarious
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas
Posts: 651
Thanks: 24
delayedinsanity is on a distinguished road
Default

For real xenon.

PHP Code:
unset ($beans$rice);
new 
Taco ($hamburger$tomato$sourcream) or die("go home"); 
-m
delayedinsanity is offline  
Reply With Quote
Old 06-24-2008, 05:07 PM   #12 (permalink)
The Acquainted
 
drewbee's Avatar
 
Join Date: May 2008
Posts: 175
Thanks: 9
drewbee is on a distinguished road
Default

PHP Code:
class Burrito extends Taco
{
     var 
$wrap_shell;
     var 
$butter;
     var 
$steak;
     var 
$cheese;
     var 
$ingrediants = array();
     var 
$burrito;
     function 
Burrito()
     {
         
parent::__construct();
         
$this->ingredients[] = $this->wrap_shell;;
         
$this->ingredients[] = $this->butter;
         
$this->ingredients[] = $this->steak;
         
$this->ingredients[] = $this->cheese;
     }
 
     function 
stealTheGoods()
     {
         unset(
$this->hamburger);
         
$this->ingredients[] = $this->tomato;
         
$this->ingredients[] = $this->sourcream;
 
         
     }
     function 
makeBurrito()
     {
          
$this->burrito implode('()'$this->ingredients);
     }
 
     function 
eat()
     {
          if (!empty(
$this->burrito))
          {
              echo 
"This burrito is delicious!";
          }
          else
          {
              echo 
"The burrito has not been made yet :(";
          }
     }

Hmm... I always wondered how to uselessly waste 5 minutes... lol.

Last edited by drewbee : 06-25-2008 at 01:19 AM.
Send a message via AIM to drewbee
drewbee is offline  
Reply With Quote
Old 06-25-2008, 05:37 AM   #13 (permalink)
The Contributor
 
Yoosha's Avatar
 
Join Date: May 2008
Posts: 36
Thanks: 24
Yoosha is on a distinguished road
Confused

Define optional argument for function...

No other...
__________________
Y.P.Y
Yoosha is offline  
Reply With Quote
Old 06-25-2008, 01:22 PM   #14 (permalink)
The Acquainted
 
drewbee's Avatar
 
Join Date: May 2008
Posts: 175
Thanks: 9
drewbee is on a distinguished road
Default

lol Hey Yoosha, we kinda went a little crazy with your thread and the taco thing.

The first few posts of this thread correctly show how to do that though.

Do you still have questions about this?
__________________
There are No Stupid Questions. But there a LOT of Inquisitive Idiots.
Send a message via AIM to drewbee
drewbee is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:13 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0