|
Posted by Markus Ernst on 09/26/06 10:14
Jiri Fogl schrieb:
> What about reference-passed variable as an argument?
>
> For example:
>
> <?php
>
> function MyFunc ($input, &$return_array)
> {
> if (!empty($input))
> {
> $return_array = Array($input, date('H:m:s'));
> return true;
> }
> else
> return false;
> }
>
> $a = Array();
> if (MyFunc('Hello', $a))
> print_r($a);
> else
> echo 'Empty input!';
>
>
> ?>
Just as an addition - this special case could also be handled without a
reference, for example:
<?php
function MyFunc ($input)
{
if (!empty($input))
return array($input, date('H:m:s'));
else
return false;
}
if ($a = MyFunc('Hello'))
print_r($a);
else
echo 'Empty input!';
?>
Or, if you need to change an existing array:
<?php
function MyFunc ($input, $arr)
{
if (!empty($input)) {
$arr[] = $input;
$arr[] = date('H:m:s');
return $arr;
}
else
return false;
}
$arr = array('some', 'data');
if ($new_arr = MyFunc('Hello', $arr))
$arr = $new_arr;
else
echo 'Empty input!';
print_r($arr);
?>
--
Markus
Navigation:
[Reply to this message]
|