Jump to content

[SOLVED] I need some If/Else Help


alconebay

Recommended Posts

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

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.