Php function arguments

I don’t know how and where I got this idea in my head, but for some reason I thought it was possible. Obviously this will not work after testing, but is there any way to make it work? I want to set $ value2 without having to enter anything at all for $ value1.

function test($value1 = 1, $value2 = 2) {

echo 'Value 1: '.$value1.'<br />';
echo 'Value 2: '.$value2.'<br />';

}

test($value2 = 3);

// output
Value 1: 3
Value 2: 2
+3
source share
8 answers

What you are trying to do is called "keyword arguments" or "named arguments" and is not available in PHP unlike other scripting languages ​​such as Python.

, PHP, , , func_get_args(). , , , .

+4

, .

function test($value1 = null, $value2 = 2) {

echo 'Value 1: '.$value1.'<br />';
echo 'Value 2: '.$value2.'<br />';

}

test(NULL, $value2 = 3);

,

function test($array) {

if(isset($array['value1'])) echo 'Value 1: '.$array['value1'].'<br />';
if(isset($array['value2'])) echo 'Value 2: '.$array['value2'].'<br />';

}

test(array('value2' => 3));

Update:

function test() {
  $args = func_get_args();
  $count = count($args);
  if($count==1) { test1Arg($args[0]); }
  elseif($count == 2) { test2Arg($args[0],$args[1]); }
  else { //void; }
}

function test1Arg($arg1) {
   //Do something for only one argument
}
function test2Arg($arg1,$arg2) {
   //Do something for two args
}
+7

, , ; , , , , , , .

, . .

class test {
    var $value1 = 1;
    var $value2 = 2;

    function __construct() {
        printf( "Value 1 = %s, value 2 = %s\n", $this->value1, $this->value2 );
    }
}

class test2 extends test {
    var $value2 = 42;
}

$me = new test();   // outputs Value 1 = 1, value 2 = 2
$me2 = new test2(); // outputs Value 1 = 1, value 2 = 42

, , . , , .

+1

, , , . , .

. null.

0
function test($value1 = null, $value2 = null) {
  $value1 = $value1 ?: 1;
  $value2 = $value2 ?: 2;
  echo 'Value 1: '.$value1.'<br />';
  echo 'Value 2: '.$value2.'<br />';
}

test(null, 3);
0

AFAIK, , . , (http://php.net/manual/en/functions.arguments.php#example-153).

, test($value2 = 3);, $value2 3, first test.

, , , :

function test($value1 = NULL, $value2 = 2) {

    if ($value1 === NULL)
        $value1 = 1 // $value1 default value

    echo 'Value 1: '.$value1.'<br />';
    echo 'Value 2: '.$value2.'<br />';

}

test(NULL, 3);

// output
Value 1: 1
Value 2: 3

, :

function test($value2 = 2, $value1 = 1) {

    echo 'Value 1: '.$value1.'<br />';
    echo 'Value 2: '.$value2.'<br />';

}

test(3);

// output
Value 1: 1
Value 2: 3
0

PHP, , , .

function func($val1=null, $val2=null) {
    // Set default values.
    if (!$val1) $val1 = 1;
    if (!$val2) $val2 = 2;
    // Your code here.
}

func(null, 74); // Uses the default value of 1 for $val1.

( , ):

function func($arr) {
    // Set default values.
    $val1 = isset($arr[1]) ? $arr[1] : 1;
    $val2 = isset($arr[2]) ? $arr[2] : 2;
    // Your code here.
}

func(array(2 => 74)); // Uses the default value of 1 for $val1.

, , , ,

$val1 = Core::val($arr, 1, 1); // array, key, default
$val2 = Core::val($arr, 2, 2);
0

, , :

    function test($value1, $value2) 
    {

      if ($value1!="") 
      {
         echo 'Value 1: '.$value1.'<br />'; 
      }
      else
      {
         echo 'Value 1: 3'<br />'; 
      }
      echo 'Value 2: '.$value2.'<br />';

    }

    test("", $value2 = 3);

    // output
    Value 1: 3
    Value 2: 2
-1

All Articles