Handy PHP
Earn More with LinkConnector
Forum
Welcome, Guest
Please Login or Register.    Lost Password?
Get variable value out of a string
(1 viewing) 1 Guest
Go to bottom
TOPIC: Get variable value out of a string
#345
pipesportugal
Fresh Boarder
Posts: 2
graphgraph
User Offline Click here to see the profile of this user
Get variable value out of a string 1 Year, 10 Months ago Karma: 0
Hello dear colleagues from the talkphp forum,

I would very much appreciate some help regarding the following subject:

CODE

$variable1 = "Hello"; 
$title = '$variable'."1"; 
echo $title;  


This routine is returning the result:
$variable1

I would like to get the value "Hello" instead.

Can someone explain to me which php function can I use to retrieve the result that I am looking for ?

Thanks in advance,
pipesportugal
 
Logged Logged
  The administrator has disabled public write access.
#346
vujsa
Moderator
Posts: 143
graph
User Offline Click here to see the profile of this user
Re:Get variable value out of a string 1 Year, 9 Months ago Karma: 5
This is a simple enough issue to fix...

When you use single quotes ( ' ), PHP reads the string literally!
If you use double quotes ( " ), PHP tries to parse variables and other information contained in the string.
I see that you use concatenation (using the period "." to connect two or more strings) which can be used like so:
PHP

$variable1 = "Hello";
$title = $variable . "1";
echo $title;


Also, since you don't have anything special in your $title definition second part ,you can also do this:
PHP

$variable1 = "Hello";
$title = $variable . '1';
echo $title;


Really, the variable doesn't need to be in any quotes unless it is in the middle of a string and then it has to be in double quotes to work but closing a string then using concatenation to add the variable to the string then use concatenation again before opening your quotes again to continue your string definition like so:
PHP

$input = 'first';
$output = 'This is me string which is the ' . $insert . ' string in the code.'; 

would look like this:
This is me string which is the first string in the code.

There are other ways to do what you are wanting including string replacement or regular expression replacement.

vujsa
 
Logged Logged
 
Last Edit: 2009/10/01 22:09 By vujsa.
 
Do not post small or one line posts.
Do not post unsolicited links or any referal links.
Do not post copied content.
Do not use foul language.
Always be respectful to the other posters.
  The administrator has disabled public write access.
#347
pipesportugal
Fresh Boarder
Posts: 2
graphgraph
User Offline Click here to see the profile of this user
Re:Get variable value out of a string 1 Year, 9 Months ago Karma: 0
Hi,

What I really was looking for was this solution:

echo $$title;

please notice the $$

Thanks for helping,
pipesportugal
 
Logged Logged
  The administrator has disabled public write access.
#348
vujsa
Moderator
Posts: 143
graph
User Offline Click here to see the profile of this user
Re:Get variable value out of a string 1 Year, 9 Months ago Karma: 5
I think this will help:
CODE

$title = "hello";
echo "\$$title";

Would print:
$hello

You can use the backslash (\) character to escape a single character. Or the following would also work:
CODE

$title = "hello";
echo '$' . "$title";

This is because the single quote tells PHP not to look for any variables in the string.

vujsa
 
Logged Logged
 
Do not post small or one line posts.
Do not post unsolicited links or any referal links.
Do not post copied content.
Do not use foul language.
Always be respectful to the other posters.
  The administrator has disabled public write access.
Go to top
Earn More with LinkConnector