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 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动