patternphpMinor
Shortening if-statements of displaying errors
Viewed 0 times
statementserrorsshorteningdisplaying
Problem
Is it possible to shorten this piece of PHP code?
Honestly, I think it does not look clean but like a mess.
Honestly, I think it does not look clean but like a mess.
if(Input::exists() && $token) {
if($validation->error('firstname') && $validation->error('lastname')) {
echo str_repeat(' ', 20), '↳ You did not enter your name.', "\n";
} else if($validation->error('firstname')) {
echo str_repeat(' ', 20), '↳ You did not enter your first name', "\n";
} else if($validation->error('lastname')) {
echo str_repeat(' ', 20), '↳ You did not enter your last name.', "\n";
}
}
if(!Input::exists()) {
echo str_repeat(' ', 20), '↳ We will address you by this name.', "\n";
}
if(Input::exists() && $token) {
if($validation->error('emailConfirm') == 'match') {
echo str_repeat(' ', 20), '↳ The email address you entered does not match.', "\n";
} else if($validation->error('emailConfirm') == 'type') {
echo str_repeat(' ', 20), '↳ You did not re-enter a valid email address.', "\n";
} else if($validation->error('emailConfirm')) {
echo str_repeat(' ', 20), '↳ You did not re-enter your email to confirm.', "\n";
}
}else if(!Input::exists()) {
echo str_repeat(' ', 20), '↳ Re-enter your email to make sure it\'s correct.', "\n";
}Solution
You are repeating the same code:
Replace it with a function which will do the job:
You are using construction:
This is the same as:
I guess, that it is not what you want to use, the correct construction should be:
The code itself can be rewritten like this:
echo str_repeat(' ', 20), '↳ You did not enter your name.', "\n";Replace it with a function which will do the job:
function message($content, $class = ''){
return str_repeat(' ', 20) . '$content\n";
}You are using construction:
if () {} else if() {}This is the same as:
if () {} else { if() {} }I guess, that it is not what you want to use, the correct construction should be:
if () {} elseif() {}The code itself can be rewritten like this:
function message($content, $class = ''){
return str_repeat(' ', 20) . '$content\n";
}
if (!Input::exists()){
echo message('We will address you by this name.', 'addition');
echo message('Re-enter your email to make sure it\'s correct.', 'addition');
} elseif (Input::exists() && $token) {
if($validation->error('firstname') && $validation->error('lastname')) {
echo message('You did not enter your name.', 'addition error');
} elseif($validation->error('firstname')) {
echo message('You did not enter your first name', 'addition error');
} else {
echo message('You did not enter your last name.', 'addition error');
}
if($validation->error('emailConfirm') == 'match') {
echo message('The email address you entered does not match.', 'addition error');
} elseif($validation->error('emailConfirm') == 'type') {
echo message('You did not re-enter a valid email address.', 'addition error');
} else {
echo message('You did not re-enter your email to confirm', 'addition error');
}
}Code Snippets
echo str_repeat(' ', 20), '<div class="addition error">↳ You did not enter your name.</div>', "\n";function message($content, $class = ''){
return str_repeat(' ', 20) . '<div' . (($class) ? " class='$class'" : '') . ">$content</div>\n";
}if () {} else if() {}if () {} else { if() {} }if () {} elseif() {}Context
StackExchange Code Review Q#47636, answer score: 6
Revisions (0)
No revisions yet.