Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在一个页面中实现PHP+MYSQL的更好方法是什么?_Php_Html_Coding Style - Fatal编程技术网

在一个页面中实现PHP+MYSQL的更好方法是什么?

在一个页面中实现PHP+MYSQL的更好方法是什么?,php,html,coding-style,Php,Html,Coding Style,我的做法: <? switch($id) { case 1: ?> a lots of html stuff goes here... <? break; case 2: ?> a lots of html stuff goes here2... <? break; } ?> 有没有办法把这件事做得更漂亮?我是说更具可读性还是什么?我真的很感激你。尚未

我的做法:

<?

switch($id) 
{ 
    case 1:
?>
        a lots of html stuff goes here...
<?    
    break;  
    case 2:
?>
        a lots of html stuff goes here2...
<?    
    break;        
}

?>

有没有办法把这件事做得更漂亮?我是说更具可读性还是什么?我真的很感激你。尚未学习smarty…

将HTML分解为单独的文件,并将其包含在相关块中。

如果此处的语句可能更简洁:

<? 
if ($id == 1) {  
?> 
        a lots of html stuff goes here... 
<?     
}
if ($id == 2) {
?> 
        a lots of html stuff goes here2... 
<?     
} 
?> 

如前所述包括在内。或者,如果你真的想把它分离出来,你可以使用一个模板引擎,比如

,我推荐一个完整的模板解决方案,如Cfreak已经提到的Smarty,将表示与逻辑分离是一个好主意。

Smarty在这种情况下没有帮助。这样的分离应该牢记在心,而不是代码。Smarty也有{PHP}标记……是的,这可能是这里最好的解决方案。我意识到,但是我觉得代码很难看,它允许在表示和逻辑之间进行更多的分离。