PHP Initialising strings as boolean first

Posted by Anriëtte Myburgh on Stack Overflow See other posts from Stack Overflow or by Anriëtte Myburgh
Published on 2013-11-11T09:40:53Z Indexed on 2013/11/11 9:53 UTC
Read the original article Hit count: 310

Filed under:
|

I'm in the habit of initialising variables in PHP to false and then applying whatever (string, boolean, float) value to it later.

Which would you reckon is better?

$name = false;
if (condition == true) {
    $name = $something_else;
}

if ($name) {  …do something…  }

vs.

$name ='';
if (condition == true) {
    $name = $something_else;
}

if (!empty($name)) {  …do something…  }

Which would you reckon can possibly give better performance? Which method would you use?

© Stack Overflow or respective owner

Related posts about php

Related posts about optimization