如果我声明,则为空白页(strict_types = 1);在文件顶部的PHP 7中 [英] Blank page if I declare(strict_types=1); in PHP 7 at top of the file

查看:91
本文介绍了如果我声明,则为空白页(strict_types = 1);在文件顶部的PHP 7中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我正在检查PHP 7,特别是返回类型声明类型提示.我已经从源代码( Github 的主分支)中编译了PHP 7,并在Ubuntu 14.04虚拟框中运行了PHP 7 .我尝试运行以下代码来测试新的异常.但这给了空白页.

Recently I was checking out on PHP 7, specifically return type declaration and type hinting. I have compiled PHP 7 from source(master branch from Github) and running it in Ubuntu 14.04 virtual box. I tried to run following code to get a test of new Exceptions. But it Gave a blank page.

<?php

function test(): string {

    return [];
}

echo test();

然后我意识到我必须设置要在屏幕上显示的错误.因此,我添加了如下所示的老式ini_set('display_errors', 1);

Then I realize I have to set error to be displayed on screen. So I added old fashioned ini_set('display_errors', 1); like below,

<?php
ini_set('display_errors', 1);

function test(): string {

    return [];
}

echo test();

根据此可扔接口 RFC

致命错误:未捕获的TypeError:test()的返回值必须是 输入字符串,在/usr/share/nginx/html/test.php中返回的数组 /usr/share/nginx/html/test.php中的7:7堆栈跟踪:#0 /usr/share/nginx/html/test.php(10):test()#1 {main}被抛出 /usr/share/nginx/html/test.php在第7行

Fatal error: Uncaught TypeError: Return value of test() must be of the type string, array returned in /usr/share/nginx/html/test.php on line 7 in /usr/share/nginx/html/test.php:7 Stack trace: #0 /usr/share/nginx/html/test.php(10): test() #1 {main} thrown in /usr/share/nginx/html/test.php on line 7

进一步挖掘,我在顶部顶部添加了declare(strict_types=1);,如下所示

Digging further I added declare(strict_types=1); at the top as below,

<?php declare(strict_types=1);

ini_set('display_errors', 1);

function test(): string {

    return [];
}

echo test();

然后爆炸,错误消失了,只剩下空白页.我不知道为什么它给了我空白页?

and bang, error just got disappeared leaving me with blank page. I cant figure out why it is giving me a blank page?

推荐答案

在搜索google和RFC之后,我来到了 RFC

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

此RFC进一步建议添加一个新的可选的每个文件 指令,declare(strict_types = 1);,它可以进行所有函数调用 文件中的return语句具有严格"类型检查 标量类型声明,包括扩展名和内置PHP 功能.

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.

这意味着指令declare(strict_types=1)没有任何问题,但是问题是我调用ini_set()函数的方式.它期望第二个参数为string类型.

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 )

我改用了int,因此显示错误本身所需的设置未能设置,因此被击中 在PHP严格模式下显示空白页.然后,我稍稍更改了代码,并按如下所示传递了字符串"1",它就起作用了.

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();

这篇关于如果我声明,则为空白页(strict_types = 1);在文件顶部的PHP 7中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆