Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Stupid If/Elses are acting up again..... :(
Old 02-12-2010, 07:35 PM Stupid If/Elses are acting up again..... :(
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Hello,

I have a bunch of If/Else statements in my PHP code, but they don't seem to be working. Here's the code:

PHP Code:
$input = array("blue","red","yellow","green","purple","orange","white","black","pink");
$rand_keys array_rand($input2);
$confirmkey $input[$rand_keys[1]] . "\n";

if (
$confirmkey == "blue") {
$soundfile "sounds/red.mp3";
}
else if (
$confirmkey == "red") {
$soundfile "sounds/blue.mp3";
}
else if (
$confirmkey == "yellow") {
$soundfile "sounds/green.mp3";
}
else if (
$confirmkey == "green") {
$soundfile "sounds/yellow.mp3";
}
else if (
$confirmkey == "purple") {
$soundfile "sounds/orange.mp3";
}
else if (
$confirmkey == "orange") {
$soundfile "sounds/purple.mp3";
}
else if (
$confirmkey == "white") {
$soundfile "sounds/black.mp3";
}
else if (
$confirmkey == "black") {
$soundfile "sounds/white.mp3";
}
else if (
$confirmkey == "pink") {
$soundfile "sounds/pink.mp3";
}
else {
$soundfile "none";
}

echo 
$confirmkey;
echo 
"$soundfile"
Now you may be wondering; why the heck would I want to do that? Well, I want to confuse bots. They'll enter the wrong value in the box, and be rejected.

The problem is is that $soundfile is always 'none'. I really don't get why. A simple random function, then a bunch of If/Elses to determine a variable for the outcome.

Why does this happen?

Thanks,

-PG
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-12-2010, 08:31 PM Re: Stupid If/Elses are acting up again..... :(
Novice Talker

Posts: 6
Trades: 0
$rand_keys = array_rand($input, 2)

have you tried changing the 2 to a 1? Not sure if that is it, but I think that number tells it to request that many items from the array.
votter is offline
Reply With Quote
View Public Profile
 
Old 02-12-2010, 08:44 PM Re: Stupid If/Elses are acting up again..... :(
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Actually, I fixed it. Chrishirst can delete this thread now, problem solved .

I can't really remember what I did to fix it, I just played around with it a bit, and I think it turned out to be something completely different. Thanks, though!
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 02-13-2010, 07:38 AM Re: Stupid If/Elses are acting up again..... :(
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
probably using else if rather elseif (no space)

Using the switch statement is better than a lot of elseif statements

PHP Code:
switch($confirmkey) {
    case 
"blue" 
        
$soundfile "sounds/blue.mp3";
        break;
    case 
"red" 
        
$soundfile "sounds/red.mp3";
        break;
    case 
"yellow" 
        
$soundfile "sounds/yellow.mp3";
        break;
    case 
"green" 
        
$soundfile "sounds/green.mp3";
        break;
    case 
"purple" 
        
$soundfile "sounds/purple.mp3";
        break;
    case 
"orange" 
        
$soundfile "sounds/orange.mp3";
        break;

And easier to follow & debug.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-17-2010, 11:58 AM Re: Stupid If/Elses are acting up again..... :(
Skilled Talker

Posts: 80
Name: John
Location: Sacramento
Trades: 0
So glad you brought up the switch.

If the confirmkey matches the filename you could also use:

$soundfile = !empty($confirmkey) ? 'sounds/'.$confirmkey.'.mp3' : 'none';

Or with an array check:

$validkeys = array('blue','red','yellow',...);
$soundfile = !empty($confirmkey) && in_array($confirmkey, $validkeys) ? 'sounds/'.$confirmkey.'.mp3' : 'none';
Envision_frodo is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Stupid If/Elses are acting up again..... :(
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.91486 seconds with 12 queries