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
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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.