settingsAccountsettings
Menusettings

Q: Multiply a Number by 2 in PHP

+2 votes

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:

Parameter name

Input

Output

 

Input

Output

num

2

4

 

3

6

 

asked in PHP category by user Jolie Ann

1 Answer

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

 

answered by user matthew44
selected by user golearnweb
...