dongshadu2546 2017-03-10 23:44
浏览 23
已采纳

重复使用网页中的单词/短语

I use a standard document for all my clients, however want to change their business name and address in several places, so to save time copy and pasting, I want to find a way to reuse words and phrases:

One approach to reusing code that I have found is:

    <html>
<body>

<?php
$color = "red";
$second = "blue";
?>

<p>Roses are <?=$color?></p>
<?=$second?>

<p><b>Note:</b> The shortcut syntax only works with the short_open_tag configuration setting enabled.</p>

</body>
</html>

However, is this best practice? or is there a more standard way to use PHP than this?

The source I got this from said: Shortcut syntax (will only work with the short_open_tag configuration setting enabled)

  • 写回答

3条回答 默认 最新

  • douhuxi4145 2017-03-10 23:53
    关注

    Making repetitive phrases into variables is a good idea. If you are using them across multiple pages you can even abstract them to another file and then include it on each page like this:

    config.php

    <?php
    
    $name    = "Business Name";
    $address = "Address";
    

    index.php

    <?php
    
    include config.php;
    
    ?>
    
    <html>
        <body>
            <p><?php echo $name; ?>
        </body>
    </html>
    

    It's a good idea to use full <?php ?> tags rather than <? ?> as this is the short form and is not always configured to work out of the box on each installation of php!

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

报告相同问题?

悬赏问题

  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'