简体   繁体   中英

Not being able to access global array from in php

i am having the following situation in a wordpress theme which I want to modify in a file called navigation.php

$array_test = array (1,2,3,4);

function func1()
{
  global $array_test;

  echo "test"; // to ensure that the function has been called
  echo $array_test[2]; // this writes nothing, though the function is being called
}

in the file called header.php the function func1 is being called, still the value of $array_test[2] cannot be accessed,

Do you have any ideas?

EDIT: i suspect it is an issue with wordpress or the theme, but i am not sure the theme is the free health_care_wp_theme

In navigation.php, (in case if there's no class in that file,define one)

class Nav
{ 
     public $array_test = array(1,2,3,4);

     public function func1()
     {  
       echo "test"; // to ensure that the function has been called
       return $this->array_test[2]; 
     }

     public function access_within_class()
     {   
       print_r($this->array_test); 
     }     
}

In header.php

include 'path/to/navigation.php';

$nav = new Nav();
$the_array = $nav->func1(); 
echo $the_array;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM