Jump to content

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'

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.