0

Every time I run this code, only "Not from a" gets written, whether the location is "a" or not.

function logsIn($dir, $account, $balance) {
   $d    = date("D F d Y - h:i A");
   $file = fopen("logs/$dir.txt", "a");

   if ($_SESSION['pass'] == "123") {
      if ($api["Location"] != "a")
         fwrite($file, "<span style='color:#FF0000;text-align:center';>Not from a.</span>");
      else
         fwrite($file, "From a.");
   } 
3
  • Check the value of each variable you use and compare its value to what you expected. Basic debugging. Commented Jul 24, 2011 at 15:48
  • location is "a" or Location is "a"? Commented Jul 24, 2011 at 15:48
  • I'd also recommend, by the way, that you get rid of that "God conditional" on $_SESSION. Commented Jul 24, 2011 at 15:48

1 Answer 1

5

You are using a variable called $api in your function, but that variable is not defined anywhere.

So, $api["Location"] cannot have the value 'a' -- which is why the you always get "Not from a".


Note : If you have a $api variable that's defined outside of your function, then, it is not visible from inside that function.

For more information about this, you should take a look at the Variable scope section of the manual.


As a solution, I suppose you could modify your code so :

  • $api is expected as a parameter by your logsIn() function,
  • And it is passed to that function when you call it.

You could also make the $api variable (if it exists outside of your function) visible from inside the function, using the global keyword -- but it's not good practice, as it makes your function dependant on an external variable.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.