-1

How can I insert a php variable inside my html string? I tried to do it like this, but it won't run. Please refer to the code below.

<?php 
  $a['id'] = 1;
  $html .= '<a href="{$a['id']}">Link</a>';
?>
claudios
  • 6,588
  • 8
  • 47
  • 90
  • 2
    how about fixing the typos? `$a['id'] = 1;` –  May 03 '16 at 02:18
  • Fix the typos. Concatenate variable, or use double quotes and escape the ones for attribute encapsulation. – chris85 May 03 '16 at 02:23
  • Above is just an example. What if I wanted to `echo` that variable inside a double coated string. @Dagon – claudios May 03 '16 at 02:24
  • I edited now., Sorry I missed that one – claudios May 03 '16 at 02:24
  • so many dupes. sigh no one bothers to search –  May 03 '16 at 02:28
  • Why down vote if already fixed the typo? – claudios May 03 '16 at 02:28
  • 1
    becasue its a lazy question that could of been answered with very little effort using any search engine –  May 03 '16 at 02:29
  • Coz sometimes using search engine leads us to many options and sometimes incorrect answers. That's why I use stackoverflow to receive direct help from professionals. But I know your point too. Thanks – claudios May 03 '16 at 02:31
  • look at the first match using your exact words: https://www.google.co.nz/search?q=What+if+I+wanted+to+echo+that+variable+inside+a+double+coated+string –  May 03 '16 at 02:31

1 Answers1

3

You missed out the closing '. Also, you need to concat them using the PHP concatenation operator . to join the string with the variable:

$a['id'] = 1;
$html .= '<a href="' . $a['id'] . '">Link</a>';
Panda
  • 6,955
  • 6
  • 40
  • 55