Dalton Conley
Dalton Conley

Reputation: 1615

How to access a variable variable with a name containing spaces?

How does PHP handle something like this:

$blah = "Testing a variable"; 
$$blah = "test"; 

What would I access my dynamically declared variable?

Upvotes: 0

Views: 234

Answers (4)

DampeS8N
DampeS8N

Reputation: 3621

Not really an answer, but...

<?php 
function I_love_you()
{
    return "haha";
}

$haha = "HoHoHo";
$tom = "I_love_you";
$blah = "tom";

echo ${$$blah()};
?>

Upvotes: 0

webbiedave
webbiedave

Reputation: 48897

echo ${'Testing a variable'};

However, you don't want to do this in practice. It makes for unmaintainable, bug-prone code.

Upvotes: 4

zsalzbank
zsalzbank

Reputation: 9857

The variable $blah must contain a valid variable name.

This will tell you about variables: http://www.php.net/manual/en/language.variables.basics.php

Upvotes: 1

Rafe Kettler
Rafe Kettler

Reputation: 76945

Everything you need to know about variable variables at http://www.php.net/manual/en/language.variables.variable.php, except for one thing: don't use them.

Upvotes: 9

Related Questions