Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If Null forget this line 3

Status
Not open for further replies.

kupe

Technical User
Sep 23, 2002
376
I use this to draw information from the mysql database to place on the webpage.

printf("<h4>%s\n</h4>", $myrow["Headline"]);
printf(" %s\n<br>", $myrow["NewsStory"]);
printf("<div class=\"names2\">- %s</div>", $myrow["Byline"]);
printf("Bulletin: %s", date("F d", strtotime($myrow["NewsDate"])));

Often there isn't a byline. How can I say in this line ...

printf("<div class=\"names2\">- %s</div>", $myrow["Byline"]);

if Byline = Null please ignore this one line, please Experts?
 
Code:
printf("<h4>%s\n</h4>", $myrow["Headline"]);  
printf(" %s\n<br>", $myrow["NewsStory"]);       
if ($myrow["Byline"])
  printf("<div class=\"names2\">-  %s</div>", $myrow["Byline"]);      
printf("Bulletin: %s", date("F d", strtotime($myrow["NewsDate"])));

(Says if there's a value in $myrow["Byline"] print the line, otherwise, move on).

[cheers]
Cheers!
Laura
 
Hi cLFlaVA, Laura

Thanks very much, Gurus. I like both of them, Laura, cLFlaVA. Is one considered a better way than the other?

Appreciated very much. Cheers
paul

 
the first one is better than the second because you are not relying on PHP to do the auto type conversion from a non-empty string to a true value for the if-test.

However, I think easier than the first would be:

if (!empty($myrow["Byline"]))

this seems more semantically what you are looking for. BEWARE: empty() will return true (meaning the string is "empty") if the string = "0" (string value of 0). As long as Byline won't take on that value, you should be fine with that above.

And technically speaking, the ! negation is quicker than the greater-than comparison (">") of the integer returned by strlen() and the 0. Though, that is pretty nit-picky, its very unlikely you'd be able to see the performance difference unless of course this code was inside a loop that executed thousands of times or something.
 
That's very compact, Shadedecho, and will be really handy because I've found many places where this same problem applies. I am most indebted to you, and thanks enormously for explaining the pros and cons to a beginner who is starry-eyed by the php/mysql combination for websites.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top