-2

I need to echo this code:

<a href="javascript:toggle('test')"> x Click.</a>  

I tried it like this:

if (!empty($item['criteria'])) {
    foreach ($item['criteria'] as $item2){
        echo "<a href="javascript:toggle('test')>Click</a>";
        echo '<div id="'. test.'" style="display: none">'. $item2['description'].'</div>';
    }
}

I think there is a mistake with the "".

Will
  • 24,082
  • 14
  • 97
  • 108
Hertus
  • 147
  • 2
  • 11
  • 2
    Possible duplicate of [PHP Parse/Syntax Errors; and How to solve them?](http://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – u_mulder Jul 09 '16 at 10:32
  • 1
    You need to learn about escaping quotes. – Mitya Jul 09 '16 at 10:32

2 Answers2

0

You're looking for something like this:

<?php
if (!empty($item['criteria'])) {
    foreach ($item['criteria'] as $item2) {
        echo "<a href=\"javascript:toggle('test');\">Click</a>";
        echo '<div id="' . 'test' . '" style="display: none">'. $item2['description'].'</div>';
    }
}

You need to properly escape your quotes inside strings.

Will
  • 24,082
  • 14
  • 97
  • 108
0

If you are going to have quotes in a string wrapped in the same type of quotes - e.g. " something" " then you need to escape the quotes in the string with \, so in your case:

if(!empty($item['criteria'])){
            foreach ($item['criteria'] as $item2){
             echo "<a href=\"javascript:toggle('test')\">Click</a>";
             echo '<div id="'. 'test' .'" style="display: none">'. $item2['description'].'</div>';
            }
        }
}

You also forgot to close the href with second "

Ivan Kovachev
  • 402
  • 5
  • 12