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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.