Now you mix me up
$str = eval("A.$b.can.$d.");
*is* identical to the 2 statements:
$val = "A.$b.can.$d.";
$str = eval($val); // string to evaluate is a.baby.can.dance. => Syntax Error
And for the example of your dancing baby both return it without dots.
As I've written: this includes a syntax error, and to correct it you must write:
$val = """A.$b.can.$d.""";
$str = eval($val); // string to evaluate is "a.baby.can.dance."
or identical:
$str = eval("""A.$b.can.$d.""");
Maybe, to understand a bit more what happens, step through the script, and see what happens with the variables and values. It shows that everything is rather correct here.
OK: we might try to guess why the eval() is here. In form of the example it just deletes the inner double-quotes, so it's *identical* to
$str = "A.$b.can.$d.";
I don't think you just want to look it a bit more complicated.
But if I would understand I would replace a pair of double quotes wiht a single single-quote:
$str = eval('"A.$b.can.$d."');
*or*
$str = eval("'A.$b.can.$d.'");
In *this* situation the results are identical. But on stepping you will see that the intermediate result is different:
- for 1st case the string to eval still contains the variable names, and eval() replaces them with their values,
- for 2nd case variables are resolved *before* eval() is called.
So there *can* be situations where the result is different. (Else you wouldn't have to use eval() at all.)