changing <a> value depending on the <?php condition?>

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
preenzraven
Forum Newbie
Posts: 1
Joined: Sun Jan 15, 2012 11:04 pm

changing <a> value depending on the <?php condition?>

Post by preenzraven »

I am just wondering if this is possible because I am using a drop down login form when I click Login Link but when I click Login this will change to Close since Dropdown Login is shown and if closed then Close link will go back to its original value Login.

Code: Select all

			<li><a rel="#fe4902" href="#" class="btn-slide">
			<? if($log==false)
			{
			$log=true;
			?>Login<?
			}elseif($log==true)
			{
			$log=false; 
			?>Close<?
			}?>
			</a></li>
this is the code I use. and the output is <a>LoginClose</a> instead of <a>Login</a> or <a>Close</a> while opened.

Hope someone helps =)
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: changing <a> value depending on the <?php condition?>

Post by social_experiment »

Yes it's possible to change the text value of the link;

Code: Select all

<li>
<a rel="#fe4902" href="#" class="btn-slide"><?php ($log == false) ? $text = 'Login' : $text = 'Close'; echo $text; ?></a>
</li>
Don't use short php tags (<? ?>) rather go for <?php and close with ?>

Hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: changing <a> value depending on the <?php condition?>

Post by twinedev »

Just curious the point of toggling the value of $log

Code: Select all

<li><a rel="#fe4902" href="#" class="btn-slide"><?php echo ($log==FALSE) ? 'Login' : 'Close'; ?></a></li>
-Greg
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: changing <a> value depending on the <?php condition?>

Post by social_experiment »

@twinedev Good use of the echo and ternary operator; i'll definitely use this format in future. I used to get tripped up by adding an echo in die 2nd and 3rd expressions so i did it in the way i posted
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply