PHP: Echoing date via german array causes error

Viewed 336

So I´ve got this array, containing the german equivilant word for each month number (without leading zero):

$months= array(1=>"Januar",
               2=>"Februar",
               3=>"März",
               4=>"April",
               5=>"Mai",
               6=>"Juni",
               7=>"Juli",
               8=>"August",
               9=>"September",
               10=>"Oktober",
               11=>"November",
               12=>"Dezember");

Now I´m fetching the month 3 days ago:

$german = date("n", strtotime("-3 days"));

Echoing the value of $german in $months produces an error:

echo $month($german);

Error Message:

Fatal error: Function name must be a string in X on line X

Echoing $german alone doesn´t produce an error.
Echoing $months(date("n", strtotime("some $row value from database"))) doesn´t produce an error.
What´s the problem?
I already tried doing this: echo $months((int)$german), but this didn´t work either.

3 Answers

In your associative array, you need to surround the keys with quotes.

Example:

$months = array("1" => "Januar",
                "2" => "Februar",
                "3" => "März",
                "4" => "April",
                "5" => "Mai",
                "6" => "Juni",
                "7" => "Juli",
                "8" => "August",
                "9" => "September",
                "10" => "Oktober",
                "11" => "November",
                "12" => "Dezember");

Further Thoughts:

Since your keys are numbers, you could forgo an associative array and simply use an indexed array:

$months = array("Neujahr",
                "Januar",
                "Februar",
                "März",
                "April",
                "Mai",
                "Juni",
                "Juli",
                "August",
                "September",
                "Oktober",
                "November",
                "Dezember");

From this indexed array:

  • array_search("Oktober", $months) gives 10.
  • $months[6] gives "Juni"

$months is an array, not an function. When referencing an element in an array, you have to use [] and not () (which is used for calling a function or method).

echo $month[$german];

should work, while $month($german) tries to call a function stored in the variable $month.

echo $month($german);

There are two problems with this piece of code.

The minor one is the variable $month doesn't exist, the array that contains the German names of months is named $months. There is a hint about this problem in the error message too. It says "Function name must be a string". It is not a string, it is undefined.

This problem could be easily caught if you enable reporting all the errors, warnings and notices the interpreter reports when it runs the code. Check your php.ini and make sure the error_reporting line reads:

error_reporting = E_ALL

Having this set, the line echo $month($german); reports the notice as "Notice: Undefined variable: month in file X on line Y" before the "Function name must be string" fatal error.

The second error is the invocation of $months() as a function when it is not a function but an array. The correct usage is:

echo $months[$german];
Related