|
Posted by Steve on 04/20/07 05:21
"tasteless" <tasteless@gazeta.pl> wrote in message
news:f0018j$sg2$1@inews.gazeta.pl...
| Hi guys,
|
| I need really hard questions (about 10) about PHP programming (some of
| elements OOP as well, but no MySQL questions - this is different part),
| this questions needs to be very hard, but the experienced senior PHP
| developer should answered on it.
|
| I've already searched in google and google groups archive but without
| any good results. So could anybody help me giving some link or sending
| some stuff to me ?
|
| Thanks and regards - Peter.
this is a good one i think as it is quirky in answering. the requirement for
both questions is that the code is run via a php version where call-back
by-reference is depreciated. the answer must not throw errors/warnings.
goal: validate an array of data using array_walk and print the results.
q: why won't this ever print any $errors?
<?
$numbers = array(1, 56, 999, 1000, 28, 65);
function validateInput(&$value, $key, &$errors)
{
$maxValue = 999;
if ($value <= $maxValue) return;
$errors[$key] = 'Element ' .
$key .
' must be a whole number ' .
'between 0 and ' .
$maxValue .
'.';
}
$errors = array();
array_walk($numbers, 'validateInput', $errors);
print_r($errors);
?>
q: what modification(s) can be made to fix the problem and still use
array_walk?
a:
<?
$numbers = array(1, 56, 999, 1000, 28, 65);
function validateInput(&$value, $key, &$errors)
{
$maxValue = 999;
if ($value <= $maxValue) return;
$errors[1][$key] = 'Element ' .
$key .
' must be a whole number ' .
'between 0 and ' .
$maxValue .
'.';
}
$error = array();
$errors = array('', &$error);
array_walk($numbers, 'validateInput', $errors);
$errors = count($errors) > 1 ? $errors[1] : array();
print_r($errors);
?>
Navigation:
[Reply to this message]
|