1

Trying to get up to speed on PHP.

I'm looking at a basic guestbook form and trying to recreate it with my style.

screenshot0

The original file has this line:

$tmpDir = getenv('TEMP');

I know variables in PHP do not have to be declared first (irritating, though that may be for me), and with a name like tmpDir, it certainly sounds like some local, temporary variable.

So, I'm punching some of the code in using a trial version of Komodo because I'm hoping the Auto Complete features of it will help me learn the language a bit faster.

As soon as I get $t typed in, Auto Complete shows me what is available.

screenshot1

So, $tmpDir isn't just some local variable to this code?

How would a Dummy like me know this?

Komodo has a Go To Definition function, but when I do that in my original example, all it does is highlight the four instances of it in the code.

screenshot2

So, without having to go out there and memorize every global variable that PHP has, is there any way to know what is global and what is not?

I was not able to find $tmpDir on php.net/manual either.

UPDATE:

superglobal is the keyword or PHP definition I missed. I was getting my familiar C# term global confused with PHP's term.

Given that, the answer is:

If PHP code were to use the superglobal variable tmpDir, then the PHP code would have to explicitly call $GLOBALS[tmpDir] to use it.

Thanks go to deceze and Wayne Whitty, though neither of them has put this into an answer. When one of them does, I will mark it.

Community
  • 1
  • 1
  • 4
    It just seems like your IDE is just grabbing every variable definition it sees, regardless of scope. – gen_Eric Nov 15 '13 at 20:05
  • Variables in PHP *do* have to be declared if you don't want to trigger errors, there's just no special keyword to do so. – deceze Nov 15 '13 at 20:06
  • 1
    Are you coming from a strongly typed language by chance? In a dynamically typed language like PHP the type of autocompletion / intellisense you can provide is limited and prone to errors. – Mike Nov 15 '13 at 20:06
  • The referenced answer http://stackoverflow.com/q/16959576/153923 does not seem to tell me how to look at a variable in code and determine if it is global or local. –  Nov 15 '13 at 20:09
  • I understand how scope works. What I do not see is how a language noobie is to determine if the variable is local or global, especially since local variables are not required to be declared anywhere. –  Nov 15 '13 at 20:12
  • 2
    @jp2 Is this code in a function and there's no `global` keyword to be found? Then it's local. Otherwise global. – deceze Nov 15 '13 at 20:14
  • @jp2 Maybe you could elaborate on what you think "global" means, you may have a misunderstanding there. "Global" is simply the scope for everything not in a function. Don't confuse that with a *superglobal*, which is a variable magically available inside all scopes. You cannot create superglobals in userland code. – deceze Nov 15 '13 at 20:21
  • **OH!** So, `$GLOBALS[tmpDir]` would have to be called explicitly to use that global variable? If so, that would be the answer I'm looking for. –  Nov 15 '13 at 20:22
  • Quote your array indices, `$GLOBALS['tmpDir']`! `$GLOBALS` is a superglobal array (see above) which holds all variables in the global scope (also see above). – deceze Nov 15 '13 at 20:25

1 Answers1

2
$tmpDir = getenv('TEMP');

In this case, $tmpDir will always be a local variable unless you specifically set it to be a global variable. However, it might exist in the global scope. Your IDE might not care too much about scope. It could be showing you a list of variable names that you recently typed. Although, in this case, I'd be willing to bet that $tmpDir is a local variable that exists in the global scope, which means that it is available for you to use (try printing it out, just to see what happens).

<?php
/* global scope */ 
$tmpDir = getenv('TEMP'); 

function test(){ 
    /* reference to local scope variable that doesn't exist */ 
    echo $tmpDir; 
} 

test();
?>

If you run the code above, you'll see that it won't work because although $tmpDir has been declared in the global scope, it isn't available inside the local scope of the function test. This is because $tmpDir is not a global variable.

Wayne Whitty
  • 19,513
  • 7
  • 44
  • 66
  • OK. That's possible. Not only am I new to PHP, but I'm also new to this IDE. –  Nov 15 '13 at 20:10
  • It's a local variable... unless this whole code is in the global scope... – deceze Nov 15 '13 at 20:12
  • How would the whole code be "in the global scope"? –  Nov 15 '13 at 20:13
  • @jp2code I elaborated a bit more in my answer. Basically, I think you might be confusing global variables and variables that exist in the global scope. In this case, $tmpDir is a local variable that exists in the global scope, which is why your IDE is probably telling you that you can use it. – Wayne Whitty Nov 15 '13 at 20:13
  • @jp2 By not being written or included inside a `function`. Have you really read the linked duplicate? ;-P – deceze Nov 15 '13 at 20:16
  • I saw that in the [book I read](http://www.amazon.com/dp/0672329166) and [the reference you gave](http://stackoverflow.com/q/16959576/153923), but I must have misunderstood what they were saying. I understood it to say a global variable in a file or class could not be accessed from within other class functions. God. The PHP way seems so... wrong. I thought that briefly when I read the definitions, but must have twisted it around to fit the logic I saw in my head. –  Nov 15 '13 at 20:30
  • 1
    @jp2code Play around with a few scope examples and you'll quickly catch onto it. – Wayne Whitty Nov 15 '13 at 20:37