PHP Variables

Hi everyone,

Variables are some tags that we can give name to them and that gives us the possiblity to hold some values inside them. Variables also can hold some expressions as well.

A PHP variable:

  • can start with an underscore or a letter(_ or a,b,c..). You can give single letter like a or a specific name like name to a variable.
  • should start with $ symbol and whenever you call it you should use your variable with that symbol.
A variable name/s:
  • can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • should not contain spaces(E.x: my name variable name is not valid)
  • are case sensitive (a and A are two different variables)

Let’s declare two variables that hold a number and a string.

$x=5;
$name="mehmet";

There is no special thing to define a PHP variable. When you define it, you give its value, otherwise the value will be null. In the example above the x variable holds the value of integer 5 and name variable holds the value of string mehmet.

Note: PHP is a loosely typed language which means that you don’t need to tell PHP which data type the variable is. PHP automatically converts the variable to the correct data type, depending on its value. In a strongly typed programming language(such as C#, Java, etc..), you have to declare the type and name of the variable before using it.

Variable Scopes

Scope of a variable is the portion of the script in which the variable can be referenced.

PHP has four different variable scopes:

  • local
  • global
  • static
  • parameter

Local Scope

There are functions in which we can process some codes in PHP. I am gonna tell about it later but to tell you about local scope I have to mention it. A variable that is declared inside a function has a local scope.

<?php
$x = 5; // global scope
function Test()
{
    echo $x; // local scope
}
Test();
?>

The function Test() here will not produce a result and output something. Because it is trying to output the local $x variable and it hold no value in it.

You can have local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared.

Local variables are deleted as soon as the function is completed.

Global Scope

By contrast with local scope a global scope variable should be declared outside of any function. We can access these types of variable from anywhere in the script but in a function. To access these variables in a function we use global key word.

<?php
$name     = "mehmet";
$surname  = "gunen";
$fullname = "";
function Test()
{
    global $name, $surname, $fullname;
    $fullname = $name . " " . $surname;
}
Test();
echo $fullname;
?>

The output of the script above will be mehmet gunen. We used . operator to combine strings here. I am gonna talk about operators later.

PHP also stores all global variables in an array called $GLOBALS[index]. Its index is the name of the variable. This array is also accessible from within functions and can be used to update global variables directly. Here is an altered version of the example above.

<?php
$name     = "mehmet";
$surname  = "gunen";
$fullname = "";
function Test()
{
    $GLOBALS['fullname'] = $GLOBALS['name'] . " " . $GLOBALS['surname'];
}
Test();
echo $fullname;
?>

The output of this script will also be mehmet gunen.

Static Scope

When a function is completed, all of its variables are normally deleted. But, sometimes you want a local variable to not be deleted. To do this, use the static keyword when you first declare the variable:

static $rememberMe;

Then, each time the function is called, that variable will still have the information it contained from the last time the function was called.

Note: The variable is still local to the function.

Parameters

A parameter is declared when we declare a function with parameter. A parameter acts like a local variable. We give values to the variable when we call the function.

function Test($para1,$para2,...)
{
// function code
}

In this lesson you have learned how to declare variables, the rules when we declare a variable, in which scope your variables will be accessible.

To learn more, stay connected!

 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *