Page 1 of 1

is $this = 'that' then $that = 'this'... How do I?

Posted: Mon Jul 12, 2010 11:33 am
by shanehunter
Hey Guys,

I have a 'spinning' code where each time a script runs - $user = a random username (from a list of about 5)

what I need, is to know how to get something along the lines of:

IF $user = 'username1'

THEN

$password = 'password1'

IF $user = 'username2'

THEN

$password = 'password2'

etc. Thanks for your help!

Re: is $this = 'that' then $that = 'this'... How do I?

Posted: Mon Jul 12, 2010 11:40 am
by Christopher
You can do it the long way like you showed:

Code: Select all

if ($user == 'username1') {
     $password = 'password1';
} elseif ($user == 'username2') {
     $password = 'password2';
}
However, it might be easier to use an array to lookup the values. Then you don't have to keep adding code, just more username/password pairs to the data:

Code: Select all

$passwords = array(
     'username1' => 'password1',
     'username2' => 'password2',
     );
if (isset($passwords[$user])) {
     $password = $passwords[$user];
}

Re: is $this = 'that' then $that = 'this'... How do I?

Posted: Mon Jul 12, 2010 11:55 am
by shanehunter
awesome!

thank you! =)