This is a discussion on DEFINE and GLOBAL variables in PHP within the PHP Programming forums, part of the Web Development category; Hi All, What is the exact roll of DEFINE and GLOBAL variable in PHP?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Hi, define - Defines a named constant. Global - An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array. This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. You don't need to do a global $GLOBALS; to access it within functions or methods.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| Example for Defining Constants PHP Code:
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| Here another example PHP Code:
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| defined function used to check whether a given named constant exists PHP Code:
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| get_defined_constants -- Returns an associative array with the names of all the constants and their values PHP Code: Code: Array
(
[E_ERROR] => 1
[E_WARNING] => 2
[E_PARSE] => 4
[E_NOTICE] => 8
[E_CORE_ERROR] => 16
[E_CORE_WARNING] => 32
[E_COMPILE_ERROR] => 64
[E_COMPILE_WARNING] => 128
[E_USER_ERROR] => 256
[E_USER_WARNING] => 512
[E_USER_NOTICE] => 1024
[E_ALL] => 2047
[TRUE] => 1
)
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| get_defined_functions PHP Code: Code: Array
(
[internal] => Array
(
[0] => zend_version
[1] => func_num_args
[2] => func_get_arg
[3] => func_get_args
[4] => strlen
[5] => strcmp
[6] => strncmp
...
[750] => bcscale
[751] => bccomp
)
[user] => Array
(
[0] => myrow
)
)
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| Whenever you're developing a new large-scale PHP script, you're bound to use global variables, since some data needs to be used by multiple parts of your script. Good examples of global data are script settings, database connections, user credentials and more. There are many ways of making this data global, but the most commonly used way is to use the global keyword, which we will explore later on in this article.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| The only problem with the global keyword, and any global data for that matter, is that's it's actually a very bad way of programming, and usually tends to lead to bigger problems later on. The reason for this is that global data ties all the separate parts of your script together, and often if you make a change in one part, something else will break in another part. If you have more than a few global variables, you're whole script will eventually become a big kludge of unmaintainable code.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| By default PHP defines a few variables called Superglobals which are automatically global, and can be accessed anywhere, like the $_GET or $_REQUEST variables. These are mainly used to retrieve form data or other outside data, and there's no real harm in using these variables, since they should never be written to anyway.
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| $a = 1; /* global scope */ function Test() { echo $a; /* reference to local scope variable */ } Test(); the value of a in full program will be 1 because that is declare as global scope. |
| |||
| global $a = 1; /* global scope */ function Test() { echo $a; /* reference to local scope variable */ } Test(); now this willl work Web Design Company | Asp.Net Programmers | web designers Delhi |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |
| How to define a global variable in PHP? | itbarota | PHP Programming | 3 | 11-01-2007 10:58 AM |
| Why are there no global variables in Java? | prasath | Java Programming | 1 | 07-19-2007 04:39 AM |
| Define Qos | vadivelanvaidyanathan | Server Management | 1 | 07-16-2007 09:36 AM |
| global and local variables | vigneshgets | Perl | 1 | 07-12-2007 10:35 PM |