Page 1 of 1

How to parse a variable withing quotes ?

Posted: Thu Oct 01, 2009 12:57 pm
by nilaymedh
Hello. I'm trying to use simplepie. The feed url code is

Code: Select all

$feed = new SimplePie('http://simplepie.org/blog/feed/');
Instead of the actual url, I want to use a variable. Have been trying since a few hours but not yet successful; I may be lacking in knowledge or patience - i don't know. What I do know is that quotes are treated a sting and so variables won't process. But is there a workaround ? I've tried using "('$var')"; but that doesn't work either. Thanks

Re: How to parse a variable withing quotes ?

Posted: Thu Oct 01, 2009 1:01 pm
by jackpf

Re: How to parse a variable withing quotes ?

Posted: Fri Oct 02, 2009 12:02 am
by nilaymedh
Thanks - that was a lot of info on variables. But I'm still stuck. What I want to do is to use a variable within parenthesis and within quotes (quotes should appear), as in below code (which is not working)

Code: Select all

 
$var = 'http://simplepie.org/blog/feed/';
$feed = new SimplePie('echo $var');
. So that the output will be with the quotes

Code: Select all

$feed = new SimplePie('http://simplepie.org/blog/feed/');
. I've tried using ("echo '$var'"), which is also not working. Thanks for help !

Re: How to parse a variable withing quotes ?

Posted: Fri Oct 02, 2009 1:45 am
by Griven
You almost had it.

ECHO is only used when you want to visually output something onto the page so that the user can see it. If you are just using the contents of a variable in a function such as the one below, you simply need to type the variable name.

Code: Select all

$var = 'http://simplepie.org/blog/feed/';
$feed = new SimplePie($var);
Also note that no quotes are needed in this case.

PHP works with both single quotes and double quotes. However, they both function different in regards to variables. Variables within double quotes will be replaced with their contents. Variables within single quotes will be turned into "string literals"--that is, they will output exactly as you type them. See below for an example.

Code: Select all

$foo = 'John Doe';
 
echo "My name is $foo.";
//This will output "My name is John Doe."
 
echo 'My name is $foo.';
//This will output "My name is $foo."
Also read up on concatenation: http://www.phpf1.com/tutorial/php-strin ... ation.html