How to use empty values in an array?
I开发者_如何学编程 have been trying to shorten this code:
if(empty($soort))
{
$error = 'ja';
$error_soort = 'ja';
$error_omschr_soort = $lang['error_no_entry'];
}
if(empty($klant_id))
{
$error = 'ja';
$error_klant_id = 'ja';
$error_omschr_klant_id = $lang['error_no_entry'];
}
if(empty($contact_id))
{
$error = 'ja';
$error_contact_id = 'ja';
$error_omschr_contact_id = $lang['error_no_entry'];
}
if(empty($aflever_id))
{
$error = 'ja';
$error_aflever_id = 'ja';
$error_omschr_aflever_id = $lang['error_no_entry'];
}
if(empty($klant_ref))
{
$error = 'ja';
$error_klant_ref = 'ja';
$error_omschr_klant_ref = $lang['error_no_entry'];
}
if(empty($materiaal))
{
$error = 'ja';
$error_materiaal = 'ja';
$error_omschr_materiaal = $lang['error_no_entry'];
}
if(empty($dikte))
{
$error = 'ja';
$error_dikte = 'ja';
$error_omschr_dikte = $lang['error_no_entry'];
}
To this much shorted code:
$check_empty = array($soort, $klant_id, $contact_id, $aflever_id, $klant_ref, $materiaal, $dikte);
foreach ($check_empty as $check)
{
if(empty($check))
{
$error = 'ja';
$error_check = 'ja';
$error_omschr_check = $lang['error_no_entry'];
}
}
The new code is not working. It seems (of course(?)) because if any of the values I want to check if they are empty they are not put in the array since an array can not hold empty values?
How can I make the original code shorter?
You are missing the keys/variable names. Instead of storing the variable values in the array, you could just store the keys/variable name and thus be able to identify the key/variable that is empty:
$check_empty = array('soort', 'klant_id', 'contact_id', 'aflever_id', 'klant_ref', 'materiaal', 'dikte');
$errors = array();
foreach ($check_empty as $name) {
if (empty($$name)) {
$errors[] = array(
'name' => $name,
'desc' => $lang['error_no_entry']
);
}
}
Here $$name
is a variable variable where the value of $name
is used to identify the variable. And as the errors are stored in an array, you can check whether there are errors by check whether $errors
is empty:
if (empty($errors)) {
// no errors
} else {
// errors
}
Then you can even iterate the errors with foreach
.
The short answer is: http://php.net/manual/en/language.variables.variable.php
If this doesn't help I'll update the answer ;)
In your specific case, you would have to work with variable variables:
${"error_".$check} = 'ja';
${"error_omschr_".$check} = $lang['error_no_entry'];
however, variable variables are regarded very bad practice. The variables built this way are impossible to document using phpDoc, for example. Also, your IDE's auto-lookup mechanism will fail for them.
It is much better to use an array:
// Earlier in the code
$error = array();
$error_omschr = array();
// In your loop
$error[$check] = "ja"; // you may want to use booleans instead
$error_omschr[$check] = $lang['error_no_entry'];
// later, for example....
foreach ($error as $key => $value)
{
if ($value == 'ja')
echo "Error: $key<br>";
}
Also, as pointed out by Gumbo and Felix Kling, you need to fix your array definition.
精彩评论