Variable Types PHP Essential Training

Variable Types PHP Essential Training


Variable Types PHP Essential Training

Difficulty: Beginner - Variable Types PHP Essential Training

Time Duration: 30 Minutes

Requirements: XAMPP Installed PC, Text Editor(Sublime Text), Browser(Chrome)

Welcome to the next lesson of our PHP class. Today we're gonna learn Variable Types PHP Essential Training. The basic idea to store information dynamically or static is only the variables, as like as other programming and scripting language the PHP also has some variable types. In PHP most of the variable value is the most recent assignment to that variable. In clear if we initialize a variable with some value and after some time because of the program flow the value may be changed then the most recent value only stored in the variable.
Variable Scope - Variable Types PHP Essential Training
Scope can be defined as the range of availability a variable has to the program in which it is declared. PHP variables can be one of four scope types:
  •  Local variables
  •  Function parameters
  •  Global variables
  •  Static variables
Variable Naming - Variable Types PHP Essential Training
Rules for naming a variable is:
Variable names must begin with a letter or underscore character.
A variable name can consist of numbers, letters, underscores but you cannot use characters like + , - , % , ( , ) . & , etc. There is no size limit for variables.
PHP has a total of eight data types which we use to construct our variables:
  •  Integers: are whole numbers, without a decimal point, like 4195.
  •  Doubles: are floating-point numbers, like 3.14159 or 49.1.
  •  Booleans: have only two possible values either true or false.
  •  NULL: is a special type that only has one value: NULL.
  •  Strings: are sequences of characters, like 'PHP supports string operations.
  •  Arrays: are named and indexed collections of other values.
  •  Objects: are instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.
  •  Resources: are special variables that hold references to resources external to PHP (such as database connections).
In the above variable types the first five are very simple and easy to handle types. The next two, Arrays and Objects are compound and complex types. We have a separate lessons for the Arrays and Objects. Lets see what the other have.

Integers - Variable Types PHP Essential Training

Integers are the whole numbers like it doesn't show any decimal values. For Example 41, 23456, and etc. It also supports negative but only whole numbers. We can use this for assign a value to available and also used in expressions. Like this,
$int_var = 12345;
$another_int = 54321 + 98765;
For most common platforms, the largest integer is (2**31 . 1) (or 2,147,483,647), and the smallest (most negative) integer is . (2**31 . 1) (or .2,147,483,647).
The Integer can be a decimal (base 10), Octal (base 8) or Hexa Decimal. The Decimal is the default value. The Octal is lead with 0, hexadecimal is lead with 0x.

Doubles - Variable Types PHP Essential Training

Like the float in other languages the double is helps to handle the dotted values. The double is print only the minimum decimal point values needed. Consider the following example.
$dec1 = 5.2888800;
$dec2 = 5.2111200;
$res = $dec1 + $dec2;
echo $dec1 . " + " . $dec2 . " = " . $res;
Variable Types PHP Essential Training

Booleans - Variable Types PHP Essential Training

Booleans are not shows the physical result like the above variables. It is just a logic of check whether the given condition is true or false. PHP provides a couple of constants especially for use as Booleans: TRUE and FALSE, which can be used like so:
if (TRUE)
print("This will always print");
print("This will never print");
Variable Types PHP Essential Training

NULL - Variable Types PHP Essential Training

The variables are obviously used for to store some data. But if you want to store NULL value then you can use the NULL variable.
$var1 = NULL;
A variable that has been assigned NULL has the following properties:
  • It evaluates to FALSE in a Boolean context.
  • It returns FALSE when tested with IsSet() function.

Strings - Variable Types PHP Essential Training

Strings are the sequence of characters like "It is a String". The following are some string examples.
$variable = "name";
$literally = 'My $variable will not print!n';
$literally = "My $variable will print!n";
Like in the above example inside the singly quoted strings are treated as literally, whereas doubly quoted strings replace the variables with their values.
Variable Types PHP Essential Training
Strings that are delimited by double quotes (as in "this") are pre processed in both the following two ways by PHP:
  • Certain character sequences beginning with backslash () are replaced with special characters
  • Variable names (starting with $) are replaced with string representations of their values.
We hope that today you can learn the basics of the various variable types in the PHP. If you want learn the echo and print the data, go and check the Lesson 2 - Hello World Program PHP Essential Training.
Download the source Code of Variable Types PHP Essential Training from here.

Leave a Reply