PHP unassociated variables sitting in code doing nothing -
i came across this question features following code piece:
<?php class goat{ public $goat_id; public $goat_name; public $hashed_password; .... public static function authenticate($username, $password){ $is_auth; // line if(!empty($result)){ $user = array_shift($result); $is_auth = pass_auth::verify_password($password, $user- >hashed_password); } if($is_auth) { return $user; } .... } } my question comes marked line inside authenticate function. not example -but in general- point of $is_auth; declaration, sitting there doing nothing?
i have tried find answer before posting here if layout/behaviour has specific name don't know it.
i have made sandbox variations , surprised find none of them produce errors, below code error free:
<?php error_reporting(e_all); $small = "tree"; //$small = false; //swapping var value types makes no difference. $small; $sdgdfghdgd; so questions are:
- what name unassociated variable usage?
- is there point this?
- if so; what?
- why doesn't produce error or warning (i'm not saying should, necessarily, curious)?
$is_auth; is valid php expression. it's value value of $is_auth , doesn't have effect.
the php compiler smart enough detect expression useless (because it's value not used , doesn't have side effect) , doesn't generate opcode it.
if generating code, code should trigger e_notice because variable not defined yet.
the situation changes when expression involves array. if, conceptually, expression $a['foo']; equivalent $is_auth;, $a['foo']; not optimized out compiler. evaluated , triggers expected e_notice when $a not defined or doesn't have key 'foo'.
Comments
Post a Comment