PHP – Variable Types

PHP – Variable Types,The foremost manner to save information inside the center of a PHP application is via the usage of a variable.

Here are the maximum essential matters to realize about variables in PHP.

  • All variables in PHP are denoted with a leading dollar signal ($).
  • The value of a variable is the fee of its maximum latest challenge.
  • The Variables are assigned with the = operator, with the variable at the left-hand facet and the expression to be evaluated at the proper.
  • The Variables can, however do now not want, to be declared before task.
  • As Variables in PHP do not have intrinsic types – a variable does now not realize in advance whether or not it’ll be used to shop a number or a string of characters.
  • The Variables used earlier than they’re assigned have default values.
  • The PHP does an excellent task of automatically changing sorts from one to every other while necessary.
  • PHP variables are Perl-like.

PHP – Variable Types,Variable Types

The PHP has a total of eight statistics types which we use to construct our variables −

  • Integers − are whole numbers, without a decimal factor, like 4195.
  • Doubles − are floating-factor numbers, like 3.14159 or 49.1.
  • Booleans − have simplest two possible values either proper or fake.
  • NULL − is a special kind that only has one value: NULL.
  • Strings − are sequences of characters, like ‘PHP helps string operations.’
  • Arrays − are named and indexed collections of different values.
  • Objects − are times of programmer-defined classes, that can package up each different kinds of values and features that are unique to the class.
  • Resources − are special variables that keep references to sources external to PHP (which include database connections).

The first 5 are easy types, and the next two (arrays and gadgets) are compound – the compound types can package deal up other arbitrary values of arbitrary type, while the simple types cannot.

We will give an explanation for only simple data kind on this chapters. Array and Objects will be explained separately.

PHP – Variable Types,Integers

They are entire numbers, without a decimal factor, like 4195. They are the most effective type .They correspond to simple whole numbers, each superb and negative. Integers may be assigned to variables, or they can be used in expressions, like so −

$int_var = 12345;
 $another_int = -12345 + 12345;

Integer can be in decimal (base 10), octal (base eight), and hexadecimal (base sixteen) format. Decimal format is the default, octal integers are detailed with a main 0, and hexadecimals have a main 0x.

For most not unusual systems, the largest integer is (231 . 1) (or 2,147,483,647), and the smallest (most bad) integer is . (231 . 1) (or .2,147,483,647).

PHP – Variable Types,Doubles

They like 3.14159 or 49.1. By default, doubles print with the minimal quantity of decimal places wanted. For example, the code −

   $many = 2.2888800;
   $many_2 = 2.2111200;
   $few = $many + $many_2;
   print("$many + $many_2 = $few <br>");

It produces the following browser output −

2.28888 + 2.21112 = 4.5

PHP – Variable Types,Boolean

They have handiest two viable values either genuine or fake. PHP presents multiple constants specially for use as Booleans: TRUE and FALSE, which can be used like so −

if (TRUE)
   print("This will always print<br>");

   print("This will never print<br>");

PHP – Variable Types,Interpreting other types as Booleans

Here are the guidelines for determine the “truth” of any price now not already of the Boolean kind −

  • If the fee is more than a few, it’s far fake if exactly identical to 0 and true otherwise.
  • If the value is a string, it’s far false if the string is empty (has zero characters) or is the string “zero”, and is authentic otherwise.
  • Values of kind NULL are always fake.
  • If the fee is an array, it’s miles fake if it carries no different values, and it is actual otherwise. For an object, containing a price method having a member variable that has been assigned a cost.
  • Valid resources are authentic (despite the fact that a few capabilities that return assets whilst they are successful will return FALSE while unsuccessful).
  • Don’t use double as Booleans.

Each of the subsequent variables has the truth fee embedded in its name whilst it’s miles used in a Boolean context.

$true_num = 3 + 0.14159;
$true_str = "Tried and true"
$true_array[49] = "An array element";
$false_array = array();
$false_null = NULL;
$false_num = 999 - 999;
$false_str = "";


NULL is a special kind that best has one cost: NULL. To deliver a variable the NULL price, clearly assign it like this −

$my_var = NULL;

The unique consistent NULL is capitalized by convention, but simply it’s far case insensitive; you can simply as well have typed −

$my_var = null;

A variable that has been assigned NULL has the subsequent houses −

  • It evaluates to FALSE in a Boolean context.
  • It returns FALSE whilst examined with IsSet() characteristic.


They are sequences of characters, like “PHP supports string operations”. Following are valid examples of string

$string_1 = "This is a string in double quotes";
$string_2 = 'This is a somewhat longer, singly quoted string';
$string_39 = "This string has thirty-nine characters";
$string_0 = ""; // a string with zero characters

Singly quoted strings are dealt with nearly literally, while doubly quoted strings replace variables with their values as well as specifically decoding positive character sequences.

   $variable = "name";
   $literally = 'My $variable will not print!';
   print "<br>";
   $literally = "My $variable will print!";

This will produce following result −

My $variable will not print!
My name will print


There aren’t any artificial limits on string duration – in the bounds of available reminiscence, you ought with a view to make arbitrarily long strings.

Strings which are delimited by using double rates (as in “this”) are preprocessed in both the subsequent methods by using PHP −

  • Certain individual sequences starting with backslash () are changed with unique characters
  • Variable names (starting with $) are changed with string representations in their values.

The get away-series replacements are −

  • n is replaced via the newline individual
  • r is replaced by means of the carriage-return man or woman
  • t is replaced through the tab character
  • $ is changed by means of the dollar sign itself ($)
  • ” is changed via a single double-quote (“)
  • is replaced with the aid of a single backslash ()

Here Document

You can assign a couple of strains to a unmarried string variable the usage of right here record −

   $channel =<<<_XML_
      <title>What's For Dinner</title>
      <link> </link>
      <description>Choose what to eat tonight.</description>
   echo <<<END
   This uses the "here document" syntax to output multiple lines with variable 
   interpolation. Note that the here document terminator must appear on a line with 
   just a semicolon. no extra whitespace!

   print $channel;

This will produce following result −

This uses the "here document" syntax to output
multiple lines with variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon. no extra whitespace!

<title>What's For Dinner<title>
<description>Choose what to eat tonight.</description>

Variable Scope

Scope can be defined as the variety of availability a variable has to the program in which it is declared. PHP variables can be one of 4 scope sorts −

  • Local variables
  • Function parameters
  • Global variables
  • Static variables

Variable Naming

Rules for naming a variable is −

  • Variable names should start with a letter or underscore person.
  • A variable call can encompass numbers, letters, underscores but you can’t use characters like + , – , % , ( , ) . & , and so on

There is not any size restrict for variables.