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.
Related
Wondered why the use of a GLOBALS variable is causing an error.
function makeNewPage($cpage = $GLOBALS['url']){
//some code here
}
See the manual:
The default value must be a constant expression, not (for example) a variable, a class member or a function call.
(Presumably, because they are determined at compile time and not run time).
Also
A function may define C++-style default values for scalar arguments
array_push
I have a PHP statement as follow (in a method of a class)
array_push(self::USER_BASIC_DETAIL_FIELDS, 'cname_username');
which gives me error
Cannot pass parameter 1 by reference
Then I tried it assigning it to variable and it all worked fine
$r = self::USER_BASIC_DETAIL_FIELDS;
array_push($r, 'cname_username');
My question is why does PHP throws an error in above case?
I have an answer but I am not sure so asked here. The answer is like:
array_push does not return the modified array but changes the variable given at argument 1. So change are made at the locations in memory where variable (argument 1) is stored.
If we are passing argument 1 as self::USER_BASIC_DETAIL_FIELDS then with the same behavior of array_push it will try to modify constant USER_BASIC_DETAIL_FIELDS of a class which will create mess for developer
Am I right?
The answer is: everything depends on the details of your project.
You can't modify the value of constants.
And the '$r' variable is not a pointer to 'self::USER_BASIC_DETAIL_FIELDS' it is a copy of 'self::USER_BASIC_DETAIL_FIELDS'.
I think than you need a static variable instead of constant in that case.
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.
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.
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