IT Community - Software Programming, Web Development and Technical Support

How to define a global variable in PHP?

This is a discussion on How to define a global variable in PHP? within the PHP Programming forums, part of the Web Development category; Hi all... How to define a global variable in PHP? Thanks......


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 10-25-2007, 03:27 AM
itbarota itbarota is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 547
itbarota is on a distinguished road
Default How to define a global variable in PHP?

Hi all...


How to define a global variable in PHP?

Thanks...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-25-2007, 03:29 AM
Pvinothkumar Pvinothkumar is offline
D-Web Analyst
 
Join Date: Sep 2007
Posts: 237
Pvinothkumar is on a distinguished road
Default Re: How to define a global variable in PHP?

Hi all...


If you want variables defined out side the function to be accessed inside the function scope then you have make them global inside the fuction. You can use the global term.


<?php

$integer_var = 3;

function print_var(){
global $integer_var;

// echos 3 on the screen
echo $integer_var;
}

print_var();

?>

Thanks...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 11-01-2007, 07:20 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: How to define a global variable in PHP?

Hi,

You can use also $GLOBALS instead of global.

For eg:
<?php
$a = 1;
$b = 2;

function Sum(){
$GLOBALS['c'] = $GLOBALS['a'] + $GLOBALS['b'];
}

Sum();
echo $c;
?>

I think this method very useful for some times.

Thanks & Regards,
R.Kamalakannan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 11-01-2007, 10:58 AM
Venkat Venkat is offline
D-Web Master
 
Join Date: Mar 2007
Posts: 350
Venkat is on a distinguished road
Thumbs up Re: How to define a global variable in PHP?

Hi,

The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well. For example:

First, an example use of global:

<?php
$a = 1;
$b = 2;

function Sum()
{
global $a, $b;

$b = $a + $b;
}

Sum();
echo $b;
?>
__________________
Venkat
knowledge is Power
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to define a static variables in php. itbarota PHP Programming 1 11-10-2007 04:40 AM
Define Quality Control itbarota Software Testing 2 07-24-2007 12:37 AM
Define Qos vadivelanvaidyanathan Server Management 1 07-16-2007 09:36 AM
Static Global Variable and Static Local Variable vigneshgets C and C++ Programming 1 05-30-2007 11:50 AM
Please Define the Terms djree General Web hosting Discussions 2 03-13-2007 07:23 AM


All times are GMT -7. The time now is 06:54 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0