tags:

views:

85

answers:

7

Hello,

I have the following excerpt:

if (empty($last_db_error)) {
    echo "OK";
} else {
    echo "Error activating subscription.";
    echo "{$last_db_error}";
}

The problem is that "{$last_db_error}" is not shown, unless I use just $last_db_error, without the quotes and brackets. Am I missing something here? Isn't the above syntax correct?

+5  A: 

The brackets and the quotes are useless in this case.

if (empty($last_db_error)) {
    echo "OK";
} else {
    echo "Error activating subscription.";
    echo $last_db_error;
}

Will do the job perfectly.

BTW, even if you do can put $vars insides quotes in PHP, this is not recommended because :

  • It works for double quotes only, single quotes will display the var name, which leads to error.
  • It slows down the string parsing.

It's much more appropriate to concatenate variables using the dot operator :

if (empty($last_db_error)) {
    echo "OK";
} else {
    echo "Error activating subscription.\n".
          $last_db_error;
}

And as soon as you have a lot of text to deal with, I urge you to use the PHP alternative syntax. E.G :

<?php if (empty($last_db_error)): ?>
        OK
<?php else : ?>
        Error activating subscription.
        <?php echo $last_db_error; ?>
<?php endif; ?>
e-satis
String interpolation is very appropriate. "Slows down the parsing" and "doesn't work with single quotes" are not valid arguments in my opinion.
Ionuț G. Stan
And, I'm sure the OP knows all that you said. He's actually interested why his code isn't working, because it's valid PHP. I believe ChrisRamakers may have found the problem.
Ionuț G. Stan
@Ionut G. Stan : well, if error prones et performance inefficiency is not valid arguments for you, what is ? But indeed, ChrisRamakers is has a better answer than mine.
e-satis
And by the way, if chris is right, it just add to the fact that it's error prone. Using "." would have raise an error.
e-satis
What is important? Structuring the code well, so that if I somehow have a variable in a string with single quotes, there is a single place that I have to change in order to fix the bug. And, please, let's drop the discussion about optimization of string interpolation. I prefer readable code and some more time optimizing database queries.
Ionuț G. Stan
The fact that vars between double quotes slows down parsing is a myth, yes indeed it take longer to parse this than interpolating but we're talking milliseconds here, perhaps 1 or 2ms slower.If you are experiencing performance problems this really isn't the cause, it's negligible.
ChrisR
@ChrisRamakers : in fact, it used to be much slower in previous versions of PHP, but it's True that in PHP5, recent benchmarks give you 100% right. @Ionut : you think that putting a string INSIDE the quotes increase readability ? PHP is the only language doing that, and there is a reason : it's hard to read. And nobody should ever use vars directly in database query, it goes against all good coding practices. Escaping, prepared statements ? But maybe this is not the place to have this debate. After all, my answer is out of the subject. Should I delete it ?
e-satis
+1  A: 

Just use

echo $last_db_error;

the rest is not needed here.

KB22
A: 

The curly brackets are used to evaluate more complex variable names. If you want the curly brackets in the output, then try escaping the them, like so:

if (empty($last_db_error)) {
    echo "OK";
} else {
    echo "Error activating subscription.";
    echo "\{$last_db_error\}";
}
Marius
uh? Curly brackets in double quoted string are perfectly legal to output variables like this, no need to escape, unless you actually want to print the brackets.
ChrisR
I thought the OP wanted the output to include the curly brackets. Seems he didn't.
Marius
+2  A: 

Is $last_db_error a string or object? If it is a string it should display properly between double quotes using curly braces (like you posted above) so the code seems correct.

Place a var_dump($last_db_error) in the else statement and see what it outputs.

ChrisR
It seems that string interpolation works with user-defined objects that have a `__toString` method. In PHP 5.2.11 at least.
Ionuț G. Stan
A: 

For me it works ok:

<?php
$last_db_error = "LLLLLLLLLLLL";
echo "{$last_db_error}";

It shows me LLLLLLLLLLLL

FractalizeR
A: 

Maybe you are using some sort of templating system which parses everything inside { .... } ?

Anax
A: 

The simpliest way is:

echo $last_db_error;

For some situations try these:

 echo "${last_db_error}";
 echo ${'last_db_error'};

Here is a good article regarding php variable names: curly braces

jerjer