well, the ideal is to abstract out the way you deal with the code, and thereby avoid deeply nested blocks like this. for example (paraphrased from a current project of mine):
PHP Code:
if(!empty($_GET['page'])){
if(preg_match('/^[A-Za-z0-9]+$/',$_GET['page'])){
$result = mysql_query("SELECT * FROM `pages` WHERE `page`='{$_GET['page']}' LIMIT 1");
if(mysql_num_rows($result) === 1){
/* do stuff with results */
}else{ /* error: page not found */ }
}else{ /* error: page request not valid */ }
}else{ /* error: no page requested */ }
this gets difficult to deal with very quickly. It can be abstracted out something like this:
PHP Code:
class page{
public function __construct(){
if(!empty($_GET['page'])){ $this->checkPage($_GET['page']); }
else{ $this->noPageRequested(); }
}
public function checkPage($page){
if(preg_match('/^[A-Za-z0-9]+$/',$page)){ $this->findPage($page); }
else{ $this->invalidPageRequest($page); }
}
public function findPage($page){
$result = mysql_query("SELECT * FROM `pages` WHERE `page`='{$_GET['page']}' LIMIT 1");
if(mysql_num_rows($result) === 1){ $this->doStuffWithResults($result); }
else{ $this->pageNotFound($page); }
}
public function doStuffWithResults($results){
/* do stuff with results */
}
/* other functions, etc. */
}
this is an over-simplified example, but i hope it shows how it can make your code far cleaner, more organized, and manageable.
Bookmarks