defining a array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

defining a array

Post 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 ?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Set error_reporting to E_ALL and see what php has to say about it.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

You can define it after you serialize it, but you will then need to unserialize it again on every use.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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";
 }
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Can you clarify please?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Should be..

Code: Select all

if(defined('magic_array')){
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

ok thanks! ... i forgot :oops:
Post Reply