PHP Variables

PHP Variables


Variables are data storage "containers".

A variables may be given a short name (such as x and y) or a more specific name (such as era, carname, or total volume).

You can also search for these topics, PHP variable type, PHP variables definition, PHP variables initialization,variable classification in PHP,what is php variable.

Rules for PHP variables

  • A variable is identified by the $ symbol, which is followed by the variable's name.
  • A variable name must begin with a letter or an underscore.
  • A number cannot be the first character in a variable name.
  • Only alpha-numeric characters and underscores (A-z, 0-9, and_ ) are used in variable names.
  • Case matters when naming variables ($age and $AGE are two distinct variables).
You can also search for these topics, rules for php variables, list the rules of php variable, can php variable start with number?, Is php variables are case sensitive.

Creating (Declaring) PHP Variables

A variable begins with a $ symbol in PHP and follows the variable name.

Example :-

<?php
$msg = "Welcome to PHP!";
$a = 6;
$b = 15.5;
?>

Example Explained :-

Following the execution of the preceding statements, the variable $msg will contain the value Welcome to PHP!, the variable $a will contain the value 6, and the variable $b will contain the value 15.5.

Note :- When you add a text value to an variable, surround the value with single or double quotes.

In contrast to other programming languages, PHP does not provide a command or keyword to declare a variable. It is generated or defined when you add a value to it for the first time.

You can also search for these topics, variable declaration in PHP, PHP declaring variables on same line, PHP declaring variables best practice, create a php variable example.

Output Variables

To display data to the browser screen, the PHP echo command is frequently used.

Example 1 :- The following example will show how to output text and a variable.

<?php
$msg = "simmanchith.com";
echo "I love $msg!";
?>

Output :-

I love simmanchith.com!

Example 2 :- The following example will produce the same output as the example above.

<?php
$msg = "simmanchith.com!";
echo "I love " . $msg . "!";
?>

Output :-

I love simmanchith.com!

Note:- We used "." symbol to concat the text and the variable name.

Example 3 :- The following example will output the sum of two variables:

<?php
$x = 5;
$y = 4;
echo $x + $y;
?>

Output :-

9

You can also search for these topics, php output variable type, php echo output variable, php output variable name, example of output variable using php.

PHP is a Loosely Typed Language

Depending on the value of the variable, PHP automatically assigns a data type to it. You can perform things like adding a string to an integer without raising an error since the data types aren't established in a strict sense.



You can also search for these topics, PHP is loosely typed language true or false, loosely typed php, PHP loosely typed object.

PHP Variables Scope

In PHP, anyplace in the script, variables can be declared.

The scope of a variable refers to the area of the script where it may be the variable referenced and utilized.

PHP has three different variable scopes.

  • local
  • global
  • static
You can also search for these topics, variable scope in php, types of variable scopes in php, php variable scope example.

Global Variable Scope

A variable declared outside of a function has a GLOBAL SCOPE and can be accessed only from outside the function.

Example :-

<?php
$x = 5; // global scope
function myTest() {
  // using x inside this function will generate an error
  echo "<p>Variable x inside function is: $x</p>";
}
myTest();
echo "<p>Variable x outside function is: $x</p>";
?>

Output :-

Variable x inside function is: ERROR

Variable x outside function is: 5

You can also search for these topics, what is global scope, define global scope, how to use global scope, global scope example,php global scope variable.

Local Variable Scope

A LOCAL SCOPE variable declared within a function can only be accessed inside that function.

Example :-

<?php
function myExam() {
  $e = 5; // local scope
  echo "<p>Variable e inside function is: $e</p>";
}
myExam();
// using e outside the function will generate an error
echo "<p>Variable e outside function is: $e</p>";
?>

Output :-

Variable x inside function is: 5

Variable x outside function is: ERROR

local variables are only recognized by the function in which they are defined, you can have local variables with the same name in various functions.



You can also search for these topics, what is local scope, define local scope, how to use php local scope, php local scope example, php local scope variable.

PHP The global Keyword

To access a global variable from within a function, use the global keyword.

To do this, use the global keyword before the variables (inside the function).

Example:-

<?php
$a = 5;
$b = 10;
function myExam() {
  global $a, $b;
  $b = $a + $b;
}
myExam();
echo $b; // outputs 15
?>

Output :-

15

PHP also keeps all global variables in a $GLOBALS[index] array. The variable's name is stored in the index. This array is also available via functions and may be used to directly update global variables.

Example:- The example above can be rewritten like this:

<?php
$a = 5;
$b = 10;
function myExam() {
  $GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}
myExam();
echo $b; // outputs 15
?>

Output :-

15

You can also search for these topics, global keyword definition, Types of variable keywords in PHP, global keyword in PHP, php global keyword example, php global keyword function, php global keyword variable inside loop.

PHP The static Keyword

When a function is finished/executed, all of its variables are normally erased.

However, there are occasions when we do not want a local variable to be destroyed and it also needs for the job.

Example :-

<?php
function test() {
  static $a = 0;    //  static variable
  $b = 0;   // local varaible
  echo "a = " . $a . " , b = " . &b;
  $a++;
  $b++;
}
test();
test();
test();
?>

Output :-

a = 0 , b = 0
a = 1 , b = 0
a = 2 , b = 0

A local variable will be destroyed after completing a function execution. It will be newly initialized whenever a function was called.

The static variable will then retain the information it contained the previous time the function was called each time the function is invoked.

You can also search for these topics, php static keyword, php static keyword example, php static keyword inside function, php use static keyword instead of self.