What Wrong with this Simple Conditional Script?

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
theoph
Forum Commoner
Posts: 47
Joined: Wed Jul 30, 2003 5:26 pm
Location: Lexington, KY USA

What Wrong with this Simple Conditional Script?

Post by theoph »

Please excuse my ignorance as I'm a newbie, but why is the following conditional argument not working? I'm getting the statment part of the script when the conditional data ($row_rsDisplay) meets one or both of the listed conditions.

Code: Select all

<?php if((($row_rsDisplay['wedding']) != '00-00') || (!empty($row_rsDisplay['wedding'])))
		{ echo '<span class="underline">Anniversary:</span>&nbsp;' . $row_rsDisplay['wedding'];}?>
-Dave
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

Code: Select all

<?php 
if
{
(($row_rsDisplay['wedding']) != '00-00') || (!empty($row_rsDisplay['wedding']))
} 
else
{ 
echo '<span class="underline">Anniversary:</span> ' . $row_rsDisplay['wedding'];
}?>
does that work
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

Isn't an && warranted in this situation and not || ?

Code: Select all

// Only output Aniversary if wedding is not empty and not equal to 00-00
if (($row_rsDisplay['wedding']) != '00-00') && (!empty($row_rsDisplay['wedding'])) {
     echo '<span class="underline">Anniversary:</span> ' . $row_rsDisplay['wedding'];
}
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

microthick wrote:Isn't an && warranted in this situation and not || ?

Code: Select all

// Only output Aniversary if wedding is not empty and not equal to 00-00
if (($row_rsDisplay['wedding']) != '00-00') && (!empty($row_rsDisplay['wedding'])) {
     echo '<span class="underline">Anniversary:</span> ' . $row_rsDisplay['wedding'];
}
sorry, microthick is right, however i dont understand the format in the code that was provided first :cry:, maybe its just me, im starting to get programmers fatigue after (insert stupid amounts of hours) in a row sat at the computer
theoph
Forum Commoner
Posts: 47
Joined: Wed Jul 30, 2003 5:26 pm
Location: Lexington, KY USA

Post by theoph »

Got it to work using the following script, but there must be a better way to write it though.

Code: Select all

<?php	if(($row_rsDisplay['wedding']) == '00-00')
		{}
	elseif(empty($row_rsDisplay['wedding']))
		{}
	else
		{
			 echo '<span class="underline">Anniversary:</span>&nbsp;' . $row_rsDisplay['wedding'];
		}

?>
-DAVE
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Code: Select all

if($row_rsDisplay['wedding'] == '00-00') $row_rsDisplay['wedding'] = '';
if(!empty($row_rsDisplay['wedding'])){
    echo '<span class="underline">Anniversary:</span> ' . $row_rsDisplay['wedding'];
}
?
Last edited by markl999 on Thu Jan 22, 2004 11:59 am, edited 1 time in total.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

theoph wrote:Got it to work using the following script, but there must be a better way to write it though.

Code: Select all

<?php
	if(($row_rsDisplay['wedding']) == '00-00')
		{}
	elseif(empty($row_rsDisplay['wedding']))
		{}
	else
		{
			 echo '<span class="underline">Anniversary:</span>&nbsp;' . $row_rsDisplay['wedding'];
		}

?>
-DAVE
there most definitely is.
in a case whre you have several tests, for serveral different options, switch is faster. your style would be better suited for switch.

microthick's is better for using an if. here it is with many more comments

Code: Select all

<?php
// Only output Aniversary if wedding is not empty and not equal to 00-00
if (($row_rsDisplay['wedding']) != '00-00') # test that it's not equal to '00-00'
 && # and set another conditionthat MUST be met
(!empty($row_rsDisplay['wedding'])) { # make that not being empty
     echo '<span class="underline">Anniversary:</span> ' .  $row_rsDisplay['wedding']; # do what needs to be done then
}
?>
theoph
Forum Commoner
Posts: 47
Joined: Wed Jul 30, 2003 5:26 pm
Location: Lexington, KY USA

Post by theoph »

Learn something every day.

Thanks for all the imput guys.

:)
Post Reply