Skip to main content

PHP variable and constant

What is variable? 
A variable is a representation of a value. Let's take for instance, A=40; the representation of 40 is A, so A is called the variable. For storing information,  a variable can be called "containers".

Rules for variable 
1. Add a $ (dollar sign) in front or before the variable name.
For example: $No_of_oranges
2. The PHP cannot begin with numerals; despite the fact of adding the dollar sign ($) in front of the variable name. The variable name must begin with alphabets or underscore sign(_).
3. The PHP variable may contain the following characters:

  • Numerals: (0-9)
  • Alphabets: (A-Z) or (a-z) 
  • Underscore: (_)
4. PHP variables are case sensitive. ie. $No_of_oranges and $NO_OF_ORANGES are different variable
5. Variable name can be any length long.

Variable Types:
1. Scalar types:

  • Boolean
  • Integer
  • Float
  • String
2. Compound types:

  • Object
  • Array
3. Special types:

  • Resource 
  • Null
Let's take an example:
<?php
$string_value="hello simply lecture! ";
$integer=126;
$float=3.658;
$boolean=true;
$array=array(1,2,3);
$object=new subject_name();
$null_variable=NULL;
?>

Constants 
PHP constants are similar to variable but they cannot be changed when the script is executed.
The basic features of a constant that distinguish them from a variable are:

  • Constants don't require the $ (dollar sign) before their names 
  • Constants are usually uppercase that distinguish them from normal variable
  • define() is used to define a constant, it requires the name and the value of the name.
Example:
 define("subject name","simply lecture");

  • You can check if a given constant name exist or not.
 Example:
if (defined("subject_name"))
echo "constant subject_name exist";

Pre-defined variables
Pre-defined variables are already installed or defined variables, and PHP provides a large number of pre-defined variables to its users. This pre-defined variables are called "super globals".
The pre-defined variables are given below:

  • $GLOBALS - All variables available in global scope.
  • $_SERVER - Server and execution environment information.
  • $_GET - HTTP GET variable.
  • $_POST - HTTP POST variable.
  • $_FILES - HTTP File upload variable. 
  • $_SESSION - Session variable.
  • $_REQUEST - HTTP Request Variable.
  • $_ENV - Environment variable.
  • $_HTTP COOKIES - HTTP cookies.




Comments

Popular posts from this blog

Schrodinger equation as a law in physics

The unified theory of wave-particle duality has been used to derive the Schrödinger equations. The Schrodinger equations are generally accepted, by postulate rather than derivation, to be laws of physics. The Schrodinger equations provide a basis for analyzing many kinds of systems (molecular, atomic, and nuclear) in a particular inertial reference frame. The success of the Schrödinger equations constitutes a basis for accepting them, their derivations, and the unified theory of wave-particle duality which makes such derivations possible. This acceptance is completely justified in the favored inertial reference frame. In accord with the principle of relativity, all physical laws must be the same in all inertial reference frames, i.e., all physical laws must be Lorentz invariant. Recall, the relationship: $$\nabla^{2} \psi = \frac{\partial^{2} \psi}{\partial t^{2}}$$ ...........(1) Equ (1) is Lorentz invariant and reduces, by means of the procedure presented in the previou...

Cyanide: All about cyanide

What is cyanide? Cyanide is a chemical compound that consist of carbon (C) and nitrogen (N). It exist in different forms, so we can say; sodium cyanide, hydrogen cyanide, potassium cyanide and others. Most of these variants (forms of cyanide) are poisonous, that in can cause death within minutes. The origin of cyanide, started from the fact that a huge number of Nazi uses the potassium cyanide suicide pills to kill themselves during the World War II. The most dangerous form of cyanide is the hydrogen cyanide, which is in the form of gas; and is deadly when inhaled. Uses of cyanide  Despite the horrible fact of cyanide as  a poison, cyanide has its own importance as it is useful.  The uses of cyanide include: I. It is used in industrial chemistry, in the production of nylon. II. It is used for pest control, bring the key ingredient in the poison used to kill animals, such as rats and other rodents. III. It is used in the mining of golds and silver, to be able to ...

Angel flight

Angel flight is a term used by group of people whose members are provided with free air transportation,  because they are in need with free medical treatment far from home. The transportation of passengers are done by volunteer pilots using their own general aviation aircraft. History of angel flight  The first two organisation to be termed "angel flight" was founded in the year 1983. The first organisation was formed in Santa Monica, California known as the "Angel flight of California (presently Angel Flight West)". The second was formed in Atlanta, Georgia and was called "Angel Flight Soars" Accidents and incidents On 15th August, 2011 a Piper PA-28 Cherokee conducting an angel flight crashed in rural Victoria, Australia. On May 24, 2013 an angel flight crashed into a pond in Ephratah, New York. On June 28, 2017 a TBIO Tobago serving an angel flight crashed into a terrain near MT Gambier heading to Adelaide. Pilots  The pilots of angel flig...