按照RATE修改变量或数组单元的值
程序员文章站
2022-04-10 18:10:15
...
按照RATE修改变量或数组单元的值
<?php /** * 按照RATE修改变量或数组单元的值 * * @author yearnfar */ define('RATE', 0.5); function affectNumber(&$number1, &$number2=0, &$number3=0, &$number4=0, &$number5=0) { $count = func_num_args(); if ($count<1 || $count>5) { exit("param error!"); } for ($i=1; $i<=$count; $i++) { $varname = 'number'.$i; $$varname = $$varname * RATE; } } // 例子1 $a = 80; $b = 90; $c = 100; affectNumber($a, $b, $c); echo $a."rn"; echo $b."rn"; echo $c."rn"; function affectArray(&$arr) { $args = func_get_args(); if (empty($args)) { exit("param error!"); } array_shift($args); foreach ($arr as $key => $value) { if (empty($args) || in_array($key, $args)) { $arr[$key] = $value * RATE; } } } //例子2 $arr = array(80, 90, 100); affectArray($arr, 0, 2); print_r($arr);