php - Is there a way to subtract two variables inside an echoed variable?
I am trying to subtract two variables which are in another variable and every time i try there is an error saying "Warning: A non-numeric value encountered". I can do multiplication, addition and even divide the variables but somehow subtraction is giving an error. Below is the code.
$first = 2;
$second = 4;
$answer.='<p>The answer is '.$first - $second.'</p> ';
echo $answer;
Answer
Solution:
Subtract both values into one variable before. You can then echo that one.
$first = 2;
$second = 4;
$result = $first - $second;
$answer.='<p>The answer is '.$result.'</p> ';
echo $answer;
Or put () around the calculation if you want to do it quick and dirty:
$answer.='<p>The answer is '.($first - $second).'</p> ';
Answer
Solution:
echo '<p>The answer is '. ($first - $second) .'</p> ';
//Output "The answer is -2"
Answer
Solution:
Just set parentheses ()
arround your calculation.
<?php
$first = 2;
$second = 4;
$answer.='<p>The answer is '.($first - $second).'</p> ';
echo $answer;
Source