PHP Loosely Type Language - Strict type from PHP 7
Categories - PHP Tags - PHP   Maniruzzaman Akash   4 months ago   219   0

PHP Loosely Type Language - Strict type from PHP 7

Loosely Typed Language - PHP

PHP is a Loosely typed language, that means we don’t need to define the Data type of variable in PHP while we declare it. PHP automatically assume a data type for that variable, based on the value of that variable. Suppose, Let’s assign some variable and test it’s data type -

$name = "Jhon";
var_dump($name); // string(4) "Jhon" 

$age = 20;
var_dump($age); // int(20) 

$price = 800.50;
var_dump($price); // float(800.5)

In the above examples, we don’t define the data type of any variable. PHP automatically associates that data type for those variables. And we can then update the variables with another data type. Like so -

$name = "Jhon";
var_dump($name); // string(4) "Jhon"

$name = 50.5;
var_dump($name); // float(50.5)

At first, we assign Jhon String type data to variable $name. And then again, assign 50.5 or float type data to it. In other programming languages, if we want to assign another data type value to that variable, then it would create a fatal error or exception. But, php doesn’t create any type of exception.

That’s why PHP is called Loosely type language.

Strict type example -

From PHP Version 7, PHP added support for strict type data type. Like, if we assign a string value to any variable, we can not assign then integer or float values to that variable. PHP will create a warning or exception like other language. We would call that Strict type.

Let’s get an example of that -

Before strict implementation is ON, we have to add declare(strict_types=1); at very first line of the PHP code.
Let’s implement the previous chapter getAge() method with strict type in PHP.

<?php

declare(strict_types=1);

function getAge(int $age): void
{
    if ($age >= 0 && $age < 18) {
        echo "You are a Child";
    } elseif ($age >= 18 && $age < 50) {
        echo "You are an Adult";
    } elseif ($age >= 50) {
        echo "You are Old";
    } else {
        echo "Age is not valid.";
    }
}

echo getAge(40.5); // Fatal Error 

Output:

Fatal error: Uncaught TypeError: getAge(): Argument #1 ($age) must be of type int, float given, called in /Applications/MAMP/htdocs/phpclass/class13.php on line 16 and defined in /Applications/MAMP/htdocs/phpclass/class13.php:5 Stack trace: #0 /Applications/MAMP/htdocs/phpclass/class13.php(16): getAge(89.2) #1 {main} thrown in /Applications/MAMP/htdocs/phpclass/class13.php on line 5

As if getAge() is expecting integer int $age, and when calling if we pass float value, it will be given a fatal error that we can not assign float value to int type argument.

Strict type in Variable in PHP

We’ll discuss this in our OOP chapter, as we can use strict data type for a variable in PHP in our PHP class. Let’s check a demo,

<?php

declare(strict_types=1);

class Student
{
    public string $name;

    public int $age;

    public float $gpa;

    public function getGpA(): float
    {
        return $this->gpa;
    }

    public function setGpA(float $gpa): void
    {
        $this->gpa = $gpa;
    }
}

Don’t worry, we’ll discuss this type of Strict use in our PHP OOP chapter.

Previous
PHP - Functions in PHP - How to create and how to use
Next
PHP Superglobal Variables and It's Practical Examples - Get, Post, Session, Cookie, Request, Server