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.

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

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?