note 39194 modified in language.variables by danbrown

From: Date: Sun, 20 Jul 2008 16:10:43 +0000
Subject: note 39194 modified in language.variables by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142160@lists.php.net to get a copy of this message
You don't necessarily have to escape the dollar-sign before a variable if you want to output its name. You can use single quotes instead of double quotes, too. For instance: <?php $var = "test"; echo "$var"; // Will output the string "test" echo "\$var"; // Will output the string "$var" echo '$var'; // Will do the exact same thing as the previous line ?> Why? Well, the reason for this is that the PHP Parser will not attempt to parse strings encapsulated in single quotes (as opposed to strings within double quotes) and therefore outputs exactly what it's being fed with :) To output the value of a variable within a single-quote-encapsulated string you'll have to use something along the lines of the following code: <?php $var = 'test'; /* Using single quotes here seeing as I don't need the parser to actually parse the content of this variable but merely treat it as an ordinary string */ echo '$var = "' . $var . '"'; /* Will output: $var = "test" */ ?> HTH - Daerion --was-- In reference to "remco at clickbizz dot nl"'s note I would like to add that you don't necessarily have to escape the dollar-sign before a variable if you want to output it's name. You can use single quotes instead of double quotes, too. For instance: <?php $var = "test"; echo "$var"; // Will output the string "test" echo "\$var"; // Will output the string "$var" echo '$var'; // Will do the exact same thing as the previous line ?> Why? Well, the reason for this is that the PHP Parser will not attempt to parse strings encapsulated in single quotes (as opposed to strings within double quotes) and therefore outputs exactly what it's being fed with :) To output the value of a variable within a single-quote-encapsulated string you'll have to use something along the lines of the following code: <?php $var = 'test'; /* Using single quotes here seeing as I don't need the parser to actually parse the content of this variable but merely treat it as an ordinary string */ echo '$var = "' . $var . '"'; /* Will output: $var = "test" */ ?> HTH - Daerion http://php.net/manual/en/language.variables.php

« previous php.notes (#142160) next »