pernest Posted October 6, 2009 Share Posted October 6, 2009 I come to php fresh from a background of C and C++, and I would like to know any tips people have with scoping, and how I should think when writing code. When introducing a temporary variable within a loop, I'm finding it maddening to have to search through my entire script, to ensure that I have not already used it, and that its use won't mess up another bit of code. I'd appreciate it if someone could explain why scoping has been implemented in this way in php, I don't see the advantage. Quote Link to comment https://forums.phpfreaks.com/topic/176769-solved-scoping-and-php/ Share on other sites More sharing options...
mikesta707 Posted October 7, 2009 Share Posted October 7, 2009 Its because the makers of PHP personally hate you and only you. they knew you would have a background in C/C++ and wanted to piss you off. But really, the specific "why" can be attained by asking the developers themselves. But , in addition to the added annoyance of making sure you don't use loop variables over again (I don't really run into this problem much, since I usually use $i for for loops, since $i isn't a good variable name for anything but loop iterators (except for maybe the character 'i'), it allows you the flexibility of being able to use the loop variable outside the loop, (for god knows what, but i'm sure there is a case this might be useful) Also, remember, PHP doesn't really have a main function, so all variables defined outside of a function have a scope that is accessible throughout the entire script. its kind of like python, javascript, and a few other languages in that respect for scope, as far as functions and classes go, its pretty much the same as C++. a tip I can give for you for loop variables, is to use names that you would only use for loops. For example, i, x, y, etc. this usually helps me. Quote Link to comment https://forums.phpfreaks.com/topic/176769-solved-scoping-and-php/#findComment-932037 Share on other sites More sharing options...
.josh Posted October 7, 2009 Share Posted October 7, 2009 I don't really see the disadvantage. It's more a matter of discipline than anything. If you are at the point of not knowing whether you used a variable in your script somewhere before, then I think the real issue is that you and/or your script aren't/isn't very organized. The only reason you should "forget" a variable is if it's outside of scope in the first place (abstracted into a function or class). Nonetheless, as mike mostly pointed out, if you make it a practice to have a naming convention for variables you use for whatever purpose, that should reduce/illuminate debugging on that count. IOW if you make it a practice to only use $c, $x, $count, etc.. as loop or counting variables: a) since they are usually counting or temp vars, their previous values shouldn't matter anyways, so you can just initialize/reset them to xyz before the loop. b) know that even if you don't do a), the only other place you would have used it was in some other loop. c) ...I got nothin'...but a) and b) are good enough. Point is, you ask what the advantage is, but I don't really see a disadvantage, especially when you practice a) and b), as they are good things to practice anyways. Quote Link to comment https://forums.phpfreaks.com/topic/176769-solved-scoping-and-php/#findComment-932045 Share on other sites More sharing options...
pernest Posted October 7, 2009 Author Share Posted October 7, 2009 Thanks for your pointers, I guess because I'm used to C++, I'm expecting more out of php than its capable of giving. I suppose you can only have relatively small scripts before things start to get out of hand. I still can't see a any advantages of having the job of scoping done by a fallible programmer, rather then enshrined in the language itself. Thanks again Paul Quote Link to comment https://forums.phpfreaks.com/topic/176769-solved-scoping-and-php/#findComment-932450 Share on other sites More sharing options...
.josh Posted October 7, 2009 Share Posted October 7, 2009 Well first off, keep in mind, php is not c++. It is not capable of doing anywhere near the amount of stuff as c++. 2nd, scripts "getting out of hand" after it gets to be a certain length is kind of stretching it, considering we are just talking about temporary vars being used in loops...php has plenty of scope and namespacing for virtually everything else. Quote Link to comment https://forums.phpfreaks.com/topic/176769-solved-scoping-and-php/#findComment-932460 Share on other sites More sharing options...
pernest Posted October 7, 2009 Author Share Posted October 7, 2009 Hi I just wanted to apologise if my posts have cause offence. I was just getting frustrated debugging runtime errors that were caused by scoping issues. I really appreciate all the help people on this forum have given me. Thanks Paul Quote Link to comment https://forums.phpfreaks.com/topic/176769-solved-scoping-and-php/#findComment-932576 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.