函数functionName(这里是if子句)可能吗?

时间:2022-11-07 22:33:28
function getTemplate($tpl if ($vars) echo ", $vars";)...function

Is this possible somehow? The above wont work.

这有可能吗?以上不会工作。

Thanks

4 个解决方案

#1


Optional arguments with default values

It looks like you want an optional argument, which you can accomplish by defining a default value in the function definition:

看起来你想要一个可选参数,你可以通过在函数定义中定义一个默认值来完成:

function getTemplate($tpl, $vars=null)   
{

}

You can call this function as getTemplate($foo) or getTemplate($foo,$bar). See the PHP manual page on function arguments for more details.

您可以将此函数称为getTemplate($ foo)或getTemplate($ foo,$ bar)。有关更多详细信息,请参阅函数参数的PHP手册页。

Variable numbers of arguments

It's also possible to write functions which take a variable number of arguments, but you need to use func_num_args, func_get_arg and func_get_args functions to get at them. Here's an example from the manual

也可以编写带有可变数量参数的函数,但是你需要使用func_num_args,func_get_arg和func_get_args函数来获取它们。这是手册中的一个例子

<?php
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";
   }
} 

foo(1, 2, 3);
?>

Calling a function with a variable number of parameters

To round off this answer even more, suppose you'd build an array of 1..n values and wanted to pass it to the foo() function defined above? You'd use call_user_func_array

为了更加完善这个答案,假设您构建了一个1..n值的数组,并希望将它传递给上面定义的foo()函数?你将使用call_user_func_array

$values=(1,2,3,4);
call_user_func_array('foo', $values);

This is the equivalent of calling

这相当于召唤

foo(1,2,3,4);

#2


What's so bad about

真是太糟糕了

function getTemplate($tpl, $vars=null)   {}

?

#3


if ($vars) { getTemplate($tpl, $vars); }
else {{ getTemplate($tpl, null); }

(semi-pseudo code)

#4


Or:

getTemplate($tpl, ($vars)?$vars:null); // not sure

getTemplate($tpl, (!empty($vars))?$vars:null);

Also, if you would like a technique similar to echo:

另外,如果你想要一种类似于echo的技术:

$code = "getTemplate($pl";
if ( $vars ) { $code = $code . ", $vars);"; }
else { $code = $code . ");"; }
$ret = eval($code);

Although this is usually considered bad practice (never say never). Please note that all code sent to eval will be executed directly. So don't put user input in an eval() call.

虽然这通常被认为是不好的做法(从不说永远不会)。请注意,发送到eval的所有代码都将直接执行。所以不要将用户输入放在eval()调用中。

#1


Optional arguments with default values

It looks like you want an optional argument, which you can accomplish by defining a default value in the function definition:

看起来你想要一个可选参数,你可以通过在函数定义中定义一个默认值来完成:

function getTemplate($tpl, $vars=null)   
{

}

You can call this function as getTemplate($foo) or getTemplate($foo,$bar). See the PHP manual page on function arguments for more details.

您可以将此函数称为getTemplate($ foo)或getTemplate($ foo,$ bar)。有关更多详细信息,请参阅函数参数的PHP手册页。

Variable numbers of arguments

It's also possible to write functions which take a variable number of arguments, but you need to use func_num_args, func_get_arg and func_get_args functions to get at them. Here's an example from the manual

也可以编写带有可变数量参数的函数,但是你需要使用func_num_args,func_get_arg和func_get_args函数来获取它们。这是手册中的一个例子

<?php
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";
   }
} 

foo(1, 2, 3);
?>

Calling a function with a variable number of parameters

To round off this answer even more, suppose you'd build an array of 1..n values and wanted to pass it to the foo() function defined above? You'd use call_user_func_array

为了更加完善这个答案,假设您构建了一个1..n值的数组,并希望将它传递给上面定义的foo()函数?你将使用call_user_func_array

$values=(1,2,3,4);
call_user_func_array('foo', $values);

This is the equivalent of calling

这相当于召唤

foo(1,2,3,4);

#2


What's so bad about

真是太糟糕了

function getTemplate($tpl, $vars=null)   {}

?

#3


if ($vars) { getTemplate($tpl, $vars); }
else {{ getTemplate($tpl, null); }

(semi-pseudo code)

#4


Or:

getTemplate($tpl, ($vars)?$vars:null); // not sure

getTemplate($tpl, (!empty($vars))?$vars:null);

Also, if you would like a technique similar to echo:

另外,如果你想要一种类似于echo的技术:

$code = "getTemplate($pl";
if ( $vars ) { $code = $code . ", $vars);"; }
else { $code = $code . ");"; }
$ret = eval($code);

Although this is usually considered bad practice (never say never). Please note that all code sent to eval will be executed directly. So don't put user input in an eval() call.

虽然这通常被认为是不好的做法(从不说永远不会)。请注意,发送到eval的所有代码都将直接执行。所以不要将用户输入放在eval()调用中。