What is strict type php?

From the Treehouse blog:

With PHP 7 we now have added Scalar types. Specifically: int, float, string, and bool.

By adding scalar type hints and enabling strict requirements, it is hoped that more correct and self-documenting PHP programs can be written. It also gives you more control over your code and can make the code easier to read.

By default, scalar type-declarations are non-strict, which means they will attempt to change the original type to match the type specified by the type-declaration. In other words, if you pass a string that starts with a number into a function that requires a float, it will grab the number from the beginning and remove everything else. Passing a float into a function that requires an int will become int(1).

By default, PHP will cast values of the wrong type into the expected scalar type if possible. For example, a function that is given an integer for a parameter that expects a string will get a variable of type string.

Strict types disabled (eval):

It is possible to enable strict mode on a per-file basis. In strict mode, only a variable of exact type of the type declaration will be accepted, or a TypeError will be thrown. The only exception to this rule is that an integer may be given to a function expecting a float. Function calls from within internal functions will not be affected by the strict_types declaration.

To enable strict mode, the declare statement is used with the strict_types declaration:

Strict types enabled (eval):

Working example:

 'azjezz','age' => 100]); // returns an stdClass

function StdClassToArray(stdClass $object): array
{
    return (array) $object;
}

$array = StdClassToArray($object); // Returns array

function ArrayToObject(array $array): object // As of PHP 7.2
{
    return new ArrayObject($array);
}

function ObjectToArray(ArrayObject $object): array
{
    return $object->getArrayCopy();
}

var_dump( ObjectToArray( ArrayToObject( [1 => 'a' ] ) ) ); // array(1 => 'a');

The default behavior in PHP is to attempt to convert scalar values of incorrect type into the expected type. For example, a function expecting a string can still be called with an integer argument, because an integer can be converted into a string as shown in the following example:

Without strict typing, PHP will change the integer 12 to string "12". In strict mode, only a variable of the exact type of the type declaration will be accepted, or a TypeError will be thrown:

declare(strict_types = 1);

function getString(string $str) {
 var_dump($str);
}

$int = 12;
getString($int);

//Fatal error: Uncaught TypeError: Argument 1 passed to getString() must be of the type string, integer given...

The only exception to this rule is that an integer may be given to a function expecting a float. Function calls from within internal functions will not be affected by the strict_types declaration:

declare(strict_types=1); affects both parameters and return type declarations of scalar type, which must then be of the exact type declared in the function:

Note: You must configure the declare(strict_types = 1); statement in the first line of your code, or else it will generate a compile error. Also, it does not affect the included files, you need to declare the declare(strict_types = 1); statement on the top of each file.


Data types in PHP:

  1. Data Types
  2. Determine Variable Data Type
  3. Type Conversion: Convert variable data type
  4. Type Declaration
  5. Strict Typing Mode
  6. Testing, setting, and unsetting variables

Is PHP syntax strict?

PHP is a Loosely Typed Language Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error.

How do you mention strict requirements about data types in PHP?

In PHP the declare(strict_types = 1); directive enables strict mode. In strict mode, only a variable of the exact type of the “type declaration” will be accepted, or a TypeError will be thrown. Without strict typing, PHP will change the integer 12 to string "12" .

What is weak type in PHP?

For instance, a function that is given a float for a parameter that expects an integer will get a variable of type integer . In other words, PHP will try to “fallback” to the target type from the given type whenever it can. This is called “weak typing”.

What is return type in PHP?

Definition and Usage. The return keyword ends a function and, optionally, uses the result of an expression as the return value of the function. If return is used outside of a function, it stops PHP code in the file from running.