Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with globals

Status
Not open for further replies.

michelleqw

Programmer
Joined
Jan 4, 2004
Messages
120
Location
DE
Dear PHP users,

We make in the beginning of our code a variable constant $cnt. In function doing() we increment this variable but when we echo after de call function the variable the variable it is still zero!

code:

<?php
global $cnt ;
$cnt = 0 ;
doing() ;
echo "second $cnt<br>" ;


function doing()
{
$cnt++ ;
echo "first: $cnt<br>" ;
}
?>

Can someone tell us what we do wrong?


Nice regards,

Michelle.
 
you need to move the global $cnt into the doing function

--BB
 
Like BB101 posted, declare the variable 'global' inside the function.
something like

Code:
<?php
    $cnt = 0    ;
    doing() ;
    echo "second $cnt<br>"    ;


    function doing()
    {
	global $cnt ;
        $cnt++  ;
        echo "first: $cnt<br>"    ;
    }
?>

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top