/    Sign up×
Community /Pin to ProfileBookmark

Convert a string to a variable name

[CODE]$name = “nice day!”;
$wish = “$”.”name”;

echo $name;
echo “<br>”;
echo $wish;
[/CODE]

Output:
[B]nice day!
$name[/B]

I wish that [B]$wish[/B] is “[B]nice day![/B]“.

I am doing database, there are many similar strings, such as:

INSERT INTO some_table
(id, name, price, …..) //field sting
VALUES
($id, $name, $price, …..). //value string

If I can convert a string to variable name, I also can convert field string to value string.

Is it possible? — hope you understand what I mean.

Thanks.

.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 19.2012 — [url=http://us3.php.net/manual/en/language.variables.variable.php]variable variables[/url]

(Although most of the time I think arrays provide a better solution.)
Copy linkTweet thisAlerts:
@html20009876authorJun 19.2012 — Hi NogDog,

It is useful, I will try to see if it is suitable for my need.

Thanks.
Copy linkTweet thisAlerts:
@NogDogJun 19.2012 — You can also use curly braces (complex variable notation) to do some tricks:
[code=php]
<?php
$foo = 'fubar';
$f = 'f';
$o = 'o';
$bar = ${$f.$o.$o};
echo $bar;
[/code]
Copy linkTweet thisAlerts:
@JUANCHO328Sep 19.2018 — Thanks for this article.
Copy linkTweet thisAlerts:
@rootSep 19.2018 — {"locked":true}
Copy linkTweet thisAlerts:
@rootSep 19.2018 — Please don't resurrect old posts, thanks.
×

Success!

Help @html20009876 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.22,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...