How to make PHP reutrn "1 + 2 = 3"? and not just 3
function show_sum($a, $b) {
return "$a + $b = " $a + $b;
}
echo show_sum(1,2);
I have already tried:
return '$a + $b' = " . $a + $b;
I just get 3
How to make PHP reutrn "1 + 2 = 3"? and not just 3
function show_sum($a, $b) {
return "$a + $b = " $a + $b;
}
echo show_sum(1,2);
I have already tried:
return '$a + $b' = " . $a + $b;
I just get 3
You're missing the concatenation operator. Your return statement should be:
return "$a + $b = ". ($a+$b);
Put the result into brackets.
function show_sum($a, $b) {
return "$a + $b = " . ($a + $b);
}
This does work. I think the problem is mixing string values and numeric values. I've made everything a string.
<?php
function show_sum($a, $b) {
$sum = $a + $b;
$return_string = "$a + $b = " . $sum;
return $return_string;
}
echo show_sum(1,2);
?>