/    Sign up×
Community /Pin to ProfileBookmark

How to call a function if I know its name from a content of a string only

Hello. Suppose I know name of a function in php that I have saved in a string. What can I do to call the function based on the knowledge of the string only? (I have met the same problem years ago when studying Pascal and there was a solution)

[code]
<!DOCTYPE html>
<html lang=”en”>
<head>
</head>
<body>

<?php

function treasureLocation(){
echo ‘There is a hidden treasure here: 49.44131895111605, 11.052444369347798.’;
}

$myString=’treasureLocation’;

/* Now what can I do? */

?>

</body>
</html>
[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 12.2021 — Sounds like PHP [u][variable functions](https://www.php.net/manual/en/functions.variable-functions.php)[/u] is what you're looking for. Just stick some () after the variable, _et voila_.
Copy linkTweet thisAlerts:
@codewitchauthorJul 12.2021 — Whaw, thank you NogDog!

<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php

<i> </i>function treasureLocation(){
<i> </i> echo 'There is a hidden treasure here: 49.44131895111605, 11.052444369347798.';
<i> </i>}

<i> </i>$myString='treasureLocation';

<i> </i>$myString();

?&gt;

&lt;/body&gt;
&lt;/html&gt;


_output:_ There is a hidden treasure here: 49.44131895111605, 11.052444369347798.

You just "call a string" and it calls the function. PHP is awesome!
Copy linkTweet thisAlerts:
@NogDogJul 12.2021 — And just to blow your mind a bit more, you could also use "variable variables":

14:39 $ /usr/bin/php -a
Interactive shell

php > function foo() { echo "Ta-da!n"; }
php > $bar = 'foo';
php > $test = 'bar';
php > $$test();
Ta-da!
php >

However, for the sake of anyone who might have to maintain you code, I'd suggest you avoid that whenever possible. ;)
Copy linkTweet thisAlerts:
@codewitchauthorJul 12.2021 — Cool. There is also: variable variable variables:

<!DOCTYPE html>
<html>
<body>

<!-- this program illustrates variable variable variables -->

<?php

function aaa() { echo "I am a cool text."; }
$bbb = 'aaa';
$ccc = 'bbb';
$ddd = 'ccc';
echo $$$ddd();

?>


</body>
</html>


output: I am a cool text.

:)
×

Success!

Help @codewitch 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.19,
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,
)...