[Solved] Multiply Two Numbers in PHP

  

3
Topic starter

You are given a number num1 and a number num2. Write a PHP script that multiplies num1 * num2 and prints the result. The input comes as parameters named num1 and num2. Print the output in the HTML page.

Examples:

multphonum
1 Answer
2
  • The form will be with 2 input elements, with names num1 and num2
  • You must check both elements, if they have values before we perform any action
  • When we have checked both elements we get them both and extract their values into variables and we perform the specified action:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
<form>
    N: <input type="text" name="num1"/>
    M: <input type="text" name="num2"/>
    <input type="submit"/>
</form>
<?php
if (isset($_GET['num1']) && isset($_GET['num2'])) {
    $num1 = intval($_GET['num1']);
    $num2 = intval($_GET['num2']);
 
    echo $num1 * $num2;
}
 
?>
</body>
</html>
Share: