You are here: Re: variable « PHP Programming Language « IT news, forums, messages
Re: variable

Posted by Michael Fesser on 09/16/07 21:15

..oO(Sanders Kaufman)

>I think that what's confusing me is that double-dollar thing.
>I've seen it before, but thought it was a typo.
>
>Could you tell me what it means, or where to look in the docs to find
>out about it.

It's called a variable variable. As said - instead of accessing a
variable directly, it's done by taking the name of the variable from
_another_ string variable:

$foo = 42;
print $foo; // prints 42
$bar = 'foo';
print ${$bar}; // prints 42, too

The latter accesses a variable whose name is stored in another variable.

http://www.php.net/manual/en/language.variables.variable.php

Usuallly it's considered bad style to use variable variables, in most
cases there's a better way. But there's a similar thing that can be
really helpful - variable class names, i.e. the class name is stored in
a variable:

$foo = 'TMyClass';
$bar = new $foo(); // creates an instance of the class TMyClass

Micha

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация