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

Posted by Rik on 04/24/07 19:04

Christoph Burschka wrote:
> Christoph Burschka schrieb:
>> Boris Savc schrieb:
>>
>>> Sorry for confuzing subject, but that's the thing I'm trying to
>>> achieve. For example:
>>>
>>> $a = "Hello";
>>> $b = 1;
>>>
>>> I want to change the name of $a to $a1 where 1 is value of variable $b!
>>>
>>> Thanks for all the help,
>>> Boris
>>>
>>
>> A brief note: Something tells me you should take a programming class
>> in a real language. PHP lets you do things no sane programmer would,
>> so if you get used to, say, Java first, you won't want to "rename
>> variables" or use strings as variable names, but follow good practices
>> and use, for example, arrays.
>>
>> But since you're asking, and since it's possible:
>>
>>
>> Code:
>>
>> $a = "Hello";
>> $b = 1;
>> $newname="$a$b";
>> $$newname=$a;
>> print $a1;
>> --------------------------
>>
>> Will output:
>>
>> Hello

>
> Correction: $newname="$a$b"; is wrong, that would make the new variable
> $Hello1. It should, of course, be $newname="a$b" (no $ before a), so the
> new variable will be $a1.


Well, simpler would be:
${'a'.$b} = 'bar'; //(or .. = $a in this case)

Something tells me the OP should use an array though....
$a = 'foo';
$b = 1;
$c[$b] = $a; //allthough you really should initialise it with $c =
array(); or $c = array( $b => $a);

Don't try to use $a[$b] = $a; though, that obviously will result in $a
== 'ffo';

Keep in mind that the naming of variables has absolutely nothing to do
with your program, so going out of your way to name that in some scheme
is often wasted time. The only thing they do is help you and your fellow
coders to identity their meaning. PHP itself could care less.

--
Rik Wasmus

 

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

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