Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将for循环的内容输出到数组中 ($numbers1=1;$numbers1_Php_Arrays_For Loop - Fatal编程技术网

Php 将for循环的内容输出到数组中 ($numbers1=1;$numbers1

Php 将for循环的内容输出到数组中 ($numbers1=1;$numbers1,php,arrays,for-loop,Php,Arrays,For Loop,/* *使用空数组初始化变量。 *数组可以包含任何类型的变量,甚至是其他数组,并且大小会自动增长。 *在PHP5.4及更新版本中,可以使用`$data=[];`语法 */ $data=array(); /* *将变量$numbers1设置为1,然后检查其值是否小于或等于150,然后在{和}之间运行代码 *在`{}`块的最后一行之后,执行变量增量(++$numbers1)-将1添加到上一个值。 *现在$numbers1变为等于2,然后再次检查其值是否小于或等于150。 *循环将反复迭代,直到$nu

/*
*使用空数组初始化变量。
*数组可以包含任何类型的变量,甚至是其他数组,并且大小会自动增长。
*在PHP5.4及更新版本中,可以使用`$data=[];`语法
*/
$data=array();
/*
*将变量$numbers1设置为1,然后检查其值是否小于或等于150,然后在{和}之间运行代码
*在`{}`块的最后一行之后,执行变量增量(++$numbers1)-将1添加到上一个值。
*现在$numbers1变为等于2,然后再次检查其值是否小于或等于150。
*循环将反复迭代,直到$numbers1变为等于151和`$numbers1
演示!


如果我读对了,for语句实际上有点像一个函数,它甚至在检查语句后运行括号中的内容?也就是说,您实际上只是用一个数组函数替换了我对数字的回显,命名为它"$numbers\u数组,然后用$numbers1占用它。因此,循环的每一轮,数组都会添加另一个结果,直到循环停止?哇,你必须以教书为生。我将把它保存为参考资料。但是,1个问题,如果我在echo语句之前插入Print\r,它会同时显示Print\r和echo,还是会显示Print\r在数组输出时对其进行解析?您会如何称呼?您没有像我预期的那样使用“数组推送”。@DanMan3395,
print\r echo$array;
将产生解析错误。
echo
print
print\r
不会修改它们的参数。
$array[]='val
与array_push有点不同,但对于这个代码示例,它们是等效的。
for ($numbers1 = 1; $numbers1 <= 150; ++$numbers1)
  {
       echo "the number " . $numbers1;
       echo "<br>";
  }
$numbers_array = array();

for ($numbers1 = 1; $numbers1 <= 150; ++$numbers1)
  {
       array_push($numbers_array,$numbers1);//add the number to the array
  }

print_r($numbers_array);//print out the array
var_dump($numbers_array[$numbers1-1]);//prints out the element in the position that is one less then the number you want to print out (since arrays in php are zero based)
/*
 * Initilalize variable with an empty array.
 * Array can contain variables of any type, even other arrays and grows in size automatically.
 * In PHP 5.4 and newer you can use `$data = [];` syntax
 */
$data = array();

/*
 * Set variable $numbers1 to 1, then check that its value less than or equal to 150 and then run code between { and }
 * After the very last line in the `{ }` block perform variable increment (++$numbers1) - add 1 to the previous value.
 * Now $numbers1 becomes equal to 2, then check that its value less than or equal to 150 and again.
 * Loop will iterate over and over untill $numbers1 become eqial to 151 and `$numbers1 <= 150` evaluates to false.
 * Then loop breaks and code after closind `}` will be executed.
 */
for ($numbers1 = 1; $numbers1 <= 150; ++$numbers1)
{
    /*
     * Set variable $value to the string. String will be built from 3 parts 
     *  - "the number ", 
     *  - string representation of $numbers1 value and
     *  - literal string "<br/>\n"
     *
     * "\n" means 'new line' symbol, in HTML is transforms into the space, but it will help you to debug your application -
     * you can see resulting code more structured in "view-source" mode of your browser (Ctrl+U in Firefox).
     * You can safely remove it.
     */
    $value = "the number " . $numbers1 . "<br/>\n";

    /**
     * Then add $value to the array. It is equivalent to array_push($data, $value)
     * String 'the number 1<br/>' becomes the first element (at 0 index, try to `echo $data[0];` somewhere),
     * string 'the number 2<br/>' becomes the second one ($data[1]) and so on.
     */
    $data[] = $value;
}

/* Tt is good habit to unset variables that you don not need anymore.
 * Try to `echo $value` and you will got Notice - variable does not exist.
 */
unset ($value, $numbers1);

/*
 * For page output you should use some kind of templating engine (Twig, for example). For now, we will use PHP itself
 */

// After this line starts plain HTML, PHP engine will output is as is. Like web server usially does.
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Demo!</title>
    </head>
    <body>
        <p>
            <?php 
                /*
                 * Meet PHP code again.
                 * All that printed or echoed will be put in place of the code between open and closing php tags
                 */
                foreach ($data as $value) {
                    echo $value;
                }
            ?>
        </p>
    </body>
</html>