PHP variable

PHP variable

PHP variable:

A variable is a container for storing information.
If you want to work with data in PHP, the data must first be stored in a variable.

Declare / create PHP variable:

To write a variable in PHP, you must first write the $ sign and the name of the variable on the right.

 

Example:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <title>This is php tutorial</title>
  7.     <link rel="stylesheet" href="">
  8. </head>
  9. <body>
  10.     <?php 
  11.         $name = "Shuvo"; // this is name
  12.         $roll = 192045; // this is roll
  13.         echo $name . "<br>";
  14.         echo $roll;
  15.     ?>
  16. </body>
  17. </html>

Output:

Shuvo
192045

$name and $roll variables are added to Shuvo and 192045 respectively.

enlightenedTip: Use quotation marks ("") to store text values in variables.


PHP variable:

Variable names can be abbreviated (eg x and y) or descriptive (eg age, color, first_name, etc.).

Rules of PHP Variable Declaration:

The variable starts with the $ sign and is followed by the name of the variable.
The name of the variable must start with a letter or underscore.
Variable names should never start with numbers.
Variable names can only contain letters, numbers, and underscores (eg A-z, 0-9, and  _ )
The name of the variable is case sensitive. For example- $ONLYLEARN24 and $onlylearn24 are two completely different variables.


The output of the variable

echo statements are often used to take the output of PHP variables in browsers/consoles.

The following example shows how to take the output of a variable:

Example:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <title>This is php tutorial</title>
  7.     <link rel="stylesheet" href="">
  8. </head>
  9. <body>
  10.  
  11.     <?php 
  12.         $name = "Only Learn 24";
  13.         echo $name . " is one of the leading IT companies in Bangladesh.";
  14.     ?>
  15.  
  16. </body>
  17. </html>

Output:

Only Learn 24 is one of the leading IT companies in Bangladesh.

 

The following example shows the sum of 2 variables:

 

Example:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <title>This is php tutorial</title>
  7.     <link rel="stylesheet" href="">
  8. </head>
  9. <body>
  10.  
  11.     <?php 
  12.         $five = 5;
  13.         $three = 3;
  14.         echo $five + $three;
  15.     ?>
  16.  
  17. </body>
  18. </html>

Output:

8

 

PHP variable scope:

Variables can be declared anywhere in the script in PHP.

The scope of a variable is the part of the script where the variable is referred to:

yes  local- function The variable inside the.
yes  global- External variable of a function.
yes  static- The function does not delete the variable at the end of the work.

Local and Global Scope:

It can only be accessed and used from within the function

Example:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <title>This is php tutorial</title>
  7.     <link rel="stylesheet" href="">
  8. </head>
  9. <body>
  10.     <?php 
  11.     function localScope(){
  12.         $name = "Sakib";
  13.         echo "$name is a good boy" . "<br>";
  14.     }
  15.     localScope();
  16.         echo "$name is a good boy " . "<br>";
  17.     ?>
  18. </body>
  19. </html>

Output:

Notice: Undefined variable: name 
is a good boy
Sakib is a good boy

Declaring variables outside the function has a GLOBAL scope and can only be accessed and used from outside the function:

 

Example:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <title>This is php tutorial</title>
  7.     <link rel="stylesheet" href="">
  8. </head>
  9. <body>
  10.     <?php 
  11.  
  12.     $name = "Sakib";
  13.     function localScope(){
  14.         echo "$name is a good boy" . "<br>";
  15.     }
  16.     localScope();
  17.         echo "$name is a good boy " . "<br>";
  18.     ?>
  19. </body>
  20. </html>

Output:

Notice: Undefined variable: name 
is a good boy
Sakib is a good boy

 

 

PHP global keyword:

To access a global variable from a function, you need to use the global keyword.

To do this, use the global keyword before the name of the variable in the function as in the following example:

Example:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <title>This is php tutorial</title>
  7.     <link rel="stylesheet" href="">
  8. </head>
  9. <body>
  10.     <?php 
  11.         $firstName = " Mamun ";
  12.         $total = " Khan ";
  13.         function globalScope(){
  14.             global $firstName, $total;
  15.             $total = $firstName . $total;
  16.         }
  17.         globalScope();
  18.         echo $total;
  19.     ?>
  20. </body>
  21. </html>

Output:

Mamun  Khan

 

PHP stores all global variables in an associative array called $ GLOBALS [index]. Here index is the name of the variable. This array can also be accessed from within the function and can be used to directly update global variables.

The above example can also be written as below.

 

Example:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <title>This is php tutorial</title>
  7.     <link rel="stylesheet" href="">
  8. </head>
  9. <body>
  10.     <?php 
  11.         $firstName = " Mamun ";
  12.         $total = " Khan ";
  13.         function globalScope(){
  14.             $GLOBALS['total'] = $GLOBALS['firstName'] . $GLOBALS['total'];
  15.         }
  16.         globalScope();
  17.         echo $total;
  18.     ?>
  19. </body>
  20. </html>

Output:

Mamun  Khan 

 


PHP static keyword:

Normally all variables used in a function are deleted as soon as a function is completed.

If you want the variable not to be deleted after the function is completed, use the static keyword before the name of the variable when declaring the variable:

Example:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <title>This is php tutorial</title>
  7.     <link rel="stylesheet" href="">
  8. </head>
  9. <body>
  10.     <?php 
  11.         function globalScope(){
  12.             static $number = 6;
  13.             echo $number;
  14.             $number++;
  15.         }
  16.         globalScope();
  17.         echo "<br>";
  18.         globalScope();
  19.         echo "<br>";
  20.         globalScope();
  21.     ?>
  22. </body>
  23. </html>

Output:

6
7
8

 

Thank You........