phpstorm - Undefined variable in PHP when the variable is present -
i have looked @ other questions , none of them fixed issue. following class defines variable , calls variable in html code. i've used same method php page , works perfectly. reason not work on page. i've done numerous times no issues. phpstorm says variable $user "might not have been defined" know is. how can fix this? have tried setting variable global @ top of page no luck. have tried setting variable global after if(isset()) , still doesn't work. here's php code:
<?php session_start(); require_once 'includes/class.user.php'; $user_edit = new user(); if(!$user_edit->is_logged_in()) { $user_edit->redirect('index.php'); } if(isset($_get['user_id'])) { $tid = $_get['user_id']; $stmt = $user_edit->runquery("select * users user_id = :id"); $stmt->execute(array(':id' => $tid)); $user = $stmt->fetch(pdo::fetch_assoc); } ?> <?php include('header.php'); ?> <div class="container side-collapse-container"> <p><?php if (!empty($user['username'])) { echo $user['username']; } ?></p> </div> <?php include('footer.php'); ?>
edit
this shown in browser after running xampp
notice: undefined variable: user in c:\users\user\phpstormprojects\site1\view_user.php on line 22
this should comment, don't have rep yet.
phpstorm saying $user variable might not defined because set in 'if' block. if want phpstorm msg go away, define $user above 'if' array.
$user = []; if (isset($_get['user_id'])) { $tid = $_get['user_id']; $stmt = $user_edit->runquery("select * users user_id = :id"); $stmt->execute(array(':id' => $tid)); $user = $stmt->fetch(pdo::fetch_assoc); }
if user name not showing on page, query either returning empty (false) or index 'username' incorrect.
of course, $user populated, user_id needs in query string.
Comments
Post a Comment