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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need some help with an if statement 2

Status
Not open for further replies.

reisende

Programmer
Mar 16, 2004
74
US
Hi everybody,

First let me preface this by saying that I'm not very good with PHP so if my issue sounds easy/stupid then please forgive me.

I have something driving me crazy. What I'm trying to do is check whether a variable is populated or not and if so set a boolean to true and vice versa.

Code:
if ($username != "") {
  $auth = true;
} else {
  $auth = false;
}

Seems easy enough. I works when I print it to the screen. When I'm logged it $auth returns 1 and 0 when I'm not.

What I don't get is that I'm then trying to use the $auth variable to determine if a link should be printed around a calendar date or not. This is not working and it's driving me nuts.

Code:
if ($auth) 
  $str .= "<a href=\"javascript: postMessage($day, $month, $year)\">$day</a>";
else
  $str .= "$day";

Again, seems easy enough, but all I ever get is the blank number ($day) even if the $auth variable is set to true.

I'm hoping this is just a quick syntax or code problem. Any help on this would be greatly appreciated.

Thanks.
 
I'd suggest some echo statements at various places for debugging, like right before the second code fragment.

I pasted your code into a php file, added constant values for the variables, and it worked just fine.
 
In addition to lgarner's sugestions, Is this
Code:
if ($username != "") {
  $auth = true;
} else {
  $auth = false;
}

on the same page as the the other piece of code.

Another thing is when checking wether a variable is set id use [green]isset[/green]
as in
Code:
if([green]isset[/green]($username)){
...

Becasue if you try to check a variable that has not been set you'll get an error.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Maybe that's the problem. The code that sets it to either a link or regular text (the second snippet) is in an include file. I'll try it in the same physical file and see if that helps.
 
OK, that worked. Thank you both. I didn't know they needed to be in the same file. Oh well, learn something new each day. :)
 
Glad you sorted it out.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top