doulao3905 2015-11-12 16:14
浏览 181
已采纳

如何在大括号和双引号内插入php函数?

In PHP you can of course insert variables inside of a string (double quoted string). Cool trick. You can also insert array references and object references inside of a string using brackets for protection. Also cool. eg:

echo "my car is $car and my house is {$house[0]} and my boat is {$boat->name}";

How do I insert functions into the array using a similar slick shorthand method? I tried this but it does not work.

echo "the unix time is {mktime()}";

Granted, I know that I can of course display the function by using the period to concat the strings, but am curious to know if there is an obscure shorthand method for doing this? Repeatedly stopping and starting strings with periods and quotes is annoying! sprintf() is closer to what I want, but still more clunky than preferred. I thought the curly brackets might be key...but do they only accept variables? If so, is there a way to represent a function (like mktime()) as a variable (without creating a dummy class)?

  • 写回答

3条回答 默认 最新

  • dshu1235 2015-11-12 16:19
    关注

    You can try this way :

    $mktime = mktime();
    echo "the unix time is {$mktime}";
    

    Also you can concatenate with dots :

    echo "the unix time is {" . mktime() . "}";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭