duanjingwei7239 2019-04-17 09:38
浏览 333
已采纳

PHP忽略了var_dump(),die()等代码

This is a very weird situation like I've never seen in my life. For some reason PHP is ignoring a lot of code inside a static function.

Here is the example:

static function describe($tableName, $columns = '*') {
    var_dump($tableName);
    die();
    $md5 = ...code...
    if (!empty($content = Cache::get($md5))) {
        return unserialize($content);
    }

I keep getting the error

Parse error: syntax error, unexpected '=', expecting ')'

in

if (!empty($content = Cache::get($md5))) {

And yes it recognises the class Cache and its function.

Can anyone guide me?

  • 写回答

2条回答 默认 最新

  • donglin7979 2019-04-17 09:42
    关注

    Prior to PHP 5.5, empty() function can only support strings.

    Any other input provided to it like: a function call e.g.

    if (empty(myfunction()) {
     // ...
    }
    

    would result parse error.

    As per documentation:

    Note: Prior to PHP 5.5, empty() only supports variables; anything else will result in a parse error. In other words, the following will not work: empty(trim($name)). Instead, use trim($name) == false.

    Better way, get your $content variable first and then check if it is not empty.

    Rather than initialising it and checking its emptiness simultaneously.

    You can separate the if statement in two parts like this:

    if ($content = Cache::get($md5) && !empty($content)) {
     return unserialize($content);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?