How to parse a variable withing quotes ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nilaymedh
Forum Newbie
Posts: 2
Joined: Thu Oct 01, 2009 12:47 pm

How to parse a variable withing quotes ?

Post 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
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: How to parse a variable withing quotes ?

Post by jackpf »

nilaymedh
Forum Newbie
Posts: 2
Joined: Thu Oct 01, 2009 12:47 pm

Re: How to parse a variable withing quotes ?

Post 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 !
Griven
Forum Contributor
Posts: 165
Joined: Sat May 09, 2009 8:23 pm

Re: How to parse a variable withing quotes ?

Post 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
Post Reply