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 12-21-2007, 03:59 PM   #1 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Smile Optional Parameters in Custom functions..

Is there a way that you can have optional Parameters of a custom function? As if there was an Optional offset for that custom function for whatever it may be?
Orc is offline  
Reply With Quote
Old 12-21-2007, 04:07 PM   #2 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

You can pre-define variables in functions.

PHP Code:
function intro($name$age$country 'Sweden'$job 'student') {

echo 
'Hello, my name is '.$name.'! I am a '.$age.' year old '.$job.' from '.country;


PHP Code:
intro('Marcus'18); 
Code:
Hello, my name is Marcus! I am a 18 year old student from Sweden
But optionally, you could add:
PHP Code:
intro('Marcus'18'Norway''fisher'); 
Code:
Hello, my name is Marcus! I am a 18 year old fisher from Norway
Tanax is offline  
Reply With Quote
Old 12-21-2007, 04:12 PM   #3 (permalink)
The Frequenter
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

But if you don't need the parameters to have pre-set values, just use nulls. As in:

Code:
function with_custom_params( $param1, $optional1 = null, $optional2 = null)
{ function body }
However, you can't 'skip' optional parameters. considering the previous defined function, you can call:

Code:
with_custom_params( 'param1', 'param2' );
// or
with_custom_params('param1');
, but you can't do this:

Code:
with_custom_params( 'param1', , 'param3'); // will throw a parsing error
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
The Following User Says Thank You to xenon For This Useful Post:
Orc (12-21-2007)
Old 12-21-2007, 04:30 PM   #4 (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 xenon View Post
But if you don't need the parameters to have pre-set values, just use nulls. As in:

Code:
function with_custom_params( $param1, $optional1 = null, $optional2 = null)
{ function body }
However, you can't 'skip' optional parameters. considering the previous defined function, you can call:

Code:
with_custom_params( 'param1', 'param2' );
// or
with_custom_params('param1');
, but you can't do this:

Code:
with_custom_params( 'param1', , 'param3'); // will throw a parsing error

Yeah using nulls popped up in my head after I made this thread :p but Tanax has a point as well! :D Terrific!
Orc is offline  
Reply With Quote
Old 12-29-2007, 11:07 PM   #5 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Another handy little trick is to use func_get_args() and func_num_args(). These are useful when you don't know how many arguments will be passed to a function.

PHP: func_get_args - Manual
PHP: func_num_args - Manual

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT 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 07:31 PM.

 
     

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