Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

PHP array/function problem

Contributor ,
Dec 07, 2013 Dec 07, 2013

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)

TOPICS
Server side applications
892
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Dec 07, 2013 Dec 07, 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.

Translate
LEGEND ,
Dec 07, 2013 Dec 07, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 07, 2013 Dec 07, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 07, 2013 Dec 07, 2013
LATEST

Great. You're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines