Forum Moderators: coopster

Message Too Old, No Replies

Help with an if else thing

         

abbeyvet

12:37 am on Aug 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am not good with PHP, and am trying to make a script display information conditionally and not succeeding at all.

The name of a city is displayed like this:
<?php echo $xcityid>0?"$xcityname":$xcountryname;?>

In the script cities are arranged under regions, and when the region page is viewed this is appended to the URL:

?cityid=-1

When a city is viewed, the url has something like?cityid=20

I want to have it so that if it is a region that is being viewed, its name is not displayed, but instead "My Text" is displayed.

I have tried this:

if ( $cityid = -1 )
{echo "My Text";}
else
{ echo $xcityid>0?"$xcityname":$xcountryname; }

The outcome is that 'My Text' is displayed on all pages and the city name never appears.

Can anyone suggest anything?

mcavic

1:08 am on Aug 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if ( $cityid = -1 )
{echo "My Text";}
else
{ echo $xcityid>0?"$xcityname":$xcountryname; }

Should be if ( $cityid == -1 )

$xcityid>0?"$xcityname":$xcountryname

I never liked the inline if thing. I'd personally stick to standard if/else if/else statements to keep things more readable.

abbeyvet

10:23 am on Aug 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<embarrased>Duh!</embarrased>

And you're right, I got rid of the inline if, it is messy.

dreamcatcher

10:38 am on Aug 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Its called a Ternary Operator [uk.php.net]

All down to personal preference really. I use them all the time as it can minimalize coding blocks and keep things tider.

dc