A variable in PHP is a name of memory location that holds data. In PHP Variables are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.
A variable can have a short name (like a and a) or a more descriptive name (name, class, section).
Rules for PHP variables:
- A variable starts with the $ sign, followed by the name of the variable
- A variable names are case-sensitive ($age and $AGE are two different variables)
- A variable name must start with a letter or the underscore character
- A variable name cannot start with a number
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Declaring PHP Variables :
In php we can store string, int, float values in variables.
<?php $str="hello string variable"; //String type $x=100; // integer type $y=10.6; // float type echo "string is: $str <br/>"; echo "integer is: $x <br/>"; echo "float is: $y <br/>"; ?>
Output :
-----------------------------------
string is: hello string variable
integer is: 100
float is: 10.6
Sum of two integer variables :
<?php $x = 5; $y = 4; echo $x + $y; ?>
Output :
----------------------
9
Concatenate two string variables :
<?php $a = "first variable"; $b = 'second variable'; echo $a." ".$b ; ?>
Output :
first variable second variable
Best part of php variable is automatically converts the variable to the correct data type, depending on its value.
PHP Variables Scope :
PHP Variables Scope :
In PHP, variables can be declared anywhere in the script.
The scope of a variable is the part of the script where the variable can be referenced/used.
PHP has three different variable scopes:
The scope of a variable is the part of the script where the variable can be referenced/used.
PHP has three different variable scopes:
- local
- global
- static
Global Scope :
Output:
------------------------------
access global variable Variable x outside function is: 5PHP Notice: Undefined variable: x in /web/com/1501411108_9799/main.php on line 7
Example : 2 (Global scope only accessible outside function)
Output :
---------------------
local scope
Variable x outside function is: 5
Global Keyword :
The global keyword is used to access a global variable from within a function. Let see the below example.
Output :
-----------------------
5
10
15
Local Scope :
Example :1 (Throw error message while accessing local variable outside function)
Output :
---------------------------------
access inside the function: 5can't access outside function : PHP Notice: Undefined variable: x in /web/com/1501411108_9799/main.php on line 9
Example : 2 ( Access local variable inside function)
Output :
------------------------------
access inside the function: 5
Static Keyword:
Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. In that case we use Static keyword.
Output:
----------------------
1
2
3
Const keyword :
constants defined using the const keyword must be declared at the top-level scope because they are defined at compile-time. This means that they cannot be declared inside functions, loops, if statements or try/ catch blocks.
Constant is case sensitive.
Constant can not be changed.
Output :
Hello const by JavaTpoint PHP
Like other programming language php variable support different kind of data types, which are as follows:
- A variable declared outside a function is called as GLOBAL SCOPE
- It can be only be accessed outside a function.
<?php $x = 5; // global scope function myTest() { //below line of code throw error message. //we can access the global variable inside function echo "access global variable $x "; } myTest(); echo "Variable x outside function is: $x"; ?>
Output:
------------------------------
access global variable Variable x outside function is: 5PHP Notice: Undefined variable: x in /web/com/1501411108_9799/main.php on line 7
Example : 2 (Global scope only accessible outside function)
<?php $x = 5; // global scope function myTest() { //below line of code throw error message. //we can access the global variable inside function echo "local scope <br>"; } myTest(); echo "Variable x outside function is: $x"; ?>
Output :
---------------------
local scope
Variable x outside function is: 5
Global Keyword :
The global keyword is used to access a global variable from within a function. Let see the below example.
<?php $x = 5; $y = 10; function add() { global $x, $y; echo "$x<br>"; // output 5 echo "$y<br>"; // output 10 $y = $x + $y; } add(); echo $y; // output 15 ?>
Output :
-----------------------
5
10
15
Local Scope :
- A variable declared within a function is called as LOCAL SCOPE.
- It can only be accessible within that function.
Example :1 (Throw error message while accessing local variable outside function)
<?php function demo() { $x = 5; // local scope echo "access inside the function: $x"; } demo(); // using x outside the function will generate an error echo "can't access outside function : $x"; ?>
Output :
---------------------------------
access inside the function: 5can't access outside function : PHP Notice: Undefined variable: x in /web/com/1501411108_9799/main.php on line 9
Example : 2 ( Access local variable inside function)
<?php function demo() { $x = 5; // local scope echo "access inside the function: $x"; } demo(); ?>
Output :
------------------------------
access inside the function: 5
Static Keyword:
Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. In that case we use Static keyword.
<?php function keep_track() { STATIC $count = 0; $count++; print $count; print "<br />"; } keep_track(); keep_track(); keep_track(); ?>
Output:
----------------------
1
2
3
Const keyword :
constants defined using the const keyword must be declared at the top-level scope because they are defined at compile-time. This means that they cannot be declared inside functions, loops, if statements or try/ catch blocks.
Constant is case sensitive.
Constant can not be changed.
<?php const MESSAGE="Hello const by JavaTpoint PHP"; echo MESSAGE; ?>
Output :
Hello const by JavaTpoint PHP
Like other programming language php variable support different kind of data types, which are as follows:
- String
- Integer
- Float (floating point numbers - also called double)
- Boolean
- Array
- Object
- NULL
No comments:
Post a Comment