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
If Statement - || and && fails.
Old 03-15-2009, 01:25 PM If Statement - || and && fails.
Average Talker

Posts: 15
Name: Declan Dowling
Trades: 0
Hello,

I'm making a slot machine script, and I've done the simple part of seeing if each random spin matches eachother.

PHP Code:
if($spin_1 == and $spin_2 == and $spin_3 == 1){
$win_amount 3;

But now I want to see if $spin 1, 2 and 3 are equal to either 1, 2 or 3.
and set the win amount.

I have tried many things, including this:
PHP Code:
if ($spin_1 == || $spin_1 == || $spin_1 == && $spin_2.. ect
Anybody help?
Declan is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-15-2009, 01:29 PM Re: If Statement - || and && fails.
Average Talker

Posts: 15
Name: Declan Dowling
Trades: 0
Lol, problem solved by a friend:
PHP Code:
$winnums = array(1,2,3);
    if(
in_array($spin_1,$winnums) == true && in_array($spin_2,$winnums) == true && in_array($spin_3,$winnums) == true) { $win_amount 500; } 
Declan is offline
Reply With Quote
View Public Profile
 
Old 03-16-2009, 05:25 PM Re: If Statement - || and && fails.
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Just a thought...

As you probably already know the in_array() function will return a boolean value, so it can be used as a expression itself, instead of actually checking if it equals true. Of course, either way works just as good, but it may be a bit simpler to read this way.

PHP Code:
if (in_array($spin_1$winnums) && in_array($spin_2$winnums) && in_array($spin_3$winnums)) {
   
// ...

And presuming that zero and negative numbers doesn't have a special meaning here, you could also just check if they're less than or equal to 3, like below. Again, either way works just as good, but perhaps this is evan simpler.

PHP Code:
if ($spin_1 <= && $spin_2 <= && $spin_3 <= 3) {
   
// ...

__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.

Last edited by lizciz; 03-16-2009 at 05:28 PM.. Reason: Misspelled variable name
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 03-16-2009, 05:34 PM Re: If Statement - || and && fails.
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Nice post lizciz. As an added suggestion, this generalizes the whole routine and provides a bit more information:

PHP Code:
<?php
$winning_numbers 
= array(2,3,4);
$spins = array(2,3,4);

$num_spins count($spins);
$num_wins 0;
for (
$i=0;$i $num_spins;$i++) {
  
$num_wins += (int)in_array($spins[$i],$winning_numbers);
}
echo 
'<p>'.$num_wins.' out of '.$num_spins.' matched</p>';
if (
$num_wins == $num_spins) {
  echo 
"<h1>YOU'RE A WINNER!</h1>";
}
?>
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to If Statement - || and && fails.
 

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.39129 seconds with 12 queries