is there any way to differentiate between a variable not having been defined yet or had been defined but is set to 'NULL'
Probably not.
You can read about NULL here.
After a little more digging it may be possible to use get_defined_vars() and check for the variable name as a key in the returned array.
This works even if the var was assigned NULL.
You can check $GLOBALS, if it contains certain key. Does not work for variables defined inside functions:
array_key_exists('variable name', $GLOBALS);
For objects, check *property_exists* function.
Although, I'd suggest that you avoid creating any code that relies on (in)existence of any variable. If you use a var, it should be defined from start of the script/object instantiation. If you have to carry more information, that you can hold in value/null, you should not go for value/null/unset way, you might want to create another boolean var, etc. And I recommend creating a code that won't issue any E_NOTICE because of operations on inexistent variables.
Related
I often get PHP errors about a variable not defined. I'm also wondering what is best practice for setting session variables. Currently I'm doing this:
session_start();
if (!isset($_SESSION["myVar"]))
$_SESSION["myVar"] = "";
But this seems untidy to me. I know there is a PHP unset function, but what is the equivalent to simply set/define a variable into existence, without setting an initial value?
Php has dynamic variable allocation and typing. When a variable is first referenced within a program, memory is allocated for its use.
Meaning that unless you don't assign a value, a variable can't be declared, like say, in java.
Best way how to make sure you "declare" all your variables?
Assign them to null or empty string at the beginning of each function / method.
About session variables, I'd apply the same logic.
Is there a possibility to add on an existing constant-array some values?
define("USERID", array(123));
I've tried with
define("USERID", '456');
And google didn't get an answer as well,-)
From : http://php.net/manual/en/language.constants.php
A constant is an identifier (name) for a simple value. As the name
suggests, that value cannot change during the execution of the script
You might have tried this:
define('USERID', [123, 456]);
But that won't work, defining a constant a second time is simply ignored by php and keeps the first value it was defined as.
Instead you might consider using a static class variable. Or even a public property or getter method for a class. Or just a global variable.
I've been searching for quite a while and cannot find what this method is actually called.
In PHP example:
$var->{'property_name'}
Depending on what you are accessing it will be called...
A variable variable
A variable property
A variable function
It is worth noting that the curly-braces are only needed when you need to disambiguate an expression (bear in mind the string you use may itself be stored in a variable!)
And so on. This is documented in the PHP manual for variable variables.
I have a php file in which I need to be able to easily flip a switch. The switch is set to 1 for local server and 0 for production server. local_on='1'; or local_on='0';. Which way is better (creating a global or using define)? If either way is good which way is best practice?
define. You can't change its value later, and its value is always available in all scopes, tersely.
A global variable is, as its name indicates, variable -- which means its value can be changed by any portion of your code ; which, in your case, is probably not what you want.
On the other hand, a constant (that you'll set with define) is... well... constant ; and, as such, cannot be modified once set ; which is probably what you want, in your case.
Considering the variable vs constant idea, for this kind of switches, I generally use define() and constants.
The define is the better choice of the two, because global variables are bad news for reasons I'm sure you're already familiar with, and because you have to remember to declare global $var in every function and method in your code. Defined constants are automatically available everywhere. Additionally, a variable could inadvertently be set from one state to the other during the running of your script. This could cause some really hard-to-find bugs if it happened.
Another way that performs a little better than define symbols and minimizes name clashes is a class declaration like
abstract config {
const LOCAL = true; // toggle to false
// or maybe
const SERVER = 'local'; // toggle to 'remote'
// (maybe having if (config.SERVER == 'remote') would be more readable in some
// cases than if (!config.LOCAL) depends on your app)
}
I prefer define. Reasons:
You can't redefine it accidentally
You can use in other scope (ex. functions) without ugly $GLOBALS construction
PS: since PHP 5.3 you can use const , not only define to declare constants. It's more readable for me
Both work fine as long as you remember to choose a distinctive name (all-uppercase for defines) and use the modern way to access global variables via the $GLOBALS superglobal (sic). Also, global variables are, well, variable, so you could, in theory, change its value by accident or so.
To simplify deployment and not setting or unsetting the switch by accident, I'd recommend automatically setting it automatically by examining the properties of $_SERVER, like
// Turn on debugging code on local machine
define('MYPAGE_LOCAL_ON', $_SERVER['SERVER_NAME'] == 'my-dev-box');
Also, I don't see why you'd set the switch to a string or an integer for that matter. The boolean values true and false seem more appropriate.
In short terms, define is what you look for (for said reasons).
However, come the future development, you might look for something like a dependency for your whole application providing the context it is running in. This can not be done with constants, so then define but as well as global variables are both wrong.
If I use PHP's extract() function to import a variable from an array, will a variable with the same name be overwritten? The reason I ask is because I'm trying to initialize all of my variables.
Thank you for your time.
By default it will overwrite.
http://php.net/extract
If extract_type [the second argument] is not specified, it is assumed to be EXTR_OVERWRITE
See the linked page for other options
The default is to overwrite, however you can change this action to one of several possiblities, by telling the function how to handle collisions:
for example passing EXTR_SKIP as the second parameter e.g extract($array,EXTR_SKIP) will cause collisions to be skipped.
The full usage is detailed here : http://php.net/manual/en/function.extract.php
This depends entirely on the extract_type value you use. The default, however, is to overwrite.
That depends on the second argument you pass to the function. extract() takes an optional second argument consisting of constants. See the docs at http://us2.php.net/manual/en/function.extract.php