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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?