Best php practise display errors -
is there best practice display errors in during controller action executing on view? mean, how display errors when 'login not found in database' or 'input value has incorrect format', or 'field require value'?
is there array errors or else? code looks like:
if ($_server['request_method'] == 'post') { $sitename = isset($_post['sitename']) ? trim($_post['sitename']) : null; $siteurl = isset($_post['siteurl']) ? trim($_post['siteurl']) : null; if(isnulloremptystring($sitename) || isnulloremptystring($siteurl)) { exit('site name or site url not empty'); } elseif (!preg_match('/^http\:\/\/[a-za-z0-9\-\.]+\.[a-za-z]{2,3}(\/\s*)?$/', $siteurl)){ exit('site url has wrong format'); } $filters = array('sitename' => $sitename, 'siteurl' => $siteurl); if($sitesrepository->select($filters)) { exit('site exist'); } }
but if use exit, page doesn't continue display.
if mean want store errors , keep processing, can put them array. before process define array , add each error array. example:
$arrerrors = array();
then replace exit statements with:
$arrerrors[] = 'the error text goes here...';
when you're done processing, can check , output errors:
for ($i=0; $i<count($arrerrors); $i++) { echo $arrerrors[$i].'<br />'; }
or
echo implode('<br />', $arrerrors);
Comments
Post a Comment