4

I'm running PHP 5.3.0. I've found that the curly string syntax only works when the first character of the expression is $. Is there a way to include other types of expressions (function calls, etc)?

Trivial example:

<?php
$x = '05';
echo "{$x}"; // works as expected
echo "{intval($x)}"; // hoped for "5", got "{intval(05)}"
1
  • 2
    yes. but why? to produce delicious spaghetti? Commented May 6, 2010 at 16:22

5 Answers 5

3
<?php
$x = '05';
echo "{$x}";
$a = 'intval';
echo "{$a($x)}";
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Clever... I'd use that if I wasn't allergic to the smell :)
2

No. Only variables of various forms can be substituted using variable substitution.

Comments

2

take a look at this link LINK

Example of the code,

Similarly, you can also have an array index or an object property parsed. With array indices, the closing square bracket (]) marks the end of the index. For object properties the same rules apply as to simple variables, though with object properties there doesn't exist a trick like the one with variables.

<?php
// These examples are specific to using arrays inside of strings.
// When outside of a string, always quote your array string keys 
// and do not use {braces} when outside of strings either.

// Let's show all errors
error_reporting(E_ALL);

$fruits = array('strawberry' => 'red', 'banana' => 'yellow');

// Works but note that this works differently outside string-quotes
echo "A banana is $fruits[banana].";

// Works
echo "A banana is {$fruits['banana']}.";

// Works but PHP looks for a constant named banana first
// as described below.
echo "A banana is {$fruits[banana]}.";

// Won't work, use braces.  This results in a parse error.
echo "A banana is $fruits['banana'].";

// Works
echo "A banana is " . $fruits['banana'] . ".";

// Works
echo "This square is $square->width meters broad.";

// Won't work. For a solution, see the complex syntax.
echo "This square is $square->width00 centimeters broad.";
?>

there are different things you can achieve with the curly brace, but it is limited, depending on how you use it.

Comments

0
<?php
class Foo
{
    public function __construct() {
        $this->{chr(8)} = "Hello World!";
    }
}

var_dump(new Foo());

Comments

0

Generally you don't need the braces around variables, unless you need to force PHP to treat something as a variable, where its normal parsing rules otherwise might not. The big one is multidimensional arrays. PHP's parser is non-greedy for deciding what's a variable and what isn't, so the braces are necessary to force PHP to see the rest of the array element references:

<?php

$arr = array(
    'a' => array(
         'b' => 'c'
    ), 
);

print("$arr[a][b]"); // outputs:  Array[b]
print("{$arr[a][b]}"); // outputs: (nothing), there's no constants 'a' or 'b' defined
print("{$arr['a']['b']}"); // ouputs: c

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.