TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   when would i use a "variable variable" (http://www.talkphp.com/general/2717-when-would-i-use-variable-variable.html)

sarmenhb 04-27-2008 06:29 PM

when would i use a "variable variable"
 
by a variable variable(dynamic Variable) i mean

Code:

<?php

$field = "firstname_field";
$$field = "sarmenhb";

echo "Field Name: ".$field."<br>";
echo "Field Value: ".$$field."<br>";
?>

have you ever used this in your code? if so can you explain how you used it?

thanks

freenity 04-27-2008 07:43 PM

check this, should help you to understant:

PHP Code:

<?php
$a 
'b';

$b 2;
$
$a 55;

var_dump($b);

The output is: 55

TlcAndres 04-27-2008 08:05 PM

It's also something of a bit of help when your doing basic skinning for example

PHP Code:


function load($file,$array){
  foreach(
$array as $key => $val){
    ${
$key} = $val;
  }
  require_once(
$file);


with that you do something like this

PHP Code:

$skinCons = array('title'=>'Production Site','content'=>'test content of stuff');
load('skin.php',$skinCons); 

the skin file would look like
PHP Code:

<html>
<head>
<title><?php echo($title);?></title>
</head>
<body>
<?php echo($body); ?>
</body>
</html>


blayne4k 04-28-2008 03:42 PM

Almost all of the variable variables ive seen were inside of loops or loops inside of loops, things like that need to be able to generate dynamic variables and still know what they are, although most of the time I just use arrays, they work just as good and its easier to output them just with a print_r


All times are GMT. The time now is 04:46 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0