Hello dear readers! welcome back to another section of my tutorial on PHP. In this tutorial guide, we will be discussing about the PHP Variable and Data Types.

The paramount way of storing information in the middle of a PHP program is by using a variable.

The following below are the most important things that you need to know on PHP.

  • All variables in PHP are denoted with a leading dollar sign ($).
  • The value of a variable is the value of its most recent assignment.
  • Variables are assigned with the “=” operator, with the variable on the left-hand side and the expression to be evaluated on the right side.
  • Variables can, but do not need to be declared before assignment.
  • Variables in PHP language do not have inherent types — a variable does not know in advance whether it will be used to store a number or a string of characters.
  • Variables used before they are assigned have difficult values.
  • PHP does a very good job of automatically converting the variable types from one form to another.
  • PHP variables are perl-like.

PHP Data Types

  • Integers — These are whole numbers, without a decimal point, like 242.
  • Doubles — are floating point numbers, like 3.141.
  • Booleans — Booleans have only two possible values, either True or False.
  • Null — is a special data type that only has one value; Null.
  • Strings — Strings are order of characters for example “PHP supports string operations”.
  • Arrays — Arrays are named & indexed collections of other values.
  • Objects — are the instances of the programmer-defined classes, which can package up other kinds of values and functions that are specific to the class.
  • Resources — Resources are special variables that holds references that are external to PHP.

The first five in the above list are the simple types, and the last two (arrays and objects) are the compound types — The compound types can package up other random values of random type, whereas the simple type can’t.

We will explain only the simple types in this tutorial guide, the compound types (Arrays and Objects) are going to be discussed in our subsequent tutorials.

Integers

Example

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

Integers can be in decimal (base 10), octal (base 8), and then hexadecimal (base 16) format. Decimal format is the default, the octal integers are specified with a leading 0, and hexedicimals have a leading 0x.

Doubles

Example

<?php
$many = 2.2888800;
$many_2 = 2.2111200;
$few = $many + $many_2;

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

Output

2.28888 + 2.21112 = 4.5

Booleans

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

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

Interpreting other Types as Booleans

  • If the value is a number, it is false if exactly equal to zero, otherwise true.
  • If the value is a string, it is false if the string is empty or the string is “0”. It is true otherwise.
  • Values of the type NULL are always false.
  • If the value is an array, it is false if it contains no other values, otherwise it is true. For an object, containing a value simply means having member variable which has been assigned a value.
  • Valid resources are true (although some function that return resources when they are successful, will return False when unsuccessful).
  • Don’t make use of double as Booleans.

Each of the the following variables has the truth value embedded in its name when it is 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

$my_var = NULL;

The special data type NULL is capitalized only by convention, but actually it is case Insensitive; you could as well type -

$my_var = 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 the IsSet() function.

Strings

Example

$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

The single quoted strings are treated almost literally, whereas the double quoted strings replaces the variables with their values. It also interprets certain character sequences.

Example

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

print($literally);
print "<br>";

$literally = "My $variable will print!";
print($literally);
?>

Output

My $variable will not print! My name will print

There are no artificial limits on string length within the bound of available memory, you should be able to make random long strings.

Strings that are delimited by double quotes are preprocessed in both the following two ways by PHP -

  • Certain character order that begins with backslash (\) are replaced with the special characters
  • The variable names(starting with $) are replaced with the string representations of their values.

The following below are the escape-sequence replacements -

  • \n is replaced by newline character
  • \r is replaced by the carriage return character
  • \t is replaced by the tab character
  • \$ is replaced by the dollar sign itself ($)
  • \” is replaced by the single double-quote (“)
  • \\ is replaced by a single backslash (\)

Here Document

<? 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! 
END; print $channel; ?>

Output

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! <channel> <title>What's For Dinner<title> <link>http://menu.example.com/<link> <description>Choose what to eat tonight.</description>

Variable Scope

  • Local Variables
  • Function Parameters
  • Global Variables
  • Static Variables

Variable Naming

  • The variable names must begin with a letter or an underscore character.
  • Variable name in PHP can consist of letters, numbers, underscores, but you can not use characters like +, -, %, (, ), etc.

Alright guys! This is where we are rounding up for this tutorial post. In my next tutorial post, we are going to be studying about the PHP Local Variables

Feel feel to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Originally published at https://www.webdesigntutorialz.com.

A tutorial blog that provides tutorials on Web Design, Programming, Java Technologies, Mobile App Development, Database, Machine Learning, etc.