zohab Posted January 10, 2012 Share Posted January 10, 2012 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 https://forums.phpfreaks.com/topic/254698-how-to-parse-php-variable-in-single-quote/ Share on other sites More sharing options...
Psycho Posted January 10, 2012 Share Posted January 10, 2012 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 https://forums.phpfreaks.com/topic/254698-how-to-parse-php-variable-in-single-quote/#findComment-1306014 Share on other sites More sharing options...
zohab Posted January 10, 2012 Author Share Posted January 10, 2012 Thanks I am able to parse variables using heredoc Link to comment https://forums.phpfreaks.com/topic/254698-how-to-parse-php-variable-in-single-quote/#findComment-1306018 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.