PHP Datatypes

PHP Datatypes

Array

There’s an explanation about arrays with examples: Array explanation

Object

The Object datatype in PHP is used to represent instances of classes in Object-Oriented Programming (OOP). An object is a complex data type that can contain both properties (variables) and methods (functions) within a class. The object is created by instantiating a class, and it allows you to model real-world entities with their attributes and behaviours.

Instantiating an Object: In PHP, you create an object by using the new keyword followed by the class name.

PHP
$object = new ClassName();

Accessing Properties: You can access or modify an object’s properties using the -> (arrow) operator.

PHP
$object->property = value;   // Setting a property
$value = $object->property;  // Getting a property

Calling Methods: Similarly, methods within a class are accessed using the -> operator.

PHP
$object->methodName();

Strings

A String is basically just well a straight line of characters for example: “Hello, World” is a String. In most programming languages a String is marked
with: “” or ”. Meaning it doesn’t really matter if you write: “Hello, World” or ‘Hello, World’, while there is a slight difference for the basics it doesn’t matter it will give the same output. So here’s what I mean:

PHP
$string_single = 'Hello, World';

$string_double = "Hello, World";

echo $string_single; // Output: Hello, World

echo $string_double;// Output: Hello, World

Of course to display this on a page it will have to be put in an HTML file so lets show how that would look:

PHP
<main>
  <p>
    <?php 
      $string_single = 'Hello, World ';

      $string_double = "Hello, World";
      
      echo $string_single, $string_double; // Output: Hello, World Hello, World
    ?>
  </p>
</main>

The space after the first “Hello, World” is so that the Output is nicer otherwise it would look like this: Hello,WorldHello, World

Now lets quickly go over the difference between single quotation marks (”) and double quotation marks (“”). While for just simple String output it doesn’t matter but if for example we want to call a variable inside a String it changes. Here’s how:

With single quotation marks:

PHP
$name = 'John';
echo 'Hello $name';  // Outputs: Hello $name

With double quotation marks:

PHP
$name = 'John';
echo "Hello $name";  // Outputs: Hello John

That’s because, in PHP, single quotes (') treat the string literally without variable interpolation or most escape sequences, while double quotes (") allow variable interpolation and interpret escape sequences like \n.

Booleans

Boolean is the simplest Datatype it can only hold 2 values either true or false, making it perfect if we want to check if something is indeed like that or not (if its true or false). A good example is a if statement (loop), it checks if the given value is true and if so it will execute the code given in the if statement. Here’s an example:

PHP
$number = 20;

if ($number == 20) {
  echo "If this text is visible then the Boolean value is true."
} else {
  "If you see this text then the boolean value is false."
};

In this example its quite simple if the value from the variable: $number is 20 (true) then echo this text: If this text is visible then the Boolean value is true but if the value of the variable: $number is not 20 (false) then echo this text: If you see this text then the Boolean value is false.

Integer

An Integer is the Datatype used for numbers without a comma so simple numbers: 1, 2, 3, …, 468, 469, …, 889, 890 etc.
An integer is a whole number without any decimal point. It can be positive, negative, or zero.

PHP
$integer = 1;

float

floats are used for numbers with a comma so complicated numbers: 1.8 / 2.4 / 3.6 / … / 468.29 / 469.13 / … / 889.487 / 890.193 etc.
A float is a number that includes a decimal point or is written in exponential form. It is used for representing real numbers.

PHP
$float = 1.8;

double

The double Datatype is basically the same as a float it is also used to store “complicated numbers”. The only difference is that while float is 4 bytes big a double is 8 bytes big so double, but in PHP that doesn’t matter since only double-precision floats are used, so the distinction between “float” and “double” does not matter. All floating-point numbers are 8 bytes in size.

But in other languages like C or C++ it does matter but like said above it doesn’t matter.

PHP
$double = 2.4;

Type Casting

PHP also supports type casting, which allows you to convert between different data types, such as from a string to an integer or a float.
Here’s an example:

PHP
$intVar = (int) "123"; // Converts string to integer
$floatVar = (float) "3.14"; // Converts string to float

Tags: