settingsAccountsettings
Menusettings

Q: Multiply Two Numbers in PHP

+2 votes

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:

Parameters names

Input

Output

 

 

 

Input

Output

num1

2

6

13

169

num2

3

13   

 

asked in PHP category by user matthew44

1 Answer

+1 vote
 
Best answer
  • 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>

 

answered by user mitko
selected by user golearnweb
...