|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2018-02-23 07:58 UTC] damianglinkowski at gmail dot com
Description:
------------
I want to use function inside complex (curly) syntax, for example:
$name = ' Some name ';
echo "Your name is {trim($name)}.";
I expected to see:
Your name is Some name.
but I've got:
Your name is {trim( Some name )}.
So I've added $ before {:
echo "Your name is ${trim($name)}";
and now I've got PHP Notice:
Undefined variable: Some name
It's Ok, there isn't that variable but why php call the method trim?
Test script:
---------------
function foo() {
return 'normal_foo';
}
class Bar
{
public function foo()
{
return 'class_foo';
}
}
$bar = new Bar;
echo foo(), "\n";
echo $bar->foo(), "\n\n";
echo "foo()\n";
echo "$bar->foo()\n\n";
// Bug?
echo "{foo()}\n";
echo "{$bar->foo()}\n\n";
// END Bug?
// Here php calls function, why?
echo "${foo()}\n";
Expected result:
----------------
normal_foo
class_foo
foo()
PHP Notice: Undefined property: Bar::$foo in /home/d0niek/test.php on line 21
()
normal_foo
class_foo
PHP Notice: Undefined variable: normal_foo in /home/d0niek/test.php on line 26
Actual result:
--------------
normal_foo
class_foo
foo()
PHP Notice: Undefined property: Bar::$foo in /home/d0niek/test.php on line 21
()
{foo()}
class_foo
PHP Notice: Undefined variable: normal_foo in /home/d0niek/test.php on line 26
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 02:00:01 2025 UTC |
So I can use object's methods inside {} but I can't use normal functions? What's the difference?Ok, I see.. the syntax is {$...} or ${$...} but I think it should be good to use functions inside {} like {trim(...)}> I think it should be good to use functions inside {} like {trim(...)} That would break a lot of existing code. If you want a way to embed arbitrary expressions in strings then check out the RFC process. https://wiki.php.net/rfc/howto