Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My PHP script reads a text file. Split's it using the character "|" and then put's it into an array. However when I try this:

if($user_details[1] == 'silver') {
  silver($userKey);


It does not seem to work and goes straight to the "else {"

I've also tried outputting what it thinks it's reading and it outputs "silver".

Any help?

What I have tried:

Here's my full code:

$success = false;
foreach ($keylist as $key) {
    $user_details = explode('|', $key);
    if ($user_details[0] == $userKey) {
        $success = true;
		if ($success) {
      //create html countdown file
      $filename = $userKey . '.txt';

      if (file_exists($filename)) {
        //exists
      } else {
        //doesnt exists

        //check key type
        if($user_details[1] == 'silver') {
          silver($userKey);
        } elseif ($user_details[1] == 'gold') {
          gold($userKey);
        } else {
          invalid($userKey, $user_details[1]);
        }
      }

			$_SESSION['userKey'] = $userKey;
			header('Location: user.php');
			break;
		} else {
			break;
		}
    }
}
Posted
Updated 1-Jan-19 3:24am
Comments
Richard MacCutchan 1-Jan-19 9:17am    
I have just recreated the key part of that code and it works correctly if $user_details[1] contains the string 'silver'. There must be something different happening in your code.
Bryian Tan 1-Jan-19 23:27pm    
which else did it went to?

1 solution

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

phpdbg | php debugger[^]
Debugging techniques for PHP programmers[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900