If I use the curly bracket syntax (referred to as the
complex syntax) within a string, how do I get to call a
function within it?
The php manual says that the first (or previous)
character for the curly bracket has to be a dollar sign '$'.
This is fine for variables, arrays and some objects but
doesn't allow me to call a function such as addslashes() or
trim() before I return the string in the variable.
I have tried using assignment, concatenation and even
methods of an instantiated object and have found the parser
refuses to allow the equal '=' sign, fullstop '.', or even a
standard left bracket '(' within the complex curly brackets.
For example, I had expected the following to work:
$x = "";
$string = <<<EOT
This is a text string ...
.... {$x . addslashes(trim($myinput1))} ...
.... {$x . addslashes(trim($myinput2))} ...
more text etc .....
EOT;
It doesn't work, the parser objects to the full stop symbol '.'
Worse still, the following was objected to
$string = "This is text .. {$x->myGetMethod($specifier)} ..
more text etc ";
The refusal to accept a left bracket here means that it is
not a real object oriented language as you can't always get
to objects within complex syntax!
Considering that they have called this the complex syntax, I
think that it is very poorly implemented. My expectation
was that any legal PHP expression should be able to be used
inside the braces so long as it resulted in a string. It is
far from that!
Strangely, I have read (but haven't tried) that the
following expression works:
$string = "This is text {${$result ? 'var1' : 'var2'}} ..
more text etc ";
Can anybody please tell me some way to use a function within
the curly bracket (complex) syntax? (without a discussion
on faults of Heredoc or doing it without the complex syntax)
Thanks
Ken