Jump to content

How to Parse php variable in single quote.


zohab

Recommended Posts

Hi,

 

I am able to parse php variable in double quote but not in single quote.

 

How can I parse in single quote.

 

Following example shows 2 results and I want same result in both.

 

First Name : Zohaib

First Name : $firstname

 

// Connecting, selecting database
$link = mysql_connect('localhost', 'root', 'password');
mysql_select_db('dbname');

// Performing SQL query
$query = 'SELECT first_name FROM tablename';
$result = mysql_query($query);

// Printing results in HTML
while ($row = mysql_fetch_assoc($result)) {
$firstname=$row['first_name'];
}

echo"<table>
<tr>
	<td>First Name : </td>
	<td>$firstname</td>
</tr></table>";

echo'<table>
<tr>
<td>First Name : </td>
<td>$firstname</td>
</tr></table>';

 

What are the changes I need to do to achieve same result.

 

Any solution ?

 

- Thanks.

That is the expected behavior for PHP. There are four different methods of defining strings in PHP: single quoted, double quoted, heredoc syntax & nowdoc syntax. Only in the double quoted and heredoc methods are variables parsed. Also, special escape sequences for things such as tabs (\t) and new lines (\n) will be parsed.

 

See the documentation here: php.net/manual/en/language.types.string.php

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.