douhao2856 2014-10-17 04:51
浏览 39
已采纳

何时可以混合HTML和PHP有什么限制?

I was surprised to find that you can break out of a PHP function into raw HTML and back. I knew that you could do this sort of thing with loops and conditionals, but this was a surprise to me. Is it an accident or is this well-defined behavior? (I couldn't find any explicit discussion of the function case in the manual.)

[NOTE: The following code doesn't give a good example of when I would use this behavior, but I kept it simple for demonstration purposes.]

<?php

$i = 0;

while($i++ < 3) {
    ?><p>I am in a while loop.</p><?php
}

// this part surprised me
function actSkeptical($adjective) {
    ?><p>Is it <?= $adjective ?> that this works?.</p><?php
}

actSkeptical("weird");
?>

Output:

I am in a while loop.

I am in a while loop.

I am in a while loop.

Is it weird that this works?

I know some people absolutely hate mixing PHP and HTML like this, but I can't do OOP/templating (for reasons I won't go into here) and I do like seeing as much raw HTML as possible.

Also, I don't quite understand the semantics of how the short open/close tag above (outputting $adjective) works in conjunction with the surrounding code. Does PHP just treat raw HTML like it was an echo statement? And then the <?= $adjective ?> is just like including a variable within a string?

  • 写回答

2条回答 默认 最新

  • doutan3040 2014-10-17 05:14
    关注

    I can't seem to find any documentation relating to the exiting of PHP tags within blocks. However, there's really only a few places escaping to HTML will work.

    1. Normal Usage

      <?php
      
          php_related_code();
      
      ?>
      
      //html/css/js/etc
      
    2. Within blocks, such as while, for, functions, etc

      <?php
      
      for ($i = 0; $i < 5; $i++) {
          ?>
      
          hello world
      
          <?php
      }
      
      $i = 5;
      while ($i-- > 0) {
          ?> hello there <?php
      }
      
      function myFunc() {
      ?>
          hello universe
      <?php
      }
      
      myFunc();
      

    You can think of ?>stuff<?php similar to an echo or print command found in PHP, because you can escape to HTML in the same places you can echo. So you can echo within the main script, in for loops, and you can echo in functions. But you can't echo in an array, for example:

    <?php
    
    $array = array(echo "here"); //not allowed
    $array = array(?>here<?php); //also not allowed
    

    So you can think of escaping the same as echoing in which it can tell you where you can use it, but you can't do the same thing when you're thinking about what it does.

    They act differently and are processed by PHP differently as well. But your question is only asking about any restrictions so I won't go into what are the differences between ?><?php and echo.

    I forgot to mention, <?=$variable?> is just short tag for <?php echo $variable; ?> if you have this feature enabled.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏