doucuodan0897 2017-04-26 12:37
浏览 29
已采纳

PHP变量钩子

i was wondering what the difference is when u put a variable in another variable with the notation. So i have a variable "Body" and in there is HTML tags, text and PHP variables, ive found out i can put them in between hooks but that they would react exactly the same. Example:

In this piece of code the variable is in between { } hooks.

$body = "
             <table style='border: 1px;'>
             <tr>
             <td><b>Naam:</b></td><td>{$naam}</td><br>
             </tr>
             </table>"

And here it is not.

$body = "
             <table style='border: 1px;'>
             <tr>
             <td><b>Naam:</b></td><td>$naam</td><br>
             </tr>
             </table>"

And this both reacts exactly the same. So can anyone tell me if this has an actual use, or that this is just like all PHP with the 10 ways to do the same thing.

Thanks in advance.

Addition

This is not a duplicate ofThis. It does not explain the part of why a variable inside of a variable can be put in between curly brackets.

  • 写回答

1条回答 默认 最新

  • doudonglu3764 2017-04-26 12:50
    关注

    Usage of curly braces inside " is particularly useful when you want to add complex instruction or access object/array properties.

    This code will perfectly work

    <?php echo "Test {$foo['bar']}"; ?>
    <?php echo "Test {$foo->bar}"; ?>
    

    While this one will fail

    <?php echo "Test $foo['bar']"; ?>
    <?php echo "Test $foo->bar"; ?>
    

    So yes, it's not particularly useful if you are accessing a simple variable, but when you want to play with array and object, it's useful.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度