使用用户输入创建 PHP 乘法表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21892577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:30:52  来源:igfitidea点击:

Creating PHP multiplication table with user input

phphtml

提问by user2975266

I am creating form where the user can input a integer and get the multiplications (1-10) of that number. Here is my web form for user input:

我正在创建表单,用户可以在其中输入一个整数并获得该数字的乘法 (1-10)。这是我用于用户输入的 Web 表单:

<html>
<head>
    <title>Assignment 9.1</title>
</head>
<body bgcolor="black" text="white">
    <form method="post" action="table.php"
        <strong>Enter No:</strong>
        <input type="text" name="num" size="10">
        <input type="submit" value="Get Table">
    </form>
</body>
</html> 

The table I have created is this:

我创建的表是这样的:

<?php
$num = $_POST['num'];
if($num)
(
    for ($i=1; $i<=10; $i++)
    (
        $mul = $num * $i;
        echo "$num * $i = $mul<br>";
    )
)
else
(
    echo "Invalid Entry!";
)
?> 

I am getting an error for the table. The error is for line 5 (FOR). I have no idea why I am getting this error. Can anyone Help?

我收到表格错误。错误是针对第 5 行 (FOR)。我不知道为什么会收到此错误。任何人都可以帮忙吗?

回答by Niet the Dark Absol

Blocks of code are marked with curly brackets { ... }, you are currently using parentheses ( ... )

代码块用大括号标记{ ... },您当前使用的是圆括号( ... )

As far as I can tell, that's pretty much the only thing wrong with it. You might want to add a bit of validation:

据我所知,这几乎是它唯一的错误。您可能想要添加一些验证:

$num = isset($_POST['num']) ? intval($_POST['num']) : 0;

But that'd just a touch-up. The brackets are your actual problem.

但这只是一个修饰。括号是您的实际问题。

回答by Fabian Rivera

You need to use {} instead () in the forand if.

您需要在forif 中使用 {} 代替 () 。

Like this:

像这样:

<?php
$num = $_POST['num'];
if($num)
{
    for ($i=1; $i<=10; $i++)
    {
        $mul = $num * $i;
        echo "$num * $i = $mul<br>";
    }
}
else
{
    echo "Invalid Entry!";
}
?> 

Hope it helps you!

希望对你有帮助!

回答by user3329684

You are using parentheses when you should be using braces. Try this...

当您应该使用大括号时,您正在使用括号。尝试这个...

<?php
$num = $_POST['num'];
if($num)
{
    for ($i=1; $i<=10; $i++)
    {
        $mul = $num * $i;
        echo "$num * $i = $mul<br>";
    }
}
else
{
    echo "Invalid Entry!";
}
?> 

回答by Ashish Verma

save the file as table.php and run working 100%
 <html>
<head><title>Table</title></head>
<body>
<form action="" method="POST">
<center><input type="text" name="number" size="20" > </center>    <br>
<center><input type="submit" name="table" value="get table"> </center>
</form>
</body>
</html>

<?php
$num=$_POST['number'];
if($num<=20)
{
  for ($i=1; $i<=10; $i++)
  {
    $mul=$num*$i;
    echo "<center>$mul </center><br>";
   }
}
  else
  {
  echo "<center>envalid entry</center>";
   }


?>



<html>

<head><title>Table</title></head>

<body>

<form action="" method="POST">

<center><input type="text" name="number" size="20" > </center>    <br>

<center><input type="submit" name="table" value="get table"> </center>

</form>

</body>

</html>

<?php

$num=$_POST['number'];

if($num<=20)

{

  for ($i=1; $i<=10; $i++)

  {

    $mul=$num*$i;

    echo "<center>$mul <br></center>";

   }

}

  else

  {

  echo "<center>envalid entry</center>";

   }

?>