WeBBy421 Posted Thursday at 05:22 PM Share Posted Thursday at 05:22 PM Have the following code that works just fine but throws a Undefined variable warning: <a class="custom-btn custom-btn--small custom-btn--style-4" <? if ($user == ""){ echo "href=\"https://www.paratuberculosis.com/login.php\">Member Login</a>>"; } If I try to use: <? if(isset($user) && $user == ''){ It does not work. In other words I get nothing and the ">" doesn't print and the entire page messed up. It's like $user is not seen by isset?? Any thoughts would be appreciated Quote Link to comment https://forums.phpfreaks.com/topic/325480-undefined-variable-warning-and-isset-issue/ Share on other sites More sharing options...
Barand Posted Thursday at 05:59 PM Share Posted Thursday at 05:59 PM Hvae you looked at the page's html source code? Quote Link to comment https://forums.phpfreaks.com/topic/325480-undefined-variable-warning-and-isset-issue/#findComment-1640098 Share on other sites More sharing options...
jodunno Posted Thursday at 06:26 PM Share Posted Thursday at 06:26 PM try empty(), which will supress warnings if a variable does not exist: <?php <a class="custom-btn custom-btn--small custom-btn--style-4" if (empty($user)){ echo 'href="https://www.paratuberculosis.com/login.php">Member Login</a>>'; } ?> https://www.php.net/manual/en/function.empty.php it will handle '', 0, !isset and false I assume that you are using wordpress or some other pre-coded resource You should be in control of your variables, especially user-based variables. Quote Link to comment https://forums.phpfreaks.com/topic/325480-undefined-variable-warning-and-isset-issue/#findComment-1640104 Share on other sites More sharing options...
WeBBy421 Posted Friday at 02:17 AM Author Share Posted Friday at 02:17 AM (empty) does not work because there is more to the code than what I showed. Didn't show entire code because thew first half fails on its own. Here is the full code that words: <li class="li-btn"> <a class="custom-btn custom-btn--small custom-btn--style-4" <? if ($user == ""){ echo "href=\"https://www.example.com/login.php\">Member Login</a>"; } elseif ($user != ""){ echo "href=\"https://www.example.com/mycp.php\">$user</a>"; } ?> > </li> That above works but throws the Undefined variable warning; If I use: if(isset($user) && $user == ''){ echo "href=\"https://www.example.com/login.php\">Member Login</a>"; The page is completely destroyed because $user is not recognized or something and the <a class is not closed: <a class="custom-btn custom-btn--small custom-btn--style-4" </li> WHY? Quote Link to comment https://forums.phpfreaks.com/topic/325480-undefined-variable-warning-and-isset-issue/#findComment-1640120 Share on other sites More sharing options...
mac_gyver Posted Friday at 03:52 AM Share Posted Friday at 03:52 AM the code that's responsible for populating the $user variable needs to setup a default value if there is no user. the code testing the value then only needs to be concerned with what the value is. also, at that point, you are only dealing with a true or false boolean value. you should test for the true case, e.g. if($user){ // there is a user} else { // there is not a user} Quote Link to comment https://forums.phpfreaks.com/topic/325480-undefined-variable-warning-and-isset-issue/#findComment-1640123 Share on other sites More sharing options...
jodunno Posted Friday at 04:28 AM Share Posted Friday at 04:28 AM 2 hours ago, WeBBy421 said: (empty) does not work 0.0 empty works. see my included code and use it in xampp. 2 hours ago, WeBBy421 said: The page is completely destroyed because $user is not recognized or something and the <a class is not closed: if you open an anchor tag outside of php, then close the anchor tag outside of php. 2 hours ago, WeBBy421 said: WHY? because apparently the user variable is undefined and the ending anchor tag is contained within illogical php code that is not executing. <?php //$undefinedVariable = '!empty'; //$user = ''; $user = 'test'; $userhref = $user ? 'href="https://www.example.com/mycp.php"': 'href="https://www.example.com/login.php"'; $usertext = $user ? $user: 'Member Login'; ?> <ul><li class="li-btn"> <a class="custom-btn custom-btn--small custom-btn--style-4" <?php if (empty($user)){ echo 'href="https://www.example.com/login.php">Member Login'; } else { echo 'href="https://www.example.com/mycp.php">'.$user; } ?> </a> </li></ul> <ul><li class="li-btn"> <a class="custom-btn custom-btn--small custom-btn--style-4" <?php echo $userhref . '>' . $usertext; ?></a> </li></ul> <ul>Also: <li class="li-btn"> <?php echo empty($undefinedVariable) ? 'empty == true': $undefinedVariable; ?> </li> </ul> start over and think about what you are trying to accomplish. for one thing, user must mean that you have a login. How are you storing user on every page within your site? is user variable based upon a form input (post)? then one has to login on every page? one would have to login on every different page because a variable is local to the current script. You should be using a session variable or reading user from a database. Show us your code for user variable. Quote Link to comment https://forums.phpfreaks.com/topic/325480-undefined-variable-warning-and-isset-issue/#findComment-1640124 Share on other sites More sharing options...
Phi11W Posted Friday at 09:33 AM Share Posted Friday at 09:33 AM You've made the closing of the anchor tag conditional on the user being set. It shouldn't be. Assuming you want an anchor element that lacks its href attribute if the user isn't set, then use something like this: printf( '<a class="custom-btn custom-btn--small custom-btn--style-4"%s>Member Login</a>' , ( ( isset( $user ) && '' !== $user ) ? ' href="https://www.paratuberculosis.com/login.php"' : '' ) ); Personally, I'd omit the whole element, like this, but YMMV: if ( isset( $user ) && '' !== $user ) echo '<a class="custom-btn custom-btn--small custom-btn--style-4" href="https://www.paratuberculosis.com/login.php">Member Login</a>' ; Regards, Phill W. Quote Link to comment https://forums.phpfreaks.com/topic/325480-undefined-variable-warning-and-isset-issue/#findComment-1640133 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.