Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 04-15-2010, 04:13 AM Functions Args
Novice Talker

Posts: 14
Name: Wade
Trades: 0
Is it possible to add args to a function from an array dynamically ?
PHP Code:
<?php

$foo 
= array('arg1''arg2');

function 
foo()
{

echo 
'<pre>';
print_r(func_get_args());
echo 
'</pre>';

}

?>
So when the func_get_args is called, it prints

PHP Code:

Array
(
    [
0] => arg1
    
[1] => arg2

Is this possible ?

Thanks in advance.

Last edited by amphtech; 04-15-2010 at 05:10 AM..
amphtech is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-15-2010, 05:31 AM Re: Functions Args
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
I don't see what you are trying to do, but as far as I know, no, you cannot.
But you can pass an array as parameter, and iterate it to extract the key/value pairs.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 04-15-2010, 06:15 AM Re: Functions Args
Novice Talker

Posts: 14
Name: Wade
Trades: 0
Its for a plugin system, the method to add plugin functions into the system has a argument for arguments to be passed into the plugins function.
I was wondering if i could put the args into an array and be passed onto the function.

I hope this has explained it ?
amphtech is offline
Reply With Quote
View Public Profile
 
Old 04-15-2010, 07:37 AM Re: Functions Args
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Then yes you can.
But you should do:
PHP Code:
<?php
function foo($pars){
  foreach(
$pars as $key=>$val){
    $
$key=$val;  //the name in $key is created as a variable with the value "$val"
  
}
}
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 04-15-2010, 08:06 AM Re: Functions Args
Novice Talker

Posts: 14
Name: Wade
Trades: 0
So is not possible to use an array eg:
PHP Code:
$foo = array('arg1''arg2'); 
and have each of its values made into seperate function args ?

Example
PHP Code:
$foo = array('arg1''arg2');

// into
functionname('arg1''arg2');

// so if another value is added into the array it will add another arg eg

functionname('arg1''arg2''arg3'); 
Sorry if im confusing you.
amphtech is offline
Reply With Quote
View Public Profile
 
Old 04-15-2010, 10:21 AM Re: Functions Args
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
No, you have to pass the array as a parameter to the function:
PHP Code:
functionName($array
You cannot call the function and expect it to have an array defined outside the function visible inside the function.

And in your example, arg1 and arg2 are just the elements of the array $foo.
If you call the function the way you wrote it, the arguments will always be the same.

If you would use your example, you would do
PHP Code:
$foo=array('arg1','arg2');
functionName($foo); 
Or, if you want to name your parameters:
PHP Code:
function bar($pars){
  foreach(
$pars as $key=>$val){
    $
$key=$val;  //the name in $key is created as a variable with the value "$val"
    
print("The parameter $key value is $val<br/>\n");
  }
}
$foo=array('arg1'=>'val1','arg2'=>'val2');
bar($foo); 
So no, you cannot use func_get_args(), you must iterate through the array given in parameter, like I showed you previously.

Which gives me:
Quote:
$:> php 3.php
The parameter arg1 value is val1<br/>
The parameter arg2 value is val2<br/>
$:>_
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 04-15-2010 at 10:24 AM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 04-15-2010, 06:46 PM Re: Functions Args
Novice Talker

Posts: 14
Name: Wade
Trades: 0
Thanks tripy for all your help (:
amphtech is offline
Reply With Quote
View Public Profile
 
Old 04-15-2010, 08:31 PM Re: Functions Args
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
I might be misunderstanding something, but take a look at this code and see if it helps you in understanding things in more depth:

PHP Code:
<?php
function myPlugin() {
  
$arguments_passed func_get_args();
  foreach(
$arguments_passed as $an_argument) {
    if (
is_array($an_argument)) {
      echo 
'<pre>'print_r($an_argumenttrue), '</pre>';
    } else {
      echo 
$an_argument,'<br>';
    }
  }
}
myPlugin('1st parameter');
echo 
'<hr><br>';
myPlugin('1st parameter''2nd parameter'85);
echo 
'<hr><br>';
myPlugin(array('Item 1','Item 2','Item 3'));
echo 
'<hr><br>';
myPlugin(array('A'=>'Item 1','B'=>'Item 2','C'=>'Item 3'));
?>
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to Functions Args
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.26231 seconds with 12 queries