You are here: Re: Change variable with a function « PHP « IT news, forums, messages
Re: Change variable with a function

Posted by Bertman on 05/13/05 20:32

Yeah great it works :D

function setvar() {
global $var1;
$var1 = 'YES';
}


"Ken Robinson" <kenrbnsn@rbnsn.com> schreef in bericht
news:1116002328.153250.318100@f14g2000cwb.googlegroups.com...
>
> Bertman wrote:
>> Hello,
>>
>> I want to change a variable in a function.
>> First I set a variable '$var1' to 'NO'.
>> Then I call the function, within the function he have to set the
> variable to
>> 'YES'
>> Then I say: echo $var1;
>> $var1 is still 'NO' :S
>>
>> <?PHP
>> $var1 = 'NO';
>>
>> setvar();
>> echo $var1;
>>
>> function setvar() {
>> $var1 = 'YES';
>> }
>> ?>
>>
>> Can a function set a variable outsite the function ?
>> BTW, this is a little example script.
>
> To do it they way you have it in your example, you need to declate
> $var1 as a global inside the function.
>
> function setvar() {
> global $var1;
> $var1 = 'YES';
> }
>
> The other way would be to have the function return a value:
>
> $var1 = setvar();
>
> function setvar() {
> return('YES');
> }
>
> Ken
>

 

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

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