Echo/Print

I have multiple if statements and each if statement works independently with the correct results but when I put all the if statements together it will only print the last if statement and the last else statement.

Would anyone know why?

if (…) {
print “something”;
}

else if (…) {
print “something”;
}

else if (…) {
print “”
}

else if (…) {
print “”;
}

else if (…) {
print “something”;
}

else if (…) {
print “”
}

else if (…) {
print “something”;
}

else if (…) {
print “something”
}

else if (…) {
print “something”;
}
else {
print “something”;
}

This is on my other post but getting no answers. :frowning:

If it prints what’s in the else block then none of the if conditions are true. It’s kinda impossible to say much more without knowing the conditions/values.

??

[php]if (“youPostYourCode”==true){
echo “We can help you”;
}
else{
echo “Good luck”;
}[/php]

Like other people have said need to see your code to get you help, but from the if statements you have provide it seems the switch statement would be more appropriate (I’m only guessing). http://us2.php.net/manual/en/control-structures.switch.php

[php]<?php

$num1 = rand(1,20);
$num2 = rand(1,20);
$num3 = rand(1,20);
$num4 = rand(1,20);
$num5 = rand(1,20);
$pwrBall = rand(1,15);

$win1 = 5;
$win2 = 10;
$win3 = 4;
$win4 = 17;
$win5 = 1;
$winpwrBall = 15;

$pwrMatch;
$match1;
$match2;
$match3;
$match4;
$match5;
$match6;

if ($num1 == $win1)
{
$match1 = 1;
}
if ($num1 == $win2)
{
$match1 = 1;
}
if ($num1 == $win3)
{
$match1 = 1;
}
if ($num1 == $win4)
{
$match1 = 1;
}
if ($num1 == $win5)
{
$match1 = 1;
}
else {
$match1 = 0;
} //end match1

if ($num2 == $win1)
{
$match2 = 1;
}

if ($num2 == $win2)
{
$match2 = 1;
}
if ($num2 == $win3)
{
$match2 = 1;
}
if ($num2 == $win4)
{
$match2 = 1;
}
if ($num2 == $win5)
{
$match2 = 1;
}
else {
$match2 = 0;
} //end match2

if ($num3 == $win1)
{
$match3 = 1;
}

if ($num3 == $win2)
{
$match3 = 1;
}
if ($num3 == $win3)
{
$match3 = 1;
}
if ($num3 == $win4)
{
$match3 = 1;
}
if ($num3 == $win5)
{
$match3 = 1;
}
else {
$match3 = 0;
} //end match3

if ($num4 == $win1)
{
$match4 = 1;
}

if ($num4 == $win2)
{
$match4 = 1;
}
if ($num4 == $win3)
{
$match4 = 1;
}
if ($num4 == $win4)
{
$match4 = 1;
}
if ($num4 == $win5)
{
$match4 = 1;
}
else {
$match4 = 0;
} //end match4

if ($num5 == $win1)
{
$match5 = 1;
}

if ($num5 == $win2)
{
$match5 = 1;
}
if ($num5 == $win3)
{
$match5 = 1;
}
if ($num5 == $win4)
{
$match5 = 1;
}
if ($num5 == $win5)
{
$match5 = 1;
}
else {
$match5 = 0;
} //end match5

if ($pwrBall == $winpwrBall)
{
$match6 = 1;
}
else {
$match6 = 0;
} //end match6

if (($match1 + $match2 + $match3 + $match4 + $match5 == 0) && ($match6 == 1)){
print “You won $4.00!
”;
}
else if (($match1 + $match2 + $match3 + $match4 + $match5 == 1) && ($match6 == 1)){
print “You won $4.00!
”;
}

else if (($match1 + $match2 + $match3 + $match4 + $match5 == 2) && ($match6 == 1)){
print “You won $7.00!
”;
}

else if (($match1 + $match2 + $match3 + $match4 + $match5 == 3) && ($match6 == 0)){
print “You won $7.00!
”;
}
else if (($match1 + $match2 + $match3 + $match4 + $match5 == 3) && ($match6 == 1)){
print “You won $100.00!
”;
}
else if (($match1 + $match2 + $match3 + $match4 + $match5 == 4) && ($match6 == 0)){
print “You won $100.00!
”;
}
else if (($match1 + $match2 + $match3 + $match4 + $match5 == 4) && ($match6 == 1)){
print “You won $10,000!
”;
}

else if ($match1 == 1 && $match2 == 1 && $match3 == 1 && $match4 == 1 && $match5 == 1 && $match6 == 0){
print “You won $1,000,000!
”;
}

else if ($match1 == 1 && $match2 == 1 && $match3 == 1 && $match4 == 1 && $match5 == 1 && $match6 == 1){
print “You won 330 million Dollars!
”;
}

else {
	echo "Sorry you did not win anything.<br />";
}


print "Your numbers are: $num1, $num2, $num3, $num4, $num5. Your PowerBall number is: $pwrBall <br />
     This weeks numbers are: 5, 10, 4, 17, 1 with the PowerBall number of 15.<br />"  

?>
[/php]

Thank You!

The part I’m asking about is at the bottom of the code if anyone is going to reply. Why am I having problems getting help on this? This is my second post with same problem and it seems people just stop helping when they see my simple code. Is this too easy? Its not to me… :frowning: I’m just wanting to learn and I’m not asking anyone to do it for me, just point me in the right direction. I just need to figure this out then I’m moving onto arrays so please don’t suggest an array, I’m not there yet.

If anyone knows of another PHP site that will help please let me know.

I can only guess, but I think it might be bad luck. Also even though the code is simple it is quite tedius/long. After a while when you see the light and code in objects your code will (hopefully) be way more organized.

Also, whenever I log on to this site I have 10-15 new posts and ~5 new messages, and between job, my private projects and my life in general I simply don’t put down enough time for this forum to go through everything. I would guess that is the case for other people as well.

I would guess a paid service where you actually get a customer representative for your case/issue is way more guaranteed to get a fast and efficient service.

But! That doesn’t mean I can’t look at this now

[php] if ($num1 == $win1)
{
$match1 = 1;
}
if ($num1 == $win2)
{
$match1 = 1;
}
if ($num1 == $win3)
{
$match1 = 1;
}
if ($num1 == $win4)
{
$match1 = 1;
}
if ($num1 == $win5)
{
$match1 = 1;
}
else {
$match1 = 0;
} //end match1[/php]

These matches of yours won’t work. Say if $num1 matches $win2, then we will get a progress like this

Does $num1 match $win1? no

Does $num1 match $win2? yes! set $match1 = 1

Does $num1 match $win3? no

Note PHP continues to validate the ifs even though we’ve found a match

Does $num1 match $win4? no

Does $num1 match $win5? no, else, set $match = 0 <-- overwriting the 1 we got already

Note the else only affect the last if, so as long as $numX doesn’t match $winX you will get $match = 0

[hr]

In order to get this as you want it to be you need to change the match to use else/if

[php] if ($num1 == $win1)
{
$match1 = 1;
}
elseif ($num1 == $win2)
{
$match1 = 1;
}
elseif ($num1 == $win3)
{
$match1 = 1;
}
elseif ($num1 == $win4)
{
$match1 = 1;
}
elseif ($num1 == $win5)
{
$match1 = 1;
}
else {
$match1 = 0;
} //end match1[/php]

Now PHP will break out of the else/if if it finds a match, or if no match is found it will set $match1 = 0

Thank You JimL for your help and explanation. I’m hoping something is going to “click” and I can move forward in this. By the way the code is now working and I did learn a lot from this simple code.

Sponsor our Newsletter | Privacy Policy | Terms of Service