Jump to content

Array's default value


Dat

Recommended Posts

How can I get $TEMPLATE's default value as true;

 

So that later when I create $TEMPLATE[uSER_CAN_EDIT] = false;

 

I can use that later with

if (TEMPLATE[uSER_CAN_EDIT] == true)
{
//Something
}
else if (TEMPLATE[uSER_CAN_EDIT] == false)
{
//Something else
}

 

I want all of the array's of $TEMPLATE's value to be true as default; so when if i were to change them later I can do what is above.

Link to comment
https://forums.phpfreaks.com/topic/115641-arrays-default-value/
Share on other sites

In short, you can't do that. What you could do is check to see if a particular key has been set. Indeed, i'd reverse your logic. It would make more sense from a safety point of view to ensure that each option is false to start with. It should only be set to true if the required authorization is met. Doing it your way leaves you open to making a mistake allowing access to something you did not wish. So, i'd do this:

 

if(isset($TEMPLATE['USER_CAN_EDIT'])){
//allow editing
}else{
//don't allow
}

 

You would then allow editing just be adding the key to the array:

 

//conditions have been met to allow editing
$TEMPLATE['USER_CAN_EDIT'] = TRUE;//the actual value assigned would be irrelevant

 

Note that you would only add the key if you want to allow it. You wouldn't, for example, set $TEMPLATE['USER_CAN_EDIT'] to false to prevent editing, you would just not set it at all.

Link to comment
https://forums.phpfreaks.com/topic/115641-arrays-default-value/#findComment-594476
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.