PHP PART 2: UNDERSTANDING BASIC DATA TYPES AND VARIABLES IN PHP
Please Subscribe Youtube| Like Facebook | Follow Twitter
DATA TYPES AND VARIABLES IN PHP
In PHP, a variable is a name that refers to a value. PHP supports several data types that can be used to define variables. In this article, we’ll discuss the basic data types and variables in PHP.
Data Types in PHP
- Integer: Integers are whole numbers that can be positive, negative or zero. In PHP, integers can be specified in decimal, hexadecimal or octal format.
- Float: Floats are numbers with a decimal point or an exponent. They are sometimes called double or real numbers. In PHP, floats can be specified in decimal or exponential notation.
- String: Strings are a series of characters, like “Hello World”. They can be specified using single or double quotes.
- Boolean: Booleans represent two possible states, true or false.
- Array: Arrays are used to store multiple values in a single variable. They can be indexed or associative.
- Object: Objects are instances of classes that contain properties and methods.
- NULL: NULL represents a variable with no value.
Variables in PHP
In PHP, variables are declared using the $ sign followed by the variable name. Here’s an example:
$name = "John";
$age = 30;
$height = 1.75;
$student = true;
In the above example, we have declared four variables: $name, $age, $height, and $student. The first variable is a string, the second variable is an integer, the third variable is a float, and the fourth variable is a boolean.
You can also declare an empty variable using the NULL keyword, like this:
$location = NULL;
Example Code
Here’s an example code that demonstrates the use of basic data types and variables in PHP.
// Integer data type
$a = 42;
echo "Value of a: " . $a . "\n";
echo "Data type of a: " . gettype($a) . "\n";
// Float data type
$b = 3.14;
echo "Value of b: " . $b . "\n";
echo "Data type of b: " . gettype($b) . "\n";
// String data type
$c = "Hello, World!";
echo "Value of c: " . $c . "\n";
echo "Data type of c: " . gettype($c) . "\n";
// Boolean data type
$d = true;
echo "Value of d: " . $d . "\n";
echo "Data type of d: " . gettype($d) . "\n";
// Array data type
$e = array(1, 2, 3, 4, 5);
echo "Value of e: ";
print_r($e);
echo "Data type of e: " . gettype($e) . "\n";
// Null data type
$f = null;
echo "Value of f: " . $f . "\n";
echo "Data type of f: " . gettype($f) . "\n";
// Object data type
class Person {
public $name;
public $age;
}
$g = new Person();
$g->name = "John";
$g->age = 30;
echo "Value of g: ";
print_r($g);
echo "Data type of g: " . gettype($g) . "\n";
Output:
Value of a: 42 Data type of a: integer Value of b: 3.14 Data type of b: double Value of c: Hello, World! Data type of c: string Value of d: 1 Data type of d: boolean Value of e: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) Data type of e: array Value of f: Data type of f: NULL Value of g: Person Object ( [name] => John [age] => 30 ) Data type of g: object
In the above code, we have declared variables and then used the echo statement to output their values and data type to the browser.
In the above output, the boolean value of true is displayed as 1, which is a common convention in PHP.
PHP Beginner Tutorial Series
- PHP Part 1: Setup and Introduction
- PHP Part 2: Basic Data Types and Variables in PHP
- PHP PART 3: OPERATORS AND EXPRESSIONS IN PHP
- PHP PART 4: CONTROL FLOW STATEMENTS IN PHP
- PHP Part 5: Functions In PHP
- PHP PART 6: ARRAYS IN PHP
- PHP Part 7: String Manipulation In PHP
- PHP Part 8: Object-Oriented Programming In PHP (Classes And Objects)
- PHP Part 9: Object-oriented Programming In PHP (OOP Pillars)
- PHP Part 10: Exception Handling in PHP