Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP is ignoring code like var_dump(), die(), etc

Tags:

php

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?

like image 254
Tiago_nes Avatar asked Jan 01 '23 01:01

Tiago_nes


2 Answers

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);
}
like image 172
Pupil Avatar answered Jan 03 '23 13:01

Pupil


Try this,

if (!empty($content) && $content = Cache::get($md5)) {
        return unserialize($content);
}

OR : To get easy readability

if (!empty($content){
   if($content = Cache::get($md5)){
       return unserialize($content);
   }
}
like image 40
M.Hemant Avatar answered Jan 03 '23 14:01

M.Hemant