user2662680 user2662680 - 2 months ago 35
PHP Question

How do you insert a php function inside of curly brackets and double quotes?

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)?


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() . "}";