-1

I am having a small trouble. I hope I am not outputing string properly.Here is the code

$icon = ( isset( $tab['icon'] ) && $tab['icon'] ) ?'<i class="$tab['icon']"</i>' : '';

I want to output this <i class="$tab['icon']"</i>

Thank you.

  • You have a **syntax error** – user2182349 Oct 04 '15 at 03:50
  • Are you trying to output the value of $tab['icon'], or the literal text "$tab['icon']"? You seem to be saying the latter, and that's what it'll do as written if the value is nonfalse. That is, if you escape your single quotes. – Paul Kienitz Oct 04 '15 at 03:54
  • WOW. I know its syntax error. But where is the error? – Busted Angon Oct 04 '15 at 03:57
  • You have single quotes in a single-quoted string. (And if you fix that, your generated HTML doesn't have a > on its – Paul Kienitz Oct 04 '15 at 04:00
  • yes. I have a variable stored in $tab['icon']. I am using shorthand property. This works perfectly $icon = ( isset( $tab['icon'] ) && $tab['icon'] ) ? $tab['icon'] : ''; – Busted Angon Oct 04 '15 at 04:00

1 Answers1

0

I am not very familiar with the shorthand if statement syntax so here is what I would do longhand. This is assuming $tab['icon'] needs to be set to the boolean True correct?

if(isset($tab['icon']) and $tab['icon']){
   echo "<i class='{$tab[icon]}'>Here it is with a class!</i>";
}
blazerunner44
  • 657
  • 8
  • 19
  • Thank you for your help. I have to edit a bit.Here it is $icon = ( isset( $tab['icon'] ) && $tab['icon'] ) ?"" : ''; – Busted Angon Oct 04 '15 at 04:07
  • Glad you got it to work. Take a look at the question someone attached about the difference between single and double quotes in PHP if you don't already know so you can see why I used them instead of single quotes. – blazerunner44 Oct 04 '15 at 04:11