2

I have looked at other Stack Overflow questions and none of them fixed the issue. The following class defines the variable and then calls the variable in the HTML code.

I've used this same method for another PHP page and it works perfectly. For some reason it does not work on this page. I've done it like this numerous times without any issues. PhpStorm says the variable $user "might not have been defined", but I know it is. How can I fix this?

I have tried setting the variable global at the top of the page without luck. I have tried setting the variable global after if(isset()) and still doesn't work. Here's my 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 * FROM users WHERE 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'); ?>

This is what is shown in the browser after running it with XAMPP

Notice: Undefined variable: user in C:\Users\USER\PhpstormProjects\site1\view_user.php on line 22

10
  • 1
    What variable? What are you expecting to happen/what actually does happen? You don't seem to have defined $user_trip anywhere. Commented Sep 12, 2017 at 2:26
  • Maybe PHPStorm just needs to be told to stop barking. Commented Sep 12, 2017 at 2:27
  • Sorry. I forgot to mention that. I can't think straight right now cuz I'm so frustrated. The variable $user is displaying as undefined Commented Sep 12, 2017 at 2:28
  • I see. And $user_trip, what is it? What is the actual error you're getting? Which line does it complain about, where you define $user or where you try to access it in the DIV? Commented Sep 12, 2017 at 2:30
  • 1
    I found it. :/ What an extreme noob mistake. I don't even think a noob would make this mistake. smh. The link was going to user.php?view_id= when it should have been going to user.php?user_id=. I feel like such an idiot. It works now. smh Commented Sep 12, 2017 at 2:40

2 Answers 2

5

PhpStorm is saying the $user variable might not be defined because it is currently set in a 'if' block. If you want the PhpStorm msg to go away, define $user above the 'if' as an array.

$user = [];
if (isset($_GET['user_id'])) {
    $tid = $_GET['user_id'];
    $stmt = $user_edit->runQuery("SELECT * FROM users WHERE user_id = :id");
    $stmt->execute(array(':id' => $tid));
    $user = $stmt->fetch(PDO::FETCH_ASSOC);
}

If the user name is not showing on the page, then the query is either returning as empty (false) or the index 'username' is incorrect.

Of course, for $user to be populated, user_id needs to be in the query string.

Sign up to request clarification or add additional context in comments.

3 Comments

I like that. I didn't think of that for stopping PHPStorm from barking :D
Whenever you initially define a variable inside a condition like that, and then call it later below, PhpStorm will always give a warning. This is because, indeed, the variable might not be defined if the condition is not met. While the code may "work", php will throw out a Notice like that every time the condition is not met and the variable is therefore, Undefined.
BTW, the reason you were getting an undefined variable with the wrong query string parameter, is because PDOStatement::fetch returns false if no row is found, setting $user as a bool. Therefore, you will get a notice if you try to use it as an array.
2
  1. Define $user at first

    $user = array();
    // your codes
    
  2. Check $user before calling

    if (isset($user['username']) && !empty($user['username'])) {
        // your codes
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.