kaze Posted November 8, 2006 Share Posted November 8, 2006 I've got a function that contains just-$arra[$c]=$x;$c=$c+q;$x is coming from another function which passes through here a lot and I want it to add whatever x is at the time to the array...Except it doesn't do that.Test echos hidden in there show that x does have values when it passes through but the arrays values just won't change.What am I missing? Quote Link to comment https://forums.phpfreaks.com/topic/26553-why-wont-the-values-change-should-be-easy/ Share on other sites More sharing options...
onlyican Posted November 8, 2006 Share Posted November 8, 2006 if that is inside a function, you need to parse the X value accrossEither something like[code]<?php$x = 5;function myfunction(){global $x;$arra[$c]=$x;$c=$c+q;}?>[/code]OR[code]<?php$x = 5;function myfunction($x){$arra[$c]=$x;$c=$c+q;}myfunction($x);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/26553-why-wont-the-values-change-should-be-easy/#findComment-121463 Share on other sites More sharing options...
pnj Posted November 8, 2006 Share Posted November 8, 2006 It's not just $x that's invisible in your function, it's all your variables. If you don't specify them as globals or pass them as references, you're creating local variables that go out of scope as soon as the function ends.Try this:[code]function myfunction() { global $x, $arra, $c, $q; $arra[$c] = $x; $c += $q;}[/code]Better yet, pass them all as reference parameters:[code]function myfunction(&$x, &$c, &$arra, &$q) { $arra[$c] = $x; $c += $q;}[/code]if q is a constant and not a variable, you don't need to pass it or declare it as a global Quote Link to comment https://forums.phpfreaks.com/topic/26553-why-wont-the-values-change-should-be-easy/#findComment-121469 Share on other sites More sharing options...
kaze Posted November 9, 2006 Author Share Posted November 9, 2006 Gragh, typo. Was $c + 1 not q...To do it in full;function thisisit($x){$arra[$c]=$x;$c=$c+1;}and elsewhere is thisisit($val); Quote Link to comment https://forums.phpfreaks.com/topic/26553-why-wont-the-values-change-should-be-easy/#findComment-122042 Share on other sites More sharing options...
onlyican Posted November 9, 2006 Share Posted November 9, 2006 [quote author=kaze link=topic=114245.msg465370#msg465370 date=1163069978]Gragh, typo. Was $c + 1 not q...To do it in full;function thisisit($x){$arra[$c]=$x;$c=$c+1;}and elsewhere is thisisit($val);[/quote]You want something like this[code]<?phpfunction thisisit($x, $c){global $arra;$arra[$c] = $x;$c = $c +1;return $c;}$function_result = thisisit($x, $c);?>[/code]Note I used global for the array, as I find it easier when working with arrays Quote Link to comment https://forums.phpfreaks.com/topic/26553-why-wont-the-values-change-should-be-easy/#findComment-122090 Share on other sites More sharing options...
HuggieBear Posted November 9, 2006 Share Posted November 9, 2006 Where you have [code=php:0]$c = $c +1;[/code] you can use an [url=http://uk.php.net/manual/en/language.operators.increment.php]increment operator[/url] to do away with this line completely, so your code will look like this:[code]<?phpfunction thisisit($x, $c){global $arra;$arra[$c++] = $x;return $c;}$function_result = thisisit($x, $c);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/26553-why-wont-the-values-change-should-be-easy/#findComment-122096 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.