Auto calculation inside the variable

[code]$multiplier=2;

$math1=‘3 times ‘.$multiplier.’ is 6’;

echo $math1;[/code]

The code above produces “3 times 2 is 6”.

I like to make the answer “6” auto calculated.
The code below is one of trials for it.

[code]$multiplier=2;

$math1='3 times ‘.$multiplier.’ is '.3*$multiplier;

echo $math1;[/code]The would be code above produes “Parse error: syntax error, unexpected T_DNUMBER”.

How can I make it work?

You are close

$multiplier=2;
$math1='3 times '.$multiplier.' is '.(3*$multiplier);
echo $math1;
2 Likes

That’s new to me!

The reason for the error is the dot concatenation is expecting a string and converts the integer $multiplier to a string but fails with a calculation.

I would have created a new echo followed by the calculation but @cpradio’s method is far better.

Thank you, I have learnt something new which will no doubt be very useful.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.