Run and print the result php code

I need help to know if what Im doing is correct. I want to print the results as for example
3+4 = 7
but when I run the code I get 3 add 4 = 7.
I dont know if im using correctly the syntax echo "

The result is $num1 $operation $num2 = $result

";

<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>Calculation Result</title>
</head>
<body>
<?php # handle_form.php

// Create a shorthand for the form data:
//retrieve the data by using $_REQUEST and name of the textbox

$num1 = $_REQUEST['number1'];
$num2 = $_REQUEST['number2'];
$operation = $_REQUEST['operation'];


//
//you code starts from here

//validation of the input using is_numeric() function

if(is_numeric($num1))
{
    $num1 = (int)$num1; // int number1
}
if(is_numeric($num2))
{
    $num2 = (int)$num2; // int number2
}


if ($operation=='add') {
	$result = $num1 + $num2;
	
	
}
elseif ($operation=='substract') {
 	$result = $num1 - $num2;
	
}
elseif ($operation=='multiple') {
 	$result = $num1 * $num2;
	
	
}
elseif ($operation=='divide') {
	$result = $num1 / $num2;
}

// Print the submitted information:
echo "<p> The result is $num1 $operation $num2 = $result <p>";

?>
</body>
</html>

Its working as expected.

You have already declared your ‘operation’ value… (add, subtract…etc) so when you use it… that is the exact value you will see when echo’d/output.

If you want to see a valid operator… you will need to declare one to be used in the visual output:

        $num1 = 3;
		$num2 = 4;
		$operation = 'add';
		$operator = '';

		//validation of the input using is_numeric() function

		if(is_numeric($num1)){
			echo '1 is numeic <br>';
			$num1 = (int)$num1; // int number1
		}
		
		if(is_numeric($num2)){
			echo '2 is numeic <br>';
			$num2 = (int)$num2; // int number2
		}

		if ($operation=='add') {
			$result = $num1 + $num2;
			$operator = '+';		
			
		}else if ($operation=='substract') {
			$result = $num1 - $num2;	
			$operator = '-';
			
		}else if ($operation=='multiple') {
			$result = $num1 * $num2;
			$operator = 'x';
			
		}else if ($operation=='divide') {
			$result = $num1 / $num2;
			$operator = '/';
		}

		// Print the submitted information:
		//echo "<p> The result is $num1 $operation $num2 = $result <p>";
		echo "<p> The result is $num1 $operator $num2 = $result <p>";

Sponsor our Newsletter | Privacy Policy | Terms of Service