PHP Variable and Data Types

Web Design Tutorialz
6 min readSep 17, 2020

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

PHP has a total of eight data types which can be used to construct our variables -

  • 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

They are whole numbers, without any decimal point, like 242. They are the simplest type. They tally with simple whole numbers, both positive and negative.

Example

Integers can be assigned to variables or they can be used in expressions, as follows -

$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

They are like 3.141 or 4.90. By default, doubles types print with the minimum number of decimal places needed.

Example

The following below is a simple example -

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

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

Output

When the above code is executed, it will produce the following result -

2.28888 + 2.21112 = 4.5

Booleans

Boolean 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 as follows -

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

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

Interpreting other Types as Booleans

Here are the rules for determing the “truth” of any value not already of the Boolean type -

  • 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

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

$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

They are the order of characters for example “PHP supports string operations”.

Example

The following below are valid examples of strings -

$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

The following below is a simple example -

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

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

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

Output

When the above code is executed, it will produce the following result -

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

You can assign multiple lines to a single string variable using the 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

When the above code is executed, it will produce the 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! <channel> <title>What's For Dinner<title> <link>http://menu.example.com/<link> <description>Choose what to eat tonight.</description>

Variable Scope

Scope can be defined as the range of availability that a variable has to the program in which it was declared in. PHP variables can be one of the four scope type below -

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

Variable Naming

The following below are the rules for naming a variable -

  • 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.

--

--

Web Design Tutorialz

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