Home arrow Tutorials arrow PHP Variables

Web Development

Welcome to Web Development section. Please choose a category. 
 
  • Hrvatski tutorijali
    Ovdje su objavljeni razni tutorijali na hrvatskom jeziku na temu PC, programiranje, web development, savjeti, trikovi, optreba racunala itd.
  • Tips / Tricks
    Articles about various tips and tricks concerning Windows, Linux and other OS usage.
  • JavaScripts
    Code snippets, tutorials and tricks for JavaScrip language.
  • PHP & MySQL
    Tutorials, code examples, tips and tricks for PHP and MySQL development.
  • Tutorials
    Place for tutorials about using various application in best and simplest way.
PHP Variables Print E-mail
(1 vote)

PHP Variables
Sunday, 10 June 2007
PHP Variables

Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive, so $myvar is different from $myVar.

A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

Example :

<?php
$myvar          = "Hello";     // valid
$yourVar_is-123 = "World";     // valid
$123ImHere      = "Something"; // invalid, starts with number
?>


Variable Scope

The scope of a variable is the context within which it is defined. Basically you can not access a variable which is defined in different scope.

The script below will not produce any output because the function Test() declares no $a variable. The echo statement looks for a local version of the $a variable, and it has not been assigned a value within this scope. Depending on error_reporting value in php.ini the script below will print nothing or issue an error message.

<?php
$a = 1; // $a is a global variable

function Test()
{
   echo $a; // try to print $a, but $a is not defined here
}

Test();
?>

If you want your global variable (variable defined outside functions) to be available in function scope you need to use the$global keyword. The code example below shows how to use the $global keyword.

<?php
$a = 1; // $a is defined in global scope ...
$b = 2; // $b too

function Sum()
{
   global $a, $b; // now $a and $b are available in Sum()
   $b = $a + $b;
}

Sum();
echo $b;
?>

PHP Superglobals

Superglobals are variables that available anywhere in the program code. They are :

  • $_SERVER
    Variables set by the web server or otherwise directly related to the execution environment of the current script. One useful variable is $_SERVER['REMOTE_ADDR'] which you can use to know you website visitor's IP address

    Your computer IP is
    <?php
    echo $_SERVER['REMOTE_ADDR'];
    ?>
  • $_GET
    Variables provided to the script via HTTP GET. You can supply GET variables into a PHP script by appending the script's URL like this : http://www.vitez-studios.com/../examples/get.php?name=php&friend=mysql
    or set the a form method as method="get"

    <?php
    echo "My name is {$_GET['name']} <br>";
    echo "My friend's name is {$_GET['friend']}";
    ?>
    Note that I put $_GET['name'] and $_GET['friend'] in curly brackets. It's necessary to use these curly brackets when you're trying to place the value of an array into a string.

    You can also split the string like this :
    echo "My name is " . {$_GET['name']} . "<br>";
    but it is easier to put the curly brackets.
  • $_POST
    Variables provided to the script via HTTP POST. These comes from a form which set method="post"
  • $_COOKIE
    Variables provided to the script via HTTP cookies.
  • $_FILES
    Variables provided to the script via HTTP post file uploads.
  • $_ENV
    Variables provided to the script via the environment.
  • $_REQUEST
    Variables provided to the script via the GET, POST, and COOKIE input mechanisms, and which therefore cannot be trusted. It's use the appropriate $_POST or $_GET from your script instead of using $_REQUEST so you will always know that a variable comes from POST or GET.
  • $GLOBALS
    Contains a reference to every variable which is currently available within the global scope of the script.

You usually won't need the last three superglobals in your script.

 
< Prev   Next >
What's your favorite Internet browser?
 

Login






Lost Password?
No account yet? Register

Tools

Coming soon...

Partners

Syndicate