You're close, you can embed variables in strings, but not function calls.
I use printf()
(and sprintf()
) for that, which is a thin wrapper around the C function of the same name:
printf('My favorite color is %sish -- at least for now.', strtolower( $color ));
See that %s
in there? That's the placeholder for the string data type that you're passing in as the 2nd argument.
sprintf()
works the same way, but it returns the formatted string instead of print'ing it.
The only other options are:
A. Performing the function calls first and assigning the end-result to the variable:
$color = strtolower( $color );
print("My favorite color is {$color}ish -- at least for now.");
B. Using concatenation, which is a little ugly IMO:
print('My favorite color is ' . strtolower( $color ) . 'ish -- at least for now.');
You may have noticed my use of single quotes (aka ticks), and double quotes.
In PHP, literals inside double quotes are parsed for variables, as you see in "A" above.
Literals inside single quotes are not parsed. Because of this, they're faster. You should, as a rule, only use double-quotes around literals when there's a variable to be parsed.