Page 1 of 1

DAMN }else{

Posted: Tue Sep 10, 2002 11:46 pm
by jbdphp
I am having problems with my ELSEIF statement (it isn't printing what it should when $sid is != to $sid). The IF is working fine but here check it out (thanks in advance):

Code: Select all

<?php
		if($sid == $sid):
			$sql = mysql_query("SELECT * FROM shows WHERE sid = '$sid' ORDER BY sid DESC LIMIT 1");
				
				while($row=mysql_fetch_array($sql)) {
					$sid = $rowї"sid"];
					$sdfdb = $rowї"sdfdb"];
					$show_date = $rowї"show_date"];
					$bandaide_band = $rowї"bandaide_band"];
					$other_bands = $rowї"other_bands"];
					$venue = $rowї"venue"];
					$city = $rowї"city"];
					$state = $rowї"state"];
					$time = $rowї"time"];
					$price = $rowї"price"];
					
						echo "<table width="100%" border="0" align="center">";
						echo "<tr><td width="30%">Date</td><td width="30%">$show_date</td></tr>";
						echo "<tr><td width="30%">Band+aide band</td><td width="30%">$bandaide_band</td></tr>";
						echo "<tr><td width="30%">Other bands</td><td width="30%">$other_bands</td></tr>";
						echo "<tr><td width="30%">Venue</td><td width="30%">$venue</td></tr>";
						echo "<tr><td width="30%">City</td><td width="30%">$city</td></tr>";
						echo "<tr><td width="30%">State</td><td width="30%">$state</td></tr>";
						echo "<tr><td width="30%">Time</td><td width="30%">$time</td></tr>";
						echo "<tr><td width="30%">Price</td><td width="30%">$price</td></tr>";
				
			
						}
?>

<tr><td width="30%"></td><td width="30%"><br><a href="javascript:;" onClick="printshow('printshow.php?sid=<?php echo "$sid" ?>','','scrollbars=yes,width=400,height=300')">Print 
Show Info</a></td></tr>
	
</table>

	<?
	
	elseif($sid != $sid):
					
						echo "<tr><td>Show ID is invalid.  Go Back.</td></tr>";
						echo "</table>";
					endif;
		
	?>

Posted: Wed Sep 11, 2002 12:32 am
by volka
if($sid == $sid):
you could write "if(TRUE): ... elseif(FALSE):" as well
A variable is always (?) equal to itself and never unequal to itself.
What do you want to achieve or is it a typo? ;)

Posted: Wed Sep 11, 2002 1:00 am
by Takuma
OK, if you are using ELSEIF shoudn't there be ELSE to? Because all the example in PHP manual has IF and ELSEIF and ELSE.

Posted: Wed Sep 11, 2002 1:10 am
by dusty
no takuma.

Like volka said, a variable is going to be equal to itself..

Posted: Wed Sep 11, 2002 5:59 am
by mikeq
What if it is a different type, === would be a better test for equality

Posted: Wed Sep 11, 2002 6:08 am
by twigletmac
But how can the same variable be of two different types at the same time?

Code: Select all

<?php
$sid = 2;

if ($sid === $sid) {
	echo 'of course it does!';
} else {
	echo 'wtf?';
}
?>
Doing

Code: Select all

if ($sid == $sid) {
is like writing

Code: Select all

if (2 == 2) {
so it's fairly pointless 2 will always be exactly equal to 2.

If you change the $sid value based on something else have an $old_sid value to compare against in the conditional statement:

Code: Select all

$sid = 2;
$old_sid = $sid;
$sid = 15;

if ($sid == $old_sid) {
    // Do something
} else {
    // Do something else
}
But once again, as Volka asked, what are you trying to achieve, what are you trying to compare?

Mac

Posted: Wed Sep 11, 2002 7:53 am
by Takuma
dusty wrote:no takuma.

Like volka said, a variable is going to be equal to itself..
That's a point but I'm also pinting another point! :wink:

Posted: Wed Sep 11, 2002 10:28 am
by dusty
Actually my post was saying no to your previous post saying that he forgot to include a } which isn't needed the way he is setting up his statements, but you seem to have edited that post.. The other part of my post was just saying the problem is with $sid == $sid and the rest of the code looks fine.

Posted: Wed Sep 11, 2002 5:08 pm
by BDKR
Perhaps I'm stupid or behind the times or something, but what's this about?

Code: Select all

if($sid == $sid):
In particular, the colon at the end of it? That actually works?

Cheers,
BDKR

Posted: Wed Sep 11, 2002 8:58 pm
by phpPete
Yes the colon works, it's an alternative syntax.

http://www.php.net/manual/en/control-st ... syntax.php

Code: Select all

if($var == 0 ):
    echo $someThing;
elseif ( $var !=0 ):
    echo $someThingElse;
else:
    echo $catchAll;
endif;   //<  --  note semi - colon on the endif

Posted: Thu Sep 12, 2002 2:24 am
by Takuma
Yesp but if you are going to use ELSEIF don't you have to have ELSE as well as ELSEIF?

Posted: Thu Sep 12, 2002 2:27 am
by twigletmac
No. You only need an else if you want something to happen if none of the conditions in the if and elseif's are met. You can just have an if or you can have an if and some elseifs or you can have an if some elseifs and an else or you can have an if and an else... It all depends what you are trying to do.

Mac

Posted: Thu Sep 12, 2002 2:55 am
by volka
it's simply a more convenient form of

Code: Select all

if(cond1) { }
else {
   if (cond2)
      ...
... }
some languages have it, some don't. But in then (like in C) it's only "a blank away"

Code: Select all

if(cond1) { }
else if (cond2) {}