|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Variable scope....
(I'm not sure how to explain this, so I'll give an example!)
PHP Code:
I know it is possible to declare $globalVariable global within the function, however, this doesn't seem like a particularly great solution.... Is there a way to achieve this?? Cheers, Ben |
|
#2
|
||||
|
||||
|
It's typically considered better practice not to use global variables. Why not just pass the variable as a parameter to the function?
__________________
Please don't PM me asking for solutions outside the scope of a thread. Keeping all responses in a thread stands to help others who come along later, which is after all what this forum's all about. |
|
#3
|
|||
|
|||
|
A better way
Code:
<?php
if (!defined('DEBUG')) { define('DEBUG', true); }
function ErrorHandler()
{
if ( (defined('DEBUG')) && (true == DEBUG) ) {
//output debug info
}
}
?>
__________________
__________________________________________________ _ Wil Moore III, MCP | Integrations Specialist | Senior Consultant Are You Listed...? | DigitallySmooth Inc. |
|
#4
|
|||
|
|||
|
Quote:
It just seems a bit of a pain! (/me being lazy!) laidbak's solution seems resonable, and I have taken that approaches for other tasks, I was just wondering if it was possible to define a variable as global. Cheers, Ben |
|
#5
|
|||
|
|||
|
Here is another way
PHP Code:
|
|
#6
|
|||
|
|||
|
You can declare a variable as global in a backwards kind of way. You use the keyword global inside a function and it makes the variable available in that functions scope.
eg. PHP Code:
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > Variable scope.... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|