Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 26th, 2007, 3:00 PM   #1
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 439
Rep Power: 4 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Question Function Parameters

ok let's assume I have a function with four parameters for instance:
function someFunction($var1, $var2, $var3, $var4){
...
}
Now in the various functions that come bundled with PHP there are certain functions that come bundled with optional parameters (that don't necessarily have to be included for the function to behave properly). For instance, the substr function which is described as follows:
string substr ( string string, int start [, int length] )
where the length parameter is optional.

My question is the following: How do I specify a parameter as optional? Or will PHP simply accept the function with one parameter passed to it and it's up to me to code the situation accordingly? For instance:
function someFunction($var1, $var2, $var3, $var4){
if(isset($var1) && isset($var2) && isset($var3) && isset($var4)){
...
}elseif(isset($var1) && isset($var2) && isset($var3)){
...
}
}
In this way making $var 4 an optional parameter? Any sample code to illustrate the idea would be appreciated.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Jan 26th, 2007, 3:39 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You can have a variable number of arguments in PHP (similar to C printf, etc.), but I think you're actually talking about default arguments. See the manual here for defaults, here for variable length argument lists.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jan 26th, 2007, 5:30 PM   #3
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 439
Rep Power: 4 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Thanks DaWei yeah I meant a variable length argument list. Here's the function that I wrote with it:[PHP]function fluctuate(){
$args = func_get_args();
switch(func_num_args()){
case 1:
if(!is_string($args[0])) return !trigger_error("bool <strong>fluctuate</strong> ( string filename [, string mode [, string string [, int mode]]]) &rarr; <em>filename</em> is not a string", E_USER_ERROR);
if(!file_exists($args[0])) return !trigger_error("bool <strong>fluctuate</strong> ( string filename [, string mode [, string string [, int mode]]]) &rarr; Nonexistent file cannot be deleted", E_USER_NOTICE);
if(unlink($args[0])){
return true;
}else{
return !trigger_error("bool <strong>fluctuate</strong> ( string filename [, string mode [, string string [, int mode]]]) &rarr; Unable to delete file", E_USER_WARNING);
}
case 4:
if(!is_string($args[0])) return !trigger_error("bool <strong>fluctuate</strong> ( string filename [, string mode [, string string [, int mode]]]) &rarr; <em>filename</em> is not a string", E_USER_ERROR);
if(!is_string($args[1]) || strlen($args[1]) !== 1) return !trigger_error("bool <strong>fluctuate</strong> ( string filename [, string mode [, string string [, int mode]]]) &rarr; <em>mode</em> is not a character", E_USER_ERROR);
if(!is_int($args[3])) return !trigger_error("bool <strong>fluctuate</strong> ( string filename [, string mode [, string string [, int mode]]]) &rarr; <em>mode</em> is not an int", E_USER_ERROR);
$args[1] = strtolower($args[1]);
$ffd = null;
switch($args[1]){
case 'a':
if(!file_exists($args[0])) return !trigger_error("bool <strong>fluctuate</strong> ( string filename [, string mode [, string string [, int mode]]]) &rarr; Cannot append to nonexistent file", E_USER_WARNING);
if(!is_writeable($args[0])) return !trigger_error("bool <strong>fluctuate</strong> ( string filename [, string mode [, string string [, int mode]]]) &rarr; File is not writeable", E_USER_WARNING);
if(false === ($ffd = fopen($args[0], "a"))) return !trigger_error("bool <strong>fluctuate</strong> ( string filename [, string mode [, string string [, int mode]]]) &rarr; Unable to open file", E_USER_WARNING);
break;
case 'w':
if(false === ($ffd = fopen($args[0], "w"))) return !trigger_error("bool <strong>fluctuate</strong> ( string filename [, string mode [, string string [, int mode]]]) &rarr; Unable to open file", E_USER_WARNING);
if(!file_exists($args[0])) return !trigger_error("bool <strong>fluctuate</strong> ( string filename [, string mode [, string string [, int mode]]]) &rarr; Cannot write to nonexistent file", E_USER_WARNING);
if(!is_writeable($args[0])) return !trigger_error("bool <strong>fluctuate</strong> ( string filename [, string mode [, string string [, int mode]]]) &rarr; File is not writeable", E_USER_WARNING);
break;
default:
return !trigger_error("bool <strong>fluctuate</strong> ( string filename [, string mode [, string string [, int mode]]]) &rarr; Illegal mode specified", E_USER_ERROR);
}
if(stream_set_write_buffer($ffd, 0) !== 0) return !trigger_error("bool <strong>fluctuate</strong> ( string filename [, string mode [, string string [, int mode]]]) &rarr; Unable to buffer stream", E_USER_WARNING);
if(fwrite($ffd, $args[2]) === false) return !trigger_error("bool <strong>fluctuate</strong> ( string filename [, string mode [, string string [, int mode]]]) &rarr; Unable to write to file", E_USER_WARNING);
if(!fclose($ffd)) return !trigger_error("bool <strong>fluctuate</strong> ( string filename [, string mode [, string string [, int mode]]]) &rarr; Unable to close file", E_USER_WARNING);
if(!chmod($args[0], $args[3])) return !trigger_error("bool <strong>fluctuate</strong> ( string filename [, string mode [, string string [, int mode]]]) &rarr; Unable to change file permissions", E_USER_WARNING);
return true;
default:
return !trigger_error("bool <strong>fluctuate</strong> ( string filename [, string mode [, string string [, int mode]]]) &rarr; Incorrect number of arguments passed", E_USER_ERROR);
}
}[/PHP]Who knew file operations could produce so many errors *_* lol
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Feb 9th, 2007, 11:40 PM   #4
writephp
Newbie
 
Join Date: Jan 2007
Posts: 3
Rep Power: 0 writephp is on a distinguished road
you could just set it to null in the function

so for instance

function something($var1,$var2,$var3,$var4=NULL){
}

so say you have to declare 1 2 and 3 and then 4 is set to null unless otherwise assigned
__________________
Learn PHP & MySQL
http://www.writephp.net
-----------------------------------------------------
For Sale
http://www.phantomdown.com
writephp is offline   Reply With Quote
Old Feb 10th, 2007, 1:02 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
That's a default. You can actually set it to any default value you like. He said he meant a variable length list, though, which isn't the same thing.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 10th, 2007, 6:36 AM   #6
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by grimpirate View Post
My question is the following: How do I specify a parameter as optional?
That seems to be asking more about default values than variable length arguments, DaWei.
Arevos is offline   Reply With Quote
Old Feb 10th, 2007, 8:22 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I know it seems to. I expressed as much in post #2, and was told differently in post #3. If we're quoting,
Quote:
Thanks DaWei yeah I meant a variable length argument list.
Since the links I provided in post #2 covered both eventualities, I took the OP at his word. Personally, I don't see anything wrong with any of my posts here. If the OP is mistaken, he still has both links.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 10th, 2007, 9:19 AM   #8
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by DaWei View Post
I know it seems to. I expressed as much in post #2, and was told differently in post #3.
Ah, you're quite right. My mistake.
Arevos is offline   Reply With Quote
Old Mar 14th, 2007, 6:37 PM   #9
woozy
Newbie
 
Join Date: Mar 2007
Posts: 15
Rep Power: 0 woozy is on a distinguished road
PHP is pretty flexible. In defining a function you can declare as few or as many variables as you wish.

The following will all do the exact same thing:

1)
function eat($subject, $object, $verb, $adjective){
if (! isset($verb)){$verb = "eats";}
if (isset($adjective)){
return $subject." ".$verb." ".$adjective." ".$object.".";
}else{
return $subject." ".$verb." ".$object.".";
}

eat("man", "chicken");

will return "man eats chicken" and the verb and the adjective weren't nescessarily. It'll work but you will get warning in your error log that it was "expecting" four parameters and only got two.

on the other hand

eat("man", "chicken", "savors", "juicy", "pickle", "my", "bottom");

will return "man savors juicy chicken" and leave no warning message what so ever. Giving too many arguments does nothing at all.

2)
function eat($subject, $object, $verb="eats"){
$adjective = func_get_arg(3);
if ($adjective){
return

}

eat("man", "chicken");

will return "man eats chicken." and gave no warnings. Why? function only expects three arguments but the $verb has a default setting to "eats" so that when it isn't supplied explicitly, "eats" is supplied by default.

eat("man", "chicken", "savors", "juicy", "pickle", "my", "bottom");

will return "man savors juicy chicken." Why? The function was "expecting" three parameters ($subject, $object, and $verb) and got them. The line:

$adjective = func_get_arg(3);

means that if there is a fourth parameter (3 is the index of the 4th parameter), set $adjective to it. If there isn't a fourth parameter $adjective is set to FALSE.

3)
function eat(){
$args = func_get_args();
$num = func_num_args();
if ($num < 2){
die ("Need to give two arguments at least");
}
else {
$subject = $args[0];
$object = $args[1];
}
if ($nums == 2){
$verb = "eats";
}else{
$verb = $args[2];
}
if ($nums >= 4){
$adjective = $args[3];
return $subject." ".$verb." ".$adjective." ".$object.".";
}else{
return $subject." ".$verb." ".$object.".";
}

eat("man", "chicken");

returns "man eats chicken." Why? eat() doesn't "expect" any parameters.

$args = func_get_args();

sets $args to the array ("man", "chicken").

$num = func_num_args();

set $num equal to 2. $num == 2 so $verb is set to "eats". $num is not >= 4 so "man eats chicken." is returned.

eat("man", "chicken", "savors", "juicy", "pickle", "my", "bottom");

yeilds "man savors juicy chicken". Which is pretty clear as $args ==("man", "chicken", "savors", "juicy", "pickle", "my", "bottom") and $num == 7.

---
As a rule of thumb define the parameters you need provided, set defaults for parameters you need but expect to be optional, and don't define the optional ones.

example:

function member($name, $id, $hobby){
if (! isset($id)) $id = set_id();
$hobbies = array_splice(func_get_args(),2);
}

Then

member("woozy",,"php","cooking", "skiing","being obtuse", "calling in sick");

will set $hobbies equal to the array ("php", "cooking", "skiing", etc.) and you can send as many arguments as you darn well please.




}
woozy is offline   Reply With Quote
Old Mar 14th, 2007, 7:41 PM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Please read the forum's rules/faq. In the absence of code tags I can't tell if your code is really ugly, or just seems that way.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Combining languages titaniumdecoy Other Programming Languages 12 Jul 13th, 2006 3:03 PM
Compiling Maverik 6.2 (from C) megamind5005 C 16 May 3rd, 2006 6:41 PM
libraries matko C 1 Jan 22nd, 2006 3:12 PM
Jackpot game zorin Visual Basic 3 Jun 10th, 2005 2:19 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 5:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:54 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC