[Solved] Multiply a Number by 2 in PHP

  

3
Topic starter

You are given a number num. Write a PHP script that multiplies the number by 2 and prints the result. The input comes as a parameter named num.

Examples:

php multiply
1 Answer
2
  • In case the form was submitted and the input parameter num exists, take its value as integer using the function intval(string).
  • Then, just print the results: echo $n * 2.

The code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>First Steps Into PHP</title>
 
</head>
<body>
<form>
    N: <input type="text" name="num"/>
    <input type="submit"/>
</form>
 
<?php
if (isset($_GET['num'])) {
    $n = intval($_GET['num']);
    echo $n * 2;
}
?>
</body>
</html>
Share: