Jump to content

Looping Function Working But Not Really


xProteuSx

Recommended Posts

I have some code, such as this:

 

$days = 1;

$hours = -2;

$minutes = -39;

$seconds = 14;

 

 

function fixTime($days,$hours,$minutes,$seconds)

{

if (($seconds < 0) || ($minutes < 0) || ($hours < 0) || ($days <0))

{

echo 'running<br />';

if ($seconds < 0)

{

echo 'seconds: ' . $days . ',' . $hours . ',' . $minutes . ',' . $seconds . '<br />';

$seconds = $seconds + 60;

$minutes = $minutes - 1;

fixTime($days,$hours,$minutes,$seconds);

}

else if ($minutes < 0)

{

echo 'minutes: ' . $days . ',' . $hours . ',' . $minutes . ',' . $seconds . '<br />';

$minutes = $minutes + 60;

$hours = $hours - 1;

echo 'minutes: ' . $days . ',' . $hours . ',' . $minutes . ',' . $seconds . '<br />';

fixTime($days,$hours,$minutes,$seconds);

 

}

else if ($hours < 0)

{

echo 'hours: ' . $days . ',' . $hours . ',' . $minutes . ',' . $seconds . '<br />';

$hours = $hours + 24;

$days = $days - 1;

echo 'hours: ' . $days . ',' . $hours . ',' . $minutes . ',' . $seconds . '<br />';

fixTime($days,$hours,$minutes,$seconds);

}

else if ($days < 0)

{

echo 'days: ' . $days . ',' . $hours . ',' . $minutes . ',' . $seconds . '<br />';

$hours = $hours - 24;

$days = $days + 1;

fixTime($days,$hours,$minutes,$seconds);

}

else

{}

}

}

 

fixTime($days,$hours,$minutes,$seconds);

 

echo '<br />' . $days . '<br />';

echo $hours . '<br />';

echo $minutes . '<br />';

echo $seconds . '<br /><br />';

 

The idea is for this to return:

 

0

21

21

14

 

0 = days

21 = hours

21 = minutes

14 = seconds

 

At present it is returning:

 

running

minutes: 1,-2,-39,14

minutes: 1,-3,21,14

running

hours: 1,-3,21,14

hours: 0,21,21,14 <---------------------- correct values!

 

1 <---------------------- incorrect values again!

-2

-39

14

 

But as you can see, it reverts back to the original values! What the heck am I doing wrong? Thanks :)

Link to comment
https://forums.phpfreaks.com/topic/272121-looping-function-working-but-not-really/
Share on other sites

It's called "variable scope" Variables created within a function are local to the function, and not available outside of the function, and vice versa, even if they have the same name. The exception is superglobals such as $_GET/$_POST/$_SESSION/$_COOKIE, etc. Those are available anywhere.

 

Run this code and it should make more sense.

$text = 'External';

function ECHO_TEXT($text) {
echo "The value of text is: $text<br>";
}

ECHO_TEXT('Internal');

echo $text;

Pikachu2000,

 

I do understand variable scope, but I didn't know that it differed within a function. I have declared a variable outside of the function, so I thought that I could manipulate it within the function, then be able to echo it outside of the function again, like this:

 

$text = 'Hello World';

 

function changeText()

{$text = 'Bye World';}

 

echo $text;

 

//output should be 'Bye World'

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.