xProteuSx Posted December 18, 2012 Share Posted December 18, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/272121-looping-function-working-but-not-really/ Share on other sites More sharing options...
BrettHartel Posted December 18, 2012 Share Posted December 18, 2012 (edited) Because of $days = $days + 1; Since $days was originally 0, you changed it to 0+1 so in the end you have $Days = 1 in the end. * I think, I am still learning php Edited December 18, 2012 by BrettHartel Quote Link to comment https://forums.phpfreaks.com/topic/272121-looping-function-working-but-not-really/#findComment-1399990 Share on other sites More sharing options...
xProteuSx Posted December 18, 2012 Author Share Posted December 18, 2012 $days is originally = 1. $days +1 is only executed if if ($days < 0). Quote Link to comment https://forums.phpfreaks.com/topic/272121-looping-function-working-but-not-really/#findComment-1399992 Share on other sites More sharing options...
BrettHartel Posted December 18, 2012 Share Posted December 18, 2012 Or there could be another reason Sometimes variables inside functions are only for that function. So when you echo the $Day after the function, it just returns the original string of 1. Quote Link to comment https://forums.phpfreaks.com/topic/272121-looping-function-working-but-not-really/#findComment-1399993 Share on other sites More sharing options...
xProteuSx Posted December 18, 2012 Author Share Posted December 18, 2012 Sorry Brett, but you're wrong. $days = $days + 1 only occurs if $days ever drops below a value of 0, which it does not. Also, this does not address the other values. Quote Link to comment https://forums.phpfreaks.com/topic/272121-looping-function-working-but-not-really/#findComment-1399994 Share on other sites More sharing options...
Pikachu2000 Posted December 18, 2012 Share Posted December 18, 2012 What you're doing is echoing the original values after the function. If you want access to the values, you should return them from the function in an array. Quote Link to comment https://forums.phpfreaks.com/topic/272121-looping-function-working-but-not-really/#findComment-1399995 Share on other sites More sharing options...
xProteuSx Posted December 18, 2012 Author Share Posted December 18, 2012 Pikachu2000, Why is the code within the 'else if' statements not updating the original values? This is confusing for me, and will cause me further headaches if I don't understand. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/272121-looping-function-working-but-not-really/#findComment-1399996 Share on other sites More sharing options...
xProteuSx Posted December 18, 2012 Author Share Posted December 18, 2012 I am having a hell of a time returning the proper array values, because the function executes itself from within itself. Any pointers? Quote Link to comment https://forums.phpfreaks.com/topic/272121-looping-function-working-but-not-really/#findComment-1399999 Share on other sites More sharing options...
Pikachu2000 Posted December 18, 2012 Share Posted December 18, 2012 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; Quote Link to comment https://forums.phpfreaks.com/topic/272121-looping-function-working-but-not-really/#findComment-1400001 Share on other sites More sharing options...
xProteuSx Posted December 18, 2012 Author Share Posted December 18, 2012 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' Quote Link to comment https://forums.phpfreaks.com/topic/272121-looping-function-working-but-not-really/#findComment-1400007 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.