Jump to content

Recommended Posts

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

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.

(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?

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}

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.

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. 

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.