Page 1 of 1

defining a array

Posted: Sat Apr 14, 2007 4:10 pm
by tecktalkcm0391
can you use define() to do something like this:

Code: Select all

<?php
$array_to_define = array(array("1","a"),array("2","b"));
define('magic_array',$array_to_define);
?>
Because we i do that and echo magic_array; it just ouputs "magic_array" and not the text Array like normal, or if i do a print_r on it it does the same thing. So is what I doing "legal" in php ?

Posted: Sat Apr 14, 2007 5:12 pm
by volka
Set error_reporting to E_ALL and see what php has to say about it.

Posted: Sat Apr 14, 2007 5:14 pm
by Benjamin
You can define it after you serialize it, but you will then need to unserialize it again on every use.

Posted: Sat Apr 14, 2007 5:25 pm
by tecktalkcm0391
thanks, i can serialize it and get it to define, but when I try to get it back to an array i get not defined, but i can ouput the serialized defined code before this.... heres my code:

Code: Select all

if(defined(columns_to_get)){ 
$output = unserialize(columns_to_get);
print_r( $output);
 } else {
 	echo "not defined";
 }

Posted: Sat Apr 14, 2007 7:09 pm
by Benjamin
Can you clarify please?

Posted: Sat Apr 14, 2007 10:31 pm
by tecktalkcm0391
astions wrote:Can you clarify please?
Yes,
Sorry I didn't post the whole code so here it is:

Code: Select all

<?php
// On an included page:
$array_to_define = array(array("1","a"),array("2","b"));
$array_to_define = serialize($array_to_define);
define('magic_array',$array_to_define);
// No error messages or any problems so far 


// On page that included the above... (YES the page is included and I am sure of it)
if(defined(magic_array)){
      $output = unserialize(magic_array);
      print_r( $output);
 } else {
        echo "not defined";
 } 

?>
I get back "not defined" not the print_r of the array.... but if i remove the rest of the second part code expect for the unserialize and print_r it works fine.... why?

Posted: Sat Apr 14, 2007 10:35 pm
by Benjamin
Should be..

Code: Select all

if(defined('magic_array')){

Posted: Sat Apr 14, 2007 10:41 pm
by tecktalkcm0391
ok thanks! ... i forgot :oops: