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

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
shanehunter
Forum Commoner
Posts: 30
Joined: Sun Jun 27, 2010 3:43 pm

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

Post 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!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post 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];
}
(#10850)
shanehunter
Forum Commoner
Posts: 30
Joined: Sun Jun 27, 2010 3:43 pm

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

Post by shanehunter »

awesome!

thank you! =)
Post Reply