Skip to main content
Inspiring
December 7, 2013
Answered

PHP array/function problem

  • December 7, 2013
  • 1 reply
  • 885 views

I'm having a problem trying to understand why a predefined array is not being echoed/print_r when requested from inside a function.

the array is global(?) by being defined outside the function(s). I can't seem to see why it wont show the array?

It works outside the function

$mondayset=array("setone", "settwo", "setthree", "setfour");

$tuesdayset=array(""setfive", "setsix", setseven");

function dosets ($pubdate)

          {

  $caledardate = explode("/", $pubdate);

  $timestamp = strtotime("$caledardate[2]"."-"."$caledardate[0]"."-"."$caledardate[1]");

  if(date('D', $timestamp) === 'Mon')

     {

          echo "It is Monday today\n";

          print_r($mondayset);


     }

$thedate = 12/2/2013

dosets($thedate)

This topic has been closed for replies.
Correct answer bregent

Variables inside functions have a local scope.

http://php.net/manual/en/language.variables.scope.php

You can pass the array to the function if you want to reference it.

1 reply

bregentCorrect answer
Participating Frequently
December 7, 2013

Variables inside functions have a local scope.

http://php.net/manual/en/language.variables.scope.php

You can pass the array to the function if you want to reference it.

JonnyDLAuthor
Inspiring
December 7, 2013

Wow Ok that link worked. got ot to work thanks!

Participating Frequently
December 7, 2013

Great. You're welcome.