0

At the beginning I would like to say that this is my first post and I apologize in advance for any possible mistakes. And these are my beginnings in programming.

I have a problem with appropriate action download.php in the following code snippet:

<?php
    $cv= $row[25];
    $output .= ' 
    <tr>
        <td> <a href='download.php?dow=$cv'>Download</a> </td> 
    </tr> 
    ';  
?>

I wish it worked like the following code (in which it operates correctly):

<tr>
    <td><?php   echo "<a href='download.php?dow=$cv'>Download</a><br>"; ?></td>
</tr>
EddyTheDove
  • 12,979
  • 2
  • 37
  • 45
  • 2
    Possible duplicate of [What is the difference between single-quoted and double-quoted strings in PHP?](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – M. Eriksson Mar 04 '17 at 23:42

2 Answers2

0

You need to concatenate the string. To concatenate you use a dot . This will put the variable value inside your string.

<?php

$output .= " 
<tr>
     <td><a href='download.php?dow=".$cv."'>Download</a> </td> 
</tr> 
"; 
Rikard Olsson
  • 841
  • 1
  • 7
  • 28
  • 1
    To help the OP understand the issue, you should explain what you have changed and why. Don't just paste some code. – M. Eriksson Mar 04 '17 at 23:43
  • Thank you very much for the apposition, but still pops up an error: Parse error: syntax error, unexpected 'download' (T_STRING) –  Mar 04 '17 at 23:50
  • 1
    @kejt You need to look at the quotes in this answer compared to yours. Specifically the usage of single and double quotes. This is one of the reasons why a good answer includes a full explanation about the changes and the original issue. – M. Eriksson Mar 04 '17 at 23:53
  • 1
    there is no need to write variable outside `"` unlike the `'`. – TheMMSH Mar 04 '17 at 23:57
  • Yes it's true. No longer causes a fatal error. But I would like to ask why after clicking on the Download downloaded file "download.php" (which is empty) instead of "something.docx" (as is the case with the correct link) –  Mar 05 '17 at 00:10
  • I don't really understand your question. Do you get a downloaded file called "download.php"? – Rikard Olsson Mar 05 '17 at 10:43
0

you closed string before it's even finished! the $output is a variable and you tried to store the HTML as an string inside it, but you've closed the ' sign before the download.php?dow=$cv and then opened it again. the correct way to write it is:

<?php
$cv= $row[25];
$output .= " 
<tr>
<td> <a href=\"download.php?dow=$cv\">Download</a> </td> 
</tr> 
";  
?>

the \ before " called escaping and is needed because we dont want to end our string! just want to add that quote sign as a character like the others.

TheMMSH
  • 501
  • 6
  • 26
  • It would have worked to just change the outer quotes to double quotes and keep the single quotes for the href-tag. Then there wouldn't be any need for escaping. – M. Eriksson Mar 04 '17 at 23:55
  • I do not know why, but when I click on Download now appears Error 403 –  Mar 05 '17 at 06:46