TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-15-2008, 02:26 PM   #1 (permalink)
The Contributor
 
Join Date: May 2008
Location: Iran
Posts: 53
Thanks: 33
Yoosha is on a distinguished road
Help Define optional argument for function

Hi,
How to define optional argument for function?
Send a message via Yahoo to Yoosha
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: 1,393
Thanks: 5
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
Posts: 1,044
Thanks: 193
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.
__________________
VillageIdiot can have my babbies ;d
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: 386
Thanks: 47
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.
__________________
Signatures are nothing but incriminating.
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: 1,393
Thanks: 5
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: 386
Thanks: 47
Aaron is on a distinguished road
Default

Thats why I posted that.
__________________
Signatures are nothing but incriminating.
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)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
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
 
Join Date: May 2008
Location: Iran
Posts: 53
Thanks: 33
Yoosha is on a distinguished road
Default

This is right(I think):

myfunction($Taco, [$beans], [$rice])
...
Send a message via Yahoo to Yoosha
Yoosha is offline  
Reply With Quote
Old 06-17-2008, 09:04 PM   #10 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
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)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
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
 
Join Date: May 2008
Location: Iran
Posts: 53
Thanks: 33
Yoosha is on a distinguished road
Confused

Define optional argument for function...

No other...
Send a message via Yahoo to Yoosha
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 Search this Thread
Search this Thread:

Advanced Search
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 08:20 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design