An easier way to define these variables?
$access_community = 1;
$access_content = 1;
$access_tools = 1;
$access_administrator = 0;
$access_moderator = 0;
Just wondering if there's an easier way to write this using an array? This seems like overkill.
开发者_如何学运维Thanks!
You could either do something like (sucks for readability):
$access_community = $access_content = $access_tools = 1;
$access_administrator = $access_moderator = 0;
Or as already been said, using an array:
$access = array('community' => 1,
'content' => 1,
'tools' => 1,
'administrator' => 0,
'moderator' => 0);
If you had more variables, then something like:
$access = array( 'community' => 1, 'content' => 1, ... );
foreach ($access as $k => $v) {
$real_name = 'access_' . $k;
$$real_name = $v;
}
Might work, but it's not that nice, and probably even more overkill than your code is now. I think that what you have isn't too bad, to be honest!
If you're interested, this uses indirect references or "variable variables" to actually define new variables. I'm not too sure about scope here, however.
Of course, if you can, try and just use the array directly, rather than relying on the variables being there - arrays can be changed and passed around, unlike variables.
Because you hardly can explain what are you doing and why, we can only guess.
It looks like some ACL.
So, the only sensible way to set these variables is to store it in the database. Especially if there will be dozens of them in the future.
http://phpgacl.sourceforge.net/ is probably what are you looking for
So, your variables will be assigned with values from database.
As for the answer you asked - no, there is no other way to initialize different variables with different values. You have to explicitly set every variable value. So, you have to define a variable name and a variable value somehow. So,
$variable = value
is the most plain and way convenient way.
You can make it more complicated way, but at the core it remains the same: "variable name - variable value" pairs
精彩评论