views:

58

answers:

3
$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!

A: 

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.

Lucas Jones
OK, it's not a very neat solution! :) It might be the only one if the OP really needs the variables to be defined like that, e.g. for interfacing with external code.
Lucas Jones
Heh, just looking for ideas :). In the future I could possibly have about 30 or 40 of these lines, so was thinking there must be an easier way :).
Sam
@Sam: In that case, I'd recommend going with the second solution in faileN's answer.
Lucas Jones
every time you are using a variable variable, you know for sure you are doing wrong
Col. Shrapnel
@Col. Shrapnel: `goto` and `eval` have their uses as well.
Lucas Jones
yes, most of time you can say the same of these two too. especially if you are answering to the newbie programmers question.
Col. Shrapnel
@Col. Shrapnel: I see your point. :) My answer isn't a very good one, but indirect references aren't *always* bad.
Lucas Jones
+6  A: 

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);
faileN
Then I can access those using echo $access_community (will echo 1?)?
Sam
with the first solution yes. with the array-solution you'll have to do `echo $access['community']`
faileN
Awesome, thanks for that. Thanks to all other answers too :).
Sam
while second one is sensible, the first one is terrible. these settings are going to be different on different pages for sure. And it will be hard as hell to get a picture of the page access rights looking into these lines.
Col. Shrapnel
That's why I said, that it sucks for readability. But it is a solution, otherwise it wouldn't have been built in php. I would also rather use the second one.
faileN
+1  A: 

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

Col. Shrapnel