php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75999 Use functions inside complex (curly) syntax
Submitted: 2018-02-23 07:58 UTC Modified: 2018-02-23 08:47 UTC
From: damianglinkowski at gmail dot com Assigned:
Status: Not a bug Package: Strings related
PHP Version: 7.2.2 OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: damianglinkowski at gmail dot com
New email:
PHP Version: OS:

 

 [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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-02-23 08:10 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2018-02-23 08:10 UTC] 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 08:26 UTC] 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:32 UTC] damianglinkowski at gmail dot com
Ok, I see.. the syntax is {$...} or ${$...} but I think it should be good to use functions inside {} like {trim(...)}
 [2018-02-23 08:47 UTC] requinix@php.net
> 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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC