PHP Variables


In this lesson, you will learn about variables in PHP. A variable is a special container that you can define to hold data or value. PHP is a loosely typed programming language, but there are a few rules about declaring and storing variables.

Introduction to PHP Variables

Below are the rules when it comes to declare and store a variable in php:

  1. You have to declare a variable before using it.
  2. All variables start with a dollar sign, followed by the variable name.
  3. Values are assigned to variables using the assignment operator, with the variable on the left-hand side and the expression/value on the right side.
  4. Variables don’t need to be declared before assignment.
  5. In PHP you don’t assign data types while declaring the variable. Just declaring a variable and assigning a value to it is enough.
  6. In PHP, a variable name must start with an underscore (_) or a letter, but not a digit.
  7. Variable names are case-sensitive which means $sum and $Sum are different variables.
  8. In PHP, a variable name cannot contain other than alphanumeric characters and underscores (a-z, A-Z, 0-9, and -)
Tip: PHP automatically adjusts the data type of the variable according to the value being assigned. For example: $sum = 10; is a statement which assigns an integer number 10 to variable $sum. If you write $sum = 10.5; PHP will automatically convert the variable data type into double.

PHP Data Types

Different types of data take a different amount of storage. In PHP there are 8 different types of data types.

  1. Integers – Whole numbers like 1, 4, 89, 1000, etc.
  2. Doubles – Floating numbers like 4.56, 3.41, etc.
  3. Booleans – True or False. Zero is also considered to false. Any number other than zero (positive or negative) is recognized as true. An empty string or “0” is also considered as false. Value of type NULL is always false. An empty array is also equal to false in a condition.
  4. NULL – This is a special kind of value which describe the absence of the value. This value is not equal to zero.
  5. Strings – Series of characters. Single quoted strings are treated literally as it is, whereas double-quoted strings replace variables with their values.
  6. Arrays
  7. Objects – An instance of the class
  8. Resources – A special kind of variable that holds the reference to the resources external to PHP like database connection, file handles, etc.

Constants

Variables are ‘variable’. They offer a flexible way of storing data throughout the life of a program. You can change the data type and value of the variable at any time. However, sometimes you need to define a variable with a fixed value. These are called constant variables. These can be declared using the define() method. You must place the name of the constant variable and the value within the parentheses.

Define(“CONSTANT_NAME”, 42);

The constant variables can be accessed by name only; no dollar symbol is required.

PHP has some built-in constants for you. _FILE_, for example, returns the name of the file currently being read by the interpreter. _LINE_ returns the line number of the file.

Variable Scope

The scope of a variable is defined as the range within a variable can be accessed within a program in which it is declared.

Tip: Variable names in PHP are case-sensitive. It means $sum isn’t the same as $Sum.

1. Local Variables

The variables declared within a function, or a set of the block are called local variables, and it has scope within those curly brackets. If you try to access outside the scope of that variable, you will get an error message.

Example of Local Variable

<?php 
  
$num = 10;   
function localvar() {    
    $num = 50; 
    echo "Inside function num is $num <br/>"; 
}   
localvar();   
echo "Outside function num is $num \n"; 
  
?>

2. Global Variables

These variables are declared outside a function but to access inside the function; you have to use a keyword global.

Example of Global Variables I

<?php

$num = 10;
function scope(){
    echo $num;
}
scope();

?>

This program will raise an error.

E_NOTICE : type 8 — Undefined variable: num — at line 6

The variable $num was declared and initialized outside the scope of function scope(). When it is called inside the function, PHP parser failed to recognize it. Now use the keyword global inside the function. The output will be 10.

Example of Global Variables II

<?php

$num = 10;

function scope(){
    global $num;
    echo $num;
}
scope();

?>

3. Static Variables

A static variable exists only within a function scope, but it doesn’t lose its value even when program execution leaves this scope.

Example of Static Variables

<?php
function static_scope()
{
    static $a = 0;
    $b = 0;
    echo "a is ". $a;
    echo " and b is ". $b. "<br/>";
    $a++;
    $b++;
}
static_scope();
static_scope();
static_scope();
?>

Now, $a is initialized only in the first call of the function, and every time the static_scope() is called it will print the value of $a and increment it whereas the variable $b is non-static and it will lose its value after every function execution completion.

Output

a is 0 and b is 0

a is 1 and b is 0

a is 2 and b is 0

PHP Syntax Tutorial Home Echo and Print
Tutorials Panel

About Tutorials Panel

programming tutorial and source code for Software Engineers and developers. https://twitter.com/TutorialsPanel

View all posts by Tutorials Panel →