alconebay Posted April 4, 2008 Share Posted April 4, 2008 OK, I have a database with fields for name, web, and phone. I'm making a directory page that displays the information, but I want to display only the name and website. However, if the person does not have a web site I want it to display their phone number instead. Here is the section I'm having problems with: <? echo $name; ?> - <? if ($web==0) {echo $phone;} else {echo $web;} ?> I have a loop set up so it pulls all the people in the directory but here is what displays on the page (no matter if they have a website or not): "The persons name" - "The persons phone" I was under the impression that "($web==0)" means "If the website field is empty" but I must be wrong. Link to comment https://forums.phpfreaks.com/topic/99557-solved-i-need-some-ifelse-help/ Share on other sites More sharing options...
alconebay Posted April 4, 2008 Author Share Posted April 4, 2008 I spoke too soon. I just found this page: http://us.php.net/empty is got my answer: <? echo $name; ?> - <? if (empty($web)) {echo $phone;} else {echo $web;} ?> Link to comment https://forums.phpfreaks.com/topic/99557-solved-i-need-some-ifelse-help/#findComment-509306 Share on other sites More sharing options...
craygo Posted April 4, 2008 Share Posted April 4, 2008 yes there is look here http://php.net/manual/en/types.comparisons.php Ray Link to comment https://forums.phpfreaks.com/topic/99557-solved-i-need-some-ifelse-help/#findComment-509307 Share on other sites More sharing options...
haku Posted April 4, 2008 Share Posted April 4, 2008 Two side points: 1) Don't use <? use <?php <? isn't supported by some servers, and your own server could change the settings one day unexpectedly, and your script wont work anymore. 2) You can change this: if (empty($web)) {echo $phone;} else {echo $web;} To this: echo (empty($web)) ? $phone : $web; Its shorthand that accomplishes the same thing. Not at all necessary, but a more compact piece of code if you want to use it! Link to comment https://forums.phpfreaks.com/topic/99557-solved-i-need-some-ifelse-help/#findComment-509314 Share on other sites More sharing options...
alconebay Posted April 4, 2008 Author Share Posted April 4, 2008 Thanks for the help, I'll use those tips. Link to comment https://forums.phpfreaks.com/topic/99557-solved-i-need-some-ifelse-help/#findComment-509317 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.