Loading

PHP VARIABLES

PHP Variables are just like a container that holds data. PHP variables start with a dollar ($) sign.

For defining PHP variables we must follow the following rules:-

  1. A variable start with a $ sign followed by the name of the variable.
  2. A variable name starts with either letter or an underscore (_).
  3. A variable name cannot start with a number or any other special character like &, *, ^, etc. except underscore (_).
  4. A variable name can only contain alphanumeric characters like [a-z], [A-Z], [0-9], and underscore (_).
  5. PHP variables are case sensitive like $a and $A both are different variables.
  6. A PHP variable cannot contain space or syntax.
  7. The assignment operator (=) is used to assign the value of a variable.
  8. After declaring the variable we can use it throughout the code.
  9. If we assign a text or string value to a variable, I am required to put single (”) or double (“”) quotes around the value.
  10. Since PHP is a loosely typed language so we do not need to declare the data type of the variable.
  11. By default variables always assign by value but we can define variables without assigning the values.

Syntax:-

$variable = value;

Example:-

$name = “KSH TUTOR”;

$age = 40;

$price= 400.89;

<?php

$a = 5;

$b= 8;

echo $total = $a + $b;

?>

<?php

$name = ‘KSH TUTOR’;

echo $str1 = “I am a $name”;

echo $str2 = ‘ I am a ‘ . $name;

?>

Variables can assign by reference, using ampersand(&) to the beginning of the variable. But only named variables can be assigned by reference.

Example:-

<?php

$name = ‘KSH TUTOR’;

$ref = &$name;

$ref = “I am a  $ref”;

echo $ref;

echo $name;

?>

$this is a special variable so we can not assign it. But Prior to PHP 7.1.0, indirect assignments such as variable variables are allowed and throw a fatal error.

isset() function checks whether the variable initializes or not. It returns true or false.

unset() function or construct is used to destroy the variable. It returns true or false.

is_null() function or construct to check whether the variable is NULL or NOT NULL. It returns true or false.

empty() function or construct check the variable emptiness. It returns true or false.

Normally we use echo, print, printf, etc. to display the value of variables.

PHP variables are mainly two types:-

  1. PHP predefines variables.
  2. PHP users define variables.
  3. PHP Predefine variables:- There are 14 predefined variables in PHP 8
  4. Superglobals — Built-in variables that are always available in all scopes
  5. $GLOBALS — References all variables available in the global scope
  6. $_SERVER — Server and execution environment information
  7. $_GET — HTTP GET variables
  8. $_POST — HTTP POST variables
  9. $_FILES — HTTP File Upload variables
  10. $_REQUEST — HTTP Request variables
  11. $_SESSION — Session variables
  12. $_ENV — Environment variables
  13. $_COOKIE — HTTP Cookies
  14. $php_errormsg — The previous error message
  15. $http_response_header — HTTP response headers
  16. $argc — The number of arguments passed to the script
  17. $argv — Array of arguments passed to the script

User Define Variable:-

User Define variables are 3 types

  1. variable scope
  2. variable variables
  3. reference variable

Variable Scope:-

variable scope are 3 types

  1. local scope
  2. global scope
  3. static variable

Read Also:-

PHP Global Variable $_GET

PHP Global Variable $_SERVER

PHP $GLOBALS Variable

PHP predefined variables

How to code a PHP application

For more detail about variables please see manual

Share with:


Leave a Reply

Connect with:

z35W7z4v9z8w
Verified by ExactMetrics