Change variable with a function

This is a discussion on Change variable with a function within the PHP General forums, part of the PHP Programming Forums category; Hello, I want to change a variable in a function. First I set a variable '$var1' to 'NO'. Then I ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-13-2005
Bertman
 
Posts: n/a
Default Change variable with a function

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.

Bert


Reply With Quote
  #2 (permalink)  
Old 05-13-2005
Ken Robinson
 
Posts: n/a
Default Re: Change variable with a function


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

Reply With Quote
  #3 (permalink)  
Old 05-13-2005
Bertman
 
Posts: n/a
Default Re: Change variable with a function

Yeah great it works :D

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


"Ken Robinson" <kenrbnsn@rbnsn.com> schreef in bericht
news:1116002328.153250.318100@f14g2000cwb.googlegr oups.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
>



Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:49 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0