Bug #75999 [Nab]: Use functions inside complex (curly) syntax

From: Date: Fri, 23 Feb 2018 08:32:24 +0000
Subject: Bug #75999 [Nab]: Use functions inside complex (curly) syntax
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-214079@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75999&edit=1 ID: 75999 User updated by: damianglinkowski at gmail dot com Reported by: damianglinkowski at gmail dot com Summary: Use functions inside complex (curly) syntax Status: Not a bug Type: Bug Package: Strings related PHP Version: 7.2.2 Block user comment: N Private report: N New Comment: Ok, I see.. the syntax is {$...} or ${$...} but I think it should be good to use functions inside {} like {trim(...)} Previous Comments: ------------------------------------------------------------------------ [2018-02-23 08:26:28] damianglinkowski at gmail dot com So I can use object's methods inside {} but I can't use normal functions? What's the difference? ------------------------------------------------------------------------ [2018-02-23 08:10:15] requinix@php.net Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php Complex syntax uses both a $ and {}s. Braces alone are not enough. http://php.net/manual/en/language.types.string.php#language.types.string.parsing.complex ------------------------------------------------------------------------ [2018-02-23 07:58:05] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=75999&edit=1

« previous php.bugs (#214079) next »