|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
On my news page I pulled some repetative code out and made it into a seperate function. I made no change to the code other then that.
If the user is logged in it is supposed to show on news articles the option to edit or remove the article. However it is not doing that anymore. The function that retrieves the users level seems not to be getting the level here, however it is working correctly when called by template2.php. I can not figure out why it is not working. The problem is at line 84 in news.php I would appreciate any and all help
__________________
CHornJr "One day I'll know what I am doing" ![]() My Blog Suanhacky Lodge #49 Rebel Squadrons |
|
#2
|
||||
|
||||
|
Instead of using globals, try returning values. For testing, at any rate, try saying doing $level=checklogin() and, in the function, returning the value you're currently putting in $level.
Does it set the name properly? |
|
#3
|
||||
|
||||
|
I did keep it as a global variable in addition to having it return $level. But I must now ask; Why did I have to set eh function to return $level?
|
|
#4
|
||||
|
||||
|
I just offered it as an option. You can usually use globals, though it's not the best idea. Ideally, you should strive to keep your variables scoped, preferably within objects, etc. With global variables, you risk having functions with overlapping names screw up other functionality, and if you're developing in an environment with multiple developers, it can be a nightmare. It's typically better to pass a function some inputs, work with them within the scope of the function, and return outputs for use within the parent scope. This keeps things tidy and makes it harder for namespace issues to cause pesky hard-to-trace bugs.
Actually, I just took another quick look at your code and I think I know why $level wasn't working. In your news.php, you don't identify $level as a global variable, so it doesn't adopt the value you assign it in your checklogin() function. I'll bet that if you declare $level global in news.php and remove the return functionality, it'll work. Still, it tends to be cleaner and less problematic and more maintenance-friendly to pass inputs to and return outputs from your functions. |
|
#5
|
||||
|
||||
|
thank you
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > FUnction not working |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|