Blank page if I declare(strict_types=1); in PHP 7 at top of the file

主宰稳场 提交于 2019-11-30 13:27:53

After searching around the google and RFC's I came to follwing sentence in RFC,

This RFC further proposes the addition of a new optional per-file directive, declare(strict_types=1);, which makes all function calls and return statements within a file have “strict” type-checking for scalar type declarations, including for extension and built-in PHP functions.

This means there was nothing wrong with directive declare(strict_types=1) but the problem was the way I was calling ini_set() function. It expects second parameter to be of string type.

string ini_set ( string $varname , string $newvalue )

I was passing int instead, and hence the setting needed to display errors itself failed to set and hence I was hit with a blank page by PHP strict mode. I then changed the code a bit and passed the string "1" as below and it worked.

<?php declare(strict_types=1);

ini_set('display_errors', "1");

function test(): string {

    return [];
}

echo test();

as the error states your function expect you to return string but instead you return an array! And function complains which is normal. So on your return simply put some string value. That's it!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!