Home » , » PHP Tutorials- Variables,Constant

PHP Tutorials- Variables,Constant

Unknown | 8:22 PM | 0 comments
Global Variables
Global variables are set by a PHP script author to assure that a variable and its value will be available inside of a function, or when another PHP file is called to manipulate that variable. When you create custom variables in the normal fashion those variables and their values will not be available to you inside of a function or an externally called file. To make sure they are available you create the variable in the normal way and then apply the following code to make global and available to cover more scope only when needed.



<?php // Define your variable and its value $flavor1 "chocolate"; // Now set it as global global $flavor1; //-------------------- EXTRA INFO ---------------------------------
// You can also set many variables as global together by using:
global $flavor1$flavor2$flavor3; ?>


Here is a code example showing where and why applying global to a variable is handy.

<?php // Define your variable and its value $name1 "Joe"; // Create a simple function that will display a sentence function sampleFunction() {
    
// Set $name1 variable as global
    
global $name1;
    
// Now echo a sentence to the browser
    
echo "Hello $name1, welcome to my  web page.";
}
// This is how you can execute a function to run, we talk more about functions later sampleFunction();
?>
in browser:
Hello Joe, welcome to my web page.


Predefined Variables ( superglobals)
There are a set of predefined variables in PHP that are always available for use in any script or scope in your applications. When data is created, sent, or stored using mechanisms that create superglobal variables, we can then access those variables sitewide or all throughout our scripts and scope. Predefined superglobals are available to all of your scripts that are part of a website or application you are creating.

The superglobal variables are:
$_GET Stores any variables created using the GET method
$_POST Stores any variables created using the POST method
$_REQUEST Stores any variables created through a user input script (it can access both POST or GET)
$_FILES Stores any file upload variables created through user input scripts
$_SESSION Stores any variables created through registering session variables
$_COOKIE Stores any variables created through setcookie
$GLOBALS Stores any variables that have been globally defined
$_SERVER Stores server information such as headers, file names, reference paths, and current page.
$_ENV Stores any variables associated with the server environment.

Here is an example of accessing superglobal variables for the server:

<?php //Obtain user IP address  $ip $_SERVER['REMOTE_ADDR'];  // Obtain browser $browser $_SERVER['HTTP_USER_AGENT']; // Obtain user system language $language $_SERVER['HTTP_ACCEPT_LANGUAGE']; // Obtain the URL of the page that they came from $referingURL $_SERVER['HTTP_REFERER']; // Obtain the page they are currently on $currentPage $_SERVER['REQUEST_URI'];
// Now show all of that information on page echo "$ip <br />";
echo 
"$browser <br />"
echo 
"$language <br />";
echo 
"$referingURL <br />"
echo 
"$currentPage <br />"; ?>


Superglobal variables are created and defined in different ways. We will demonstrate these methods throughout our lessons, we felt that while we are discussing variables we should tell you about all of the different types available to you and your scripts.

Constants and Magic Constants in PHP
Constants are named values whose values cannot be changed. When you create a constant you should use all capital letters and underscores separating words to let yourself and others know they are constants. A dollar sign is not needed in front of the name when creating constants. We use the define() function in PHP to create them.

Here is how we create a constant, and use true on the end to make it not worry about letter casing:

<?php
// define(constant_name, value, case_sensitive=true); define("AUTHOR_NAME""Adam Khoury"true);

echo 
"The person that created this web page is named " AUTHOR_NAME ".";
?>

The person that created this web page is named Adam Khoury.





Magic Constants (predefined)

PHP also has predefined magic constants available for you to use. Here we demonstrate a few:


<?php // __LINE__ Returns the current line number of the file.   echo __LINE__;
echo 
"<br />"; // __FILE__  Returns the full path and filename of the file. If used inside an 

include, the name of the included file is returned.  
echo __FILE__;
echo 
"<br />"; // __FUNCTION__  Returns the function name the code is currently executing inside of echo __FUNCTION__;
echo 
"<br />"; // __CLASS__ The class name. As of PHP 5 this magic constant returns the class name as it 

was declared. echo __CLASS__;
echo 
"<br />"; ?>

Share this article :

0 comments:

Post a Comment

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Learn to Share - All Rights Reserved
Template Modify by Creating Website
Proudly powered by Blogger