PHP Variables

variable in php

The main way to store information in the middle of a PHP program is by using a PHP Variables.Here are the most important things to know about variables in PHP.

  1. All variables in PHP are denoted with a leading dollar sign ($).
  2. The value of a variable is the value of its most recent assignment.
  3. Variables are assigned with the = operator, with the variable on the left-hand side and the expression to be evaluated on the right.
  4. Variables can, but do not need, to be declared before assignment.
  5. Php variable do not have intrinsic types – a variable does not know in advance whether it will be used to store a number or a string of characters.
  6. Variables used before they are assigned have default values.
  7. PHP does a good job of automatically converting types from one to another when necessary.
  8. Its variables are Perl-like.

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

  1. Integers
  2. Doubles
  3. Booleans
  4. NULL
  5. Strings
  6. Arrays
  7. Objects
  8. Resources

The first five are simple types, and the next two (arrays and objects) are compound – the compound types can package up other arbitrary values of arbitrary type, whereas the simple types cannot.


Integers

Double

They like 3.14159 or 49.1. By default, doubles print with the minimum number of decimal places needed. For example, the code −

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

Boolean

They have only two possible values either 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<br>");

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

NULL

NULL is a special type that only has one value: NULL. To give a variable the NULL value, simply assign it like this −

$my_var = NULL;

The special constant NULL is capitalized by convention, but actually it is case insensitive; you could just as well have typed −

$my_var = null;

A variable that has been assigned NULL has the following properties −

i. It evaluates to FALSE in a Boolean context.
ii. It returns FALSE when tested with IsSet() function.


Strings

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 treated almost literally, whereas doubly quoted strings replace variables with their values as well as specially interpreting certain character sequences.

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

There are no artificial limits on string length – within the bounds of available memory, you ought to be able to make arbitrarily long strings.

Strings that are delimited by double quotes (as in “this”) are preprocessed in both the following two ways by PHP −

i. Certain character sequences beginning with backslash (\) are replaced with special characters
ii. Variable names (starting with $) are replaced with string representations of their values.


The escape-sequence replacements are −

  1. \n is replaced by the newline character
  2. \r is replaced by the carriage-return character
  3. \t is replaced by the tab character
  4. \$ is replaced by the dollar sign itself ($)
  5. \” is replaced by a single double-quote (“)
  6. \\ is replaced by a single backslash (\)

Here Document

You can assign multiple lines to a single string variable using here document.

Example:

<?php
   $channel =<<<_XML_
   
   <channel>
      <title>What's For Dinner</title>
      <link>http://menu.example.com/ </link>
      <description>Choose what to eat tonight.</description>
   </channel>
   _XML_;
   
   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!
   <br />
   END;
   
   print $channel;
?>

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *