I was stuffing around and I was wondering if it was at all possible to make a function or method, dynamic at all? In what context? well I would like not to have the specify the elements required by the function so at any given day it can be more or less. Confused? so am I! HA

Example:

public function dynamic($x0, $x1, $x2, $x3, etc) {
 // blah blah blah
}

so those $x, one, two, three, etc can be used without them actually being defined maybe i want to use $x0-100. This at all possible or some ideas how we could make it possible?

Thanks, Regards X

Recommended Answers

All 6 Replies

I was stuffing around and I was wondering if it was at all possible to make a function or method, dynamic at all? In what context? well I would like not to have the specify the elements required by the function so at any given day it can be more or less. Confused? so am I! HA

Example:

public function dynamic($x0, $x1, $x2, $x3, etc) {
 // blah blah blah
}

so those $x, one, two, three, etc can be used without them actually being defined maybe i want to use $x0-100. This at all possible or some ideas how we could make it possible?

Thanks, Regards X

Are you talking about having inputs of a function that may not be used but could be?

example

<?php
   function doStuff($a='not set', $b='not set', $c='not set') {
      echo $a. "<br />";
      echo $b. "<br />";
      echo $c. "<br />";
   }

doStuff('a', 'b'); 
//will out put:
//a
//b
//not set
?>

by assigning null to all of the variables in the function, if a value is not passed into the function when called it will automaticly assign null to it and will not give you an error.

Look up func_get_args().

commented: Thanks for the direction! +2

I was stuffing around and I was wondering if it was at all possible to make a function or method, dynamic at all? In what context? well I would like not to have the specify the elements required by the function so at any given day it can be more or less. Confused? so am I! HA

Example:

public function dynamic($x0, $x1, $x2, $x3, etc) {
 // blah blah blah
}

so those $x, one, two, three, etc can be used without them actually being defined maybe i want to use $x0-100. This at all possible or some ideas how we could make it possible?

Thanks, Regards X

public function dynamic()
{
$num_args = func_num_args();
echo "2nd argument=>".func_get_arg(1);
$args_list = func_get_args();
for ($i = 0; $i < $num_args; $i++) {
        echo "Argument $i is: " . $args_list[$i] . "<br />\n";
    }

}
//call to function using 2 params
dynamic($a,$b);
//call to function using 4 params
dynamic($a,$b,$c,$d);
commented: Thanks for the help! +2
<?
//you used any of the function shown below according to your requirement i am concatinating function parameters and exploding in the function the use them

function dynamicFun($val)
{
	$arr=explode(",",$val);
	foreach($arr as $key=>$value)
	{
		print $value."<br>";
	}
}


for($i=1;$i<3;$i++)
{
	print dynamicFun($i);
	for($j=1;$j<$i;$j++)
	{
		$k=$i.",".$j;
		print dynamicFun($k);
	}
}

print "<br>*****************************************************<br>";


function dyna($a=1,$b=2,$c=3)
{
	print $a."--".$b."--".$c."<br>";
}

dyna("9");
dyna("10","11");
dyna("11","12","13");
?>

ok lets say if i have a method which I declare no arguments in but then I call that method using a list of arguments, i would have to run "func_get_args()" first to retrieve those arguments, then use those arguments in that code. correct?

This way I get a dynamic function (not setting the arguments) before use being able to add them as a I please weither it be 1 or 100 arguments. correct?

Thanks, Regards X

ok lets say if i have a method which I declare no arguments in but then I call that method using a list of arguments, i would have to run "func_get_args()" first to retrieve those arguments, then use those arguments in that code. correct?

This way I get a dynamic function (not setting the arguments) before use being able to add them as a I please weither it be 1 or 100 arguments. correct?

Thanks, Regards X

yes, sure this way .
apart from this there are lot many other functions to help the task -
function_get_arg(), func_num_args()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.