1

可以将值传递给函数中的特定参数吗?

function fun1($a,$b)
{
echo $b;
}
@fun1(123);
4

4 回答 4

3

Functions can be defined so that they do not require all parameters. For example:

function foo($a, $b = 2) {
    echo $a + $b;
}

foo(1); //gives 3

Read about default function values here

However, you cannot pass in later parameters without specifying earlier ones. Some simple programming-function-parameters-basics... when you do foo($b) the function has no idea that the variable was named b... It just gets the data; usually a primitive type (in this case an int) or a reference.

You haven't specified how you're using these variables, so you may want to give a dummy value like "-1" to $a (and handle it in your function) (foo(-1, 123)), or rewrite your function so that $a is the second parameter with the default value. (function foo($b, $a = NULL))

That's why you must pass the variables in order; the function assumes you're using it right, and it lines up the values passed with the function definition. function foo($a, $b) means "I'm assuming I should associate your first value with a and your second value with b)".


With your original example function foo($a, $b):

No context, so I would just say do this function foo($b, $a = some_default_value). However, I'm assuming you're using $a and $b equally so you could check to see if it was some default-invalid-value and act on it. However, if your function performs different tasks depending on the (number of) parameters passed, you probably want to separate your function.

If you insist on not switching the order, you could call foo(-1, 123) with a dummy value and check it. Again though, same problem as above


Edit: You've given another example foo($a, $b, $c) and you said you want to do foo($b) to update the middle value. See the explanation in the first paragraph about how a function knows what parameter is what.

If you mean you want to pass an arbitrary set of variables to a function and it knows which ones it got? Again I don't think this is the best practice (you'll need to give us more detail about how you're using this) but you could pass an associative array:

function foo($arr) {
    if (isset($arr['a'])) {
        echo $a;
    }
    if (isset($arr['b'])) {
        echo $b;
    }
    if (isset($arr['c'])) {
        echo $c;
    }
}
foo(array('b' => 123));

I feel horrible after writing this function :P


<?php
function FUN1($a, $b)
{
    echo "HI";
    echo $b;
} //$_a=    123; //error_reporting (E_ALL ^ E_NOTICE ^ E_WARNING); //$b=23; echo @FUN1(123);//it gives HI123 
?>

I formatted your function. Firstly, when I tried that call it doesn't give me "HI123". Secondly, @ is bad practice and really slows down the code. Thirdly, you don't echo FUN1 since it doesn't return anything; your function prints the stuff itself.

You (your student) are/is going in the wrong direction. As I said in my comment, functions already have a beautiful way of sorting out the parameters. Instead of trying to do something funky and work around that, just change your approach.

The example above has no real use and I'm sure in actual code you should just write different functions when you're setting different variables. like setA($a) setB($b) setC($c) setAll($a, $b, $c) and use them accordingly. Arrays are useful for easy variable length functions, but if you're checking each tag to do something, then something's wrong.

于 2012-07-18T16:08:36.237 回答
1

如果你只想传递一个参数,你可以像这样创建一个包装函数:

function PassOne($arg)
{
   fun1(NULL,$arg);
}


function fun1($a,$b)
{
 echo $b;
}
于 2012-07-18T16:18:10.043 回答
1

原谅任何不准确之处。自从我用 PHP 编码以来已经有一段时间了。

如果要确保参数的顺序,可以将单个数组作为参数传递。

$args = array(
    'name' => 'Robert',
    'ID' => 12345,
    'isAdmin' => true
);

example($args);

function example($args)
{
    echo $args['name']; // prints Robert
    echo $args['ID']; // prints 12345
    echo $args['isAdmin']; // prints true
}

使用这种方法,您还可以将默认值硬编码到函数中,仅在参数数组中提供它们时替换它们。例子:

$args = array(
    'name' => 'Robert',
    'ID' => 12345
    // Note that I didn't specify whether Robert was admin or not
);

example($args);

function example($args)
{
    $defaultArgs = array(
        'name' => '',
        'ID' => -1,
        'isAdmin' => false // provides a default value to incomplete requests
    );

    // Create a new, mutable array that's a copy of the default arguments
    $mixArgs = $defaultArgs;

    // replace the default arguments with what was provided
    foreach($args as $k => $v) {
        $mixArgs[$k] = $v;
    }

    /* 
      Now that we have put all the arguments we received into $mixArgs,
      $mixArgs is mix of supplied values and default values.  We can use
      this fact to our advantage:
    */

    echo $mixArgs['name']; // prints Robert

    // if ID is still set to the default value, the user never passed an ID
    if ($mixArgs['ID'] == -1) {
        die('Critical error! No ID supplied!');  // use your imagination
    } else {
        echo mixArgs['ID']; // prints 12345
    }

    echo mixArgs['isAdmin']; // prints false

    // ... etc. etc.
}

2018 年的 PHP 语法和默认值

function example($args=[], $dftArgs=['name'=>'', 'ID' => -1, 'isAdmin'=>false])
{
    if (is_string($args)) 
       $args = json_decode($args,true); // for microservice interoperability
    $args = array_merge($dftArgs,$args); 
    // ... use $args
}
//  PS: $dftArgs as argument is not usual, is only a generalization
于 2012-07-18T16:21:39.447 回答
0

不。

NULL但是按照惯例,您可以通过传入该位置来跳过内置函数的参数:

fun1(NULL, 123);

显然这对所有事情都没有意义 - 例如这没有意义:

$result = strpos(NULL, 'a string');

对于用户定义的函数,您可以以任何您认为合适的方式处理参数 - 但您可能会发现func_get_arg()/func_get_args()对使用不确定数量参数的函数有用。

另外,不要忘记您可以通过定义默认值来使参数成为可选:

function fun ($arg = 1) {
  echo $arg;
}

fun(2); // 2
fun();  // 1

请注意,默认值只能在最右边的参数上定义。如果参数右边的参数没有默认值,则不能给参数一个默认值。所以这是非法的:

function fun ($arg1 = 1, $arg2) {
  // Do stuff heere
}
于 2012-07-18T16:05:56.040 回答