Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - cant call for function inside double quotes

i have a php code :

class Test {
    function someThing(){ return 1}
}

$test = new Test();

//why this isnt printing bla bla bla 1 ????
echo "bla bla bla $test->someThing()";

but it seems like i cant call function inside a double quoted string

how can i do this ?

thanks

like image 892
The Best Avatar asked Jan 07 '14 10:01

The Best


3 Answers

You can also put a function name inside a variable and then use that variable like a real function inside double quotes string:

$arr = [1,2,3];
$func_inside_var = 'implode';
echo "output: {$func_inside_var($arr)}" . '<br>';

Note that you can even pass paramater to that call.

like image 179
hijack Avatar answered Sep 23 '22 23:09

hijack


you can only call variables inside a string

but if you use {} you can add code to the block

try this :

    echo "bla bla bla {$test->someThing()}";
like image 28
Nimrod007 Avatar answered Sep 26 '22 23:09

Nimrod007


Try this way

class Test {
    function someThing()
    {
        return 1;
    }
}

$test = new Test();

echo 'bla bla bla ' . $test->someThing();
like image 25
sas Avatar answered Sep 25 '22 23:09

sas