功能的PHP for循环

功能的PHP for循环

问题描述:

虽然我练了PHP我想打一个函数在PHP for功能的PHP for循环

function Yearsbox(,) { 
    for ($i=2000; $i < 2020 ; $i++) { 
     echo $i; 
    } 
} 
echo Yearsbox; 

有什么不对?

+0

请再次阅读功能手册 - HTTP:// PHP .net/manual/en/functions.user-defined.php –

+0

'Yearsbox(,)'是错误的,它是无意义的,而且无效的语法 –

+0

这个逗号在param列表中是什么意思? – halfer

您可能不会将一个逗号作为该函数的参数。请给你的参数一个有效的名字或者把括号留空。

其次,你打电话与支架功能(忘了我以前写的,你必须添加它们,否则PHP解释为常数)

这是应该工作:

function Yearsbox() { 
    for ($i=2000; $i < 2020 ; $i++) { 
     echo $i; 
    } 
} 
Yearsbox(); 
+0

为什么要低估我?不要downvote w/o告诉为什么... –

首先你必须定义这个函数,如果你想从中获取数据,就让它返回一些数据。然后,你调用这个函数。你的情况:

<?php 
    function Yearsbox() { 
     for ($i=2000; $i < 2020 ; $i++) { 
      echo $i; 
     } 
    } 

    Yearsbox(); 

OR:

<?php 

    function Yearsbox() { 
     $output = ""; 
     for ($i=2000; $i < 2020 ; $i++) { 
      $output .=$i; 
     } 
     return $output; 
    } 

    echo Yearsbox(); 

或者你也可以使用两种可选的参数组合:

<?php 

    function Yearsbox($echo=false) { 
     $output = ""; 
     for ($i=2000; $i < 2020 ; $i++) { 
      $output .=$i; 
     } 
     if($echo){ 
      echo $output; 
      return null; 
     } 
     return $output; 
    } 

    echo Yearsbox(); 
+0

请不要使用报价块作为一般的荧光笔 - 使用这些当你引用外部来源请。另外,请不要使用全部大写 - 它被广泛解释为喊叫。 – halfer

+0

@halfer记住这一点...谢谢 – Poiz