Home > php教程 > PHP源码 > 按照RATE修改变量或数组单元的值

按照RATE修改变量或数组单元的值

PHP中文网
Release: 2016-05-25 16:58:14
Original
1485 people have browsed it

按照RATE修改变量或数组单元的值

<?php

/**
 * 按照RATE修改变量或数组单元的值
*
 * @author yearnfar
*/

define(&#39;RATE&#39;, 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 = &#39;number&#39;.$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);
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template