用于循环的函数[关闭]

While I am practicing for PHP I want to make a function with PHP for loop

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

What is wrong?

You may not give a single comma as an argument to the function. Either give your parameter a valid name or leave the brackets empty.

Second, you call a function with brackets (forget what i wrote before, you have to add them, otherwise php interprets it as a constant)

This is what should work:

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

First you have to define the function and if you want to get data from it, make it return some data. Then, you call the function. In your case:

<?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();

Or you can combine both using an optional argument:

<?php

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

    echo Yearsbox();