Strict typing

suggest change

Since PHP 7.0, some of the harmful effects of type juggling can be mitigated with strict typing. By including this declare statement as the first line of the file, PHP will enforce parameter type declarations and return type declarations by throwing a TypeError exception.

declare(strict_types=1);

For example, this code, using parameter type definitions, will throw a catchable exception of type TypeError when run:

<?php
declare(strict_types=1);

function sum(int $a, int $b) {
    return $a + $b;
}

echo sum("1", 2);

Likewise, this code uses a return type declaration; it will also throw an exception if it tries to return anything other than an integer:

<?php
declare(strict_types=1);

function returner($a): int {
    return $a;
}

returner("this is a string");

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Type juggling and non-strict comparison issues:
* Strict typing

Table Of Contents
2 Arrays
4 Types
10 Cookies
14 JSON
15 SOAP
17 cURL
19 XML
21 Traits
35 UTF-8
36 URLs
38 PHPDoc
41 Loops
44 Closur
46 Type juggling and non-strict comparison issues
72 YAML
77 Cache
78 Streams
81 PDO
82 SQLite3
83 Sockets
87 MongoDB
93 IMAP
94 Redis
95 Imagick
102 APCu
108 PSR