doufud21086 2015-10-23 00:15
浏览 62

如何从一个文本框中读取多个整数并使用php存储在数组中?

Can I use php to read values from one input and store it in array?

I want to read multi integers from the user and calculate the total of them if the user entered ZERO!

For example, I add one input text and one submit:

<form method="GET">
enter numbers : <input type = "text" name="txt">
<input type = "submit" value = "Calculate">

</form>

then use php to read them !!

<?php
$n = @$_GET[txt];  
$i=0;
while ($n!=0)
{

$ary[$i]=$n;
$i++;

}
    if ($n==0)
echo @count($ary);     
    ?>

it doesn't work .. what should I do here? There is the same problem in java, but there I can use scanner. Can any one help to solve it with php?

  • 写回答

1条回答 默认 最新

  • douxiong2999 2015-10-23 12:50
    关注

    I think this is what you're asking for:

    // Get the value of the input box, note that I put `txt` in quotes 
    // so I didn't need the `@`
    $txt = $_GET ['txt'];
    
    // `explode` will create an array from a string using the first 
    // parameter as a delimiter
    $ary = explode (' ', $txt);
    
    // Search the array for the ZERO value
    if (array_search(0, $ary) !== false) {
      // Output the sum of the array if the ZERO value is found
      echo (array_sum ($ary));
    }
    

    A note: You should almost never use the @ operator in PHP, if you do then you're likely doing some incorrect.

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘