PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 15th, 2004, 06:04 AM
bah26 bah26 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 18 bah26 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 29 sec
Reputation Power: 0
Variable scope....

(I'm not sure how to explain this, so I'll give an example!)

PHP Code:
<?php 
 
$globalVariable 
'blah';
 
function 
test()
{
     echo 
$globalVariable//output 'blah'
}
?>


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

Reply With Quote
  #2  
Old July 15th, 2004, 07:32 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
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.

Reply With Quote
  #3  
Old July 15th, 2004, 11:07 PM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 788 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 7
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.

Reply With Quote
  #4  
Old July 16th, 2004, 03:10 AM
bah26 bah26 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 18 bah26 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 29 sec
Reputation Power: 0
Quote:
Originally Posted by dhouston
It's typically considered better practice not to use global variables. Why not just pass the variable as a parameter to the function?

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

Reply With Quote
  #5  
Old July 16th, 2004, 05:53 AM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 788 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 7
Here is another way

PHP Code:
<?php

if (!isset($GLOBALS['DEBUG'])) { $GLOBALS['DEBUG'] = true; } 

function 
ErrorHandler()
{

     if ( (isset(
$GLOBALS['DEBUG'])) ) {
        
printf('$DEBUG = %s'."\n"$GLOBALS['DEBUG'] ? 'true' 'false');
     } else {
        print 
'$DEBUG does not exist in this scope'."\n";
     }

}

ErrorHandler();

?>

Reply With Quote
  #6  
Old July 16th, 2004, 03:13 PM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 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:
<?php

$globalVariable 
'blah';

function 
test()
{
     global 
$globalVariable;
     echo 
$globalVariable//output 'blah'
}
?>

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > Variable scope....


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT