dpquu9206 2014-10-23 08:13 采纳率: 0%
浏览 50
已采纳

PHP脚本标记

Why does this if statement have each of its conditionals wrapped in PHP tags?

  <?php if(!is_null($sel_subject)) { //subject selected? ?>
     <h2><?php echo $sel_subject["menu_name"]; ?></h2>
  <?php } elseif (!is_null($sel_page)) { //page selected? ?>
     <h2><?php echo $sel_page["menu_name"]; ?></h2>
  <?php  } else { // nothing selected ?>
     <h2>Select a subject or a page to edit</h2>
  <?php } ?>  
  • 写回答

5条回答 默认 最新

  • duanfuchi7236 2014-10-23 08:15
    关注

    Because there is html used. Jumping between PHP and HTML is called escaping.

    But I recommend you not to use PHP and HTML like this. May have a look to some template-systems e.g. Smarty or Frameworks with build-in template-systems like e.g. Symfony using twig.

    Sometimes its ok if you have a file with much HTML and need to pass a PHP variable.

    Sample

    <?php $title="sample"; ?>
    <html>
        <title><?php echo $title; ?></title>
        <body>
        </body>
    </html>
    

    This is not much html but a sample how it could look like.

    That sample you provided us should more look like....

    <?php 
        if(!is_null($sel_subject)) 
        {   //subject selected?
            $content = $sel_subject["menu_name"];
        } 
        else if (!is_null($sel_page)) 
        {   //page selected?
            $content = $sel_page["menu_name"];
        } 
        else 
        {   // nothing selected
            $content = "Select a subject or a page to edit";
        }
        echo "<h2>{$content}</h2>";
    ?> 
    

    You could echo each line of course. I prefer to store this in a variable so I can easy prevent the output by editing one line in the end and not each line where I have added a echo.

    According to some comments i did a approvement to the source :)

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

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程