Jump to content

Problem with Quotes


doubledee

Recommended Posts

I have a block of navigation tabs that I need to place inside of PHP so I can toggle whether the tabs appear or not.

 

Here is a snippet of what I am trying to do, but it is messy and not compiling or whatever you call it...

 

<?php
	echo "<ul id='profileMenu'>
			<li id='firstTab'>" . (isset($tabName=='about-me') ? class='current' : '') . "</li>
		</ul>";
?>

 

How can I both FIX THIS and CLEAN IT UP - if that is possible?!

 

Thanks,

 

 

Debbie

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/264672-problem-with-quotes/
Share on other sites

in your OP code:

 


<?php
	echo "<ul id='profileMenu'>
			<li id='firstTab'>" . (isset($tabName=='about-me') ? class='current' : '') . "</li>
		</ul>";
?>

 

1) Looks like your ternary is supposed to add something inside your opening "li" tag but you closed it before the ternary

 

2) isset syntax is wrong.  isset takes variables for arguments, not expressions.

 

3) Did not wrap quotes around the strings you are trying to add to the "li" tag

 

4) Did not properly wrap the ternary in parenthesis

 

Here is what the code should look like

 

<?php
	echo "<ul id='profileMenu'>
			<li id='firstTab'" . ((isset($tabName)&&$tabName=='about-me') ? " class='current'" : '') . "></li>
		</ul>";
?>

 

Couple notes/opinions/suggestions:

 

1) Issues like this won't happen if you separate your logic from your content.  Put that ternary in a variable before the echo and then include the variable in the echo.

 

2) If you want to echo things multi-line like that, you might wanna look into using heredoc syntax.  This will only really work out for you if you follow suggestion #1. 

 

Link to comment
https://forums.phpfreaks.com/topic/264672-problem-with-quotes/#findComment-1356501
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.