在php中通过循环获取连续输入

发布于 2024-10-08 09:28:54 字数 89 浏览 4 评论 0原文

其实我想写一个程序,不断地接受数字作为输入,并假设输入是42时停止接受输入,然后显示输入的数字。

我尝试了 if 条件,但它只需要一个输入并显示它。

Actually, I want to write a program of taking numbers as input continously and to stop taking input when suppose the input is 42, and then display the input numbers.

I tried the if condition but it takes only one input and displays it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

反话 2024-10-15 09:28:54

显然,如果您使用纯 PHP,循环将无法工作。

我想您可以在使用表单的地方进行一些设置,将操作属性设置为同一页面,并将每个数字保存在全局变量数组中;设置哨兵号码后,在页面重新加载时打印 cookie 中的号码列表而不是表单。

Obviously a loop wouldn't work if you're using plain PHP.

I suppose you could set something up where you use a form, set the action attribute to the same page, and keep each number in a global variable array; when the sentinel number is set, print the list of numbers in the cookie instead of the form when the page reloads.

眸中客 2024-10-15 09:28:54

接受输入&将其存储在数组中,除非它是 42 。要检查使用 if 循环。

eg:
 $num = 'take input value' ;
 if( $num == 42) {
    foreach($numbers_info as $number)
       echo $number;
 }else {
    $numbers_info $numbers_info[] = $num;
 }

take input & store it in array unless it is 42 .to check use if loop.

eg:
 $num = 'take input value' ;
 if( $num == 42) {
    foreach($numbers_info as $number)
       echo $number;
 }else {
    $numbers_info $numbers_info[] = $num;
 }
前事休说 2024-10-15 09:28:54

您可能想尝试编写一个类似这样的类:

class trackNumber{
  private static $instance = null;
  protected $numbers = array();

  private function __construct()
  {
  }

  public static function getInstance()
  {
    if (!self::$instance) {
      self::$instance = new trackNumber();
    }
    return self::$instance;
  }

  public function addNumber($number) {
    if ($number < 42 ) {
      $this->numbers[] = $number;
    }
  }

  public function getNumbers()
  {
    return implode(', ', $this->numbers);
  }
}   

$myNumbers = array(1,3,5,6,8,9);

$trackNumbers = trackNumber::getInstance();

foreach($myNumbers as $number) {
  $trackNumbers->addNumber($number);
}

?>

<?php 
echo $trackNumbers->getNumbers(); 
// result will be: 1, 3, 5, 6, 8, 9  
?>

您可以添加任意数量的检查和函数。由于这是一个单例,您始终可以在任何地方添加或检索数字。

You might want to try writing a class something like this:

class trackNumber{
  private static $instance = null;
  protected $numbers = array();

  private function __construct()
  {
  }

  public static function getInstance()
  {
    if (!self::$instance) {
      self::$instance = new trackNumber();
    }
    return self::$instance;
  }

  public function addNumber($number) {
    if ($number < 42 ) {
      $this->numbers[] = $number;
    }
  }

  public function getNumbers()
  {
    return implode(', ', $this->numbers);
  }
}   

$myNumbers = array(1,3,5,6,8,9);

$trackNumbers = trackNumber::getInstance();

foreach($myNumbers as $number) {
  $trackNumbers->addNumber($number);
}

?>

<?php 
echo $trackNumbers->getNumbers(); 
// result will be: 1, 3, 5, 6, 8, 9  
?>

You can add as much checks and functions as you'd like. Due to this being a singleton you can always add or retrieve the numbers anywhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文