ProgrammingEnthusiast
ProgrammingEnthusiast

Reputation: 219

Problem With Conditional Statements

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.");
   } 

Upvotes: 0

Views: 73

Answers (1)

Pascal MARTIN
Pascal MARTIN

Reputation: 401182

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.

Upvotes: 5

Related Questions