Table of Contents
Exception information:
Stack trace:
Request Parameters:
回复讨论(解决方案)
Home Backend Development PHP Tutorial PHP这种写法是有什么好处?

PHP这种写法是有什么好处?

Jun 23, 2016 pm 02:14 PM

本帖最后由 u010572351 于 2013-06-24 21:21:33 编辑

我发现很多MVC模式的VIEW层(比如有的搜索页面等必须写php代码时)写php代码,都是类似以下写法,感觉很是不理解, 不知道为什么要这样写(以下代码懂,意思不理解)。

比如要循环打印一个水果数组(不能理解为什么要这样写,代码懂,这样做的目的不明白):

<?php foreach($arr as $v){  ?><!--然后是一段html代码--><?php echo $v; ?> :<span>新鲜水果</span><br/><?php  } ?>
Copy after login


好杂乱啊,,,,怎么到处都是php代码,而且有N个php标签,不能像下面一样一次写完吗?
我想的是这样就很好啊(我想像中应该这样,反正都要有php,还不如爽快点):
<?phpforeach($arr as $v){echo $v."新鲜水果<br/>";}
Copy after login


但是很多官方程序都是这样搞的如dede搜索页,zend framework一些页面,还有老师等都是最上面的写法,太不理解了,如果说是为了mvc分离,但是上面感觉没怎么分离啊,反而感觉很 杂乱。。。。求解。。。。




参考以下zend framework的一段if语句的写法就是这种杂乱式的大杂烩:

  <?php if (isset($this->exception)): ?>    <h3 id="Exception-nbsp-information">Exception information:</h3>  <p>      <b>Message:</b> <?php echo $this->exception->getMessage() ?>  </p>  <h3 id="Stack-nbsp-trace">Stack trace:</h3>  <pre class="brush:php;toolbar:false"><?php echo $this->exception->getTraceAsString() ?>  
Copy after login

Request Parameters:

<?php echo $this->escape(var_export($this->request->getParams(), true)) ?>  
Copy after login

回复讨论(解决方案)

这就是php的特点,将程序嵌入到HTML文档中去执行,这样极大的提高了执行效率。

这就是php的特点,将程序嵌入到HTML文档中去执行,这样极大的提高了执行效率。

直接像我一样写一个整体php有什么问题吗?

而且上面的写法为了封尾,还出现了


这样感觉没什么意思的东西,,然后,,,,这种写法的php好像语法有点变异的感觉啊,和正常的php不太一样,不是很理解。。。。

这是鼻祖写法,最早的php就是这样的
好像思想取自 server side include

有什么要理解的,只需记住那样写执行效率高就行了。你自己也知道框架/cms 都采用这种写法,应该就能明白了。

代码混写最大的好处是“方便”,缺点也不言而喻“混乱”
方便与维护性永远是向左右向右走,看你选择哪边而已

你的第一种写法是:在 HTML 中嵌入 PHP
你的第二种写法是:在 PHP 中嵌入 HTML
两者都一样,就看你把谁作为主体了

在HTML中嵌入PHP代码相对于PHP中嵌入HTML代码的在阅读和编写方面的优势一个是可以利用IDE对HTML代码的高亮和代码提示,另一个是不需要对特殊字符进行转义。

维护效率比较低,还是程序与界面分离。

在HTML中嵌入PHP代码相对于PHP中嵌入HTML代码的在阅读和编写方面的优势一个是可以利用IDE对HTML代码的高亮和代码提示,另一个是不需要对特殊字符进行转义。

有点认同

mvc  v里面也要判定的 只是每个角色不一样  

其实你的例子有局限性,如果是foreach包含的是100-200行的html标记字符串的话,你如果用php嵌入html的话,肯定是大段的字符串,很难保证大段的字符串不出现html标签错误,而简单使用html包含php的话,则很容易检测html中的错误。

PHP里echo出html的话你会因为编辑界面时无法对html实施高亮方案(全部用PHP字符串着色方案)而不好维护代码.

其实也没什么乱的,写习惯就好了,就像HTML一样有起始标签就有结束标签,PHP也一样,比如这样写看不习惯就当成HTML语言写好了,个人还是习惯用
感觉看的舒服

你们居然在讨论这么高深的问题。

菜鸟表示围观一下 就可以了

因为那些家伙可能需要用dw之类工具看html代码

混编...........

可能是怕别人改代码吧

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles