I have a php object that has a key=>value with something like [ipAddress] = 'NULL'
and if I do:
if(isset($object->ipAddress)){
echo "I am set!!!";
}
It never echoes. because apparently it's not "Set." I was under them impression that it is set, because oft he word NULL.
Is there a away to get around this to say, you are set? with out actually giving it a value? I ask because I attempted to write a function like this: (Don't mind the debugging, its the debugging that lead me to this question)
private function checkForColumnInModelObject($modelObject, $column, $custom_name){
$relationship = array();
foreach($modelObject as $model){
if(isset($model->$column)){
$value_returned = $model->$column;
var_dump($column);
var_dump($custom_name);
var_dump($value_returned);
//$relationship[$custom_name] = $value_returned;
}
}
//return $this->toObj($relationship);
}
So what I am trying to do here is check for a column in a model object. Now you might be given an array of columns, which we walk through in a function that calls this one, and an array of different model objects. were trying to see if the model object has that column.
So for example:
Does equipmentModel have ipAddress Column? yes? fetch me the value.
and the way we do this is by saying "is the column on this model set". The problem is, we might have columns with NULL value ... hypothetically their set, their value is just null, but PHP's isset() is all like NO, you are not set.
Any ideas on how I could write this to keep the same logic, BUT allow values of null to pass through assuming that model has that particular column?
If you want to know if an object property exists, regardless of its value, you can use property_exists.
if (property_exists($model, $column) {
...
}
isset returns true whenever you do an assignment to some variable. When you do $somevar=NULL; (In this case it is an assignment), if(isset($somevar) { echo "Inside"; } , The "Inside" will never print. Since NULL is never considered a value.
Related
I want to create a function that won't create errors if it's passed arguments that aren't valid array keys, and successfully check if the argument is set as a key in an array.
static function IsAwesome($name) {
return isset(self::$_awesomeThings[$name]);
}
This creates a lovely message when someone passes, say, an object as $name:
Warning: Illegal offset type in isset or empty in ...
What's the simplest way to avoid this behavior without excluding potentially valid keys, like true, for example? Assume existing code that can't be changed already relies on this behavior.
maybe you need something like:
static function IsAwesome($name) {
return array_key_exists((string)$name, self::$_awesomeThings);
}
http://php.net/manual/ru/function.array-key-exists.php
I was wondering if there is any difference when setting default array value to be an empty array or NULL.
For example:
function arrayTest(array $array = array()) {
if(!empty($array)) {
// Do something
}
}
or
function arrayTest(array $array = NULL) {
if(!empty($array)) {
// Do something
}
}
What I noticed is that first example doesn't allow NULL values to be passed and the second example does because of type casting.
Any other differences? Which one should be used?
The other difference is that if you don't pass an argument , it will default to array() or null, which are two very distinct values. You can check for that of course, but you will need to take it into account. empty will work for both, but a foreach loop over null won't work that well, and various array functions will also fail.
What you noticed is correct: Passing null for a typehinted argument only works if you add = null to the declaration. This is not only true for arrays but for objects as well. In PHP there is no way in PHP to make a typehinted argument that is mandatory but can be null. As soon as you add =null to the declaration, you can pass null but you can also omit the parameter.
If to you there is no logical difference between an empty array or null, I would choose the first method of defaulting to an empty array. Then at least you'll know that the input is always an array. I think that add clarity to both the implementer of the function and the programmer(s) who use it. But I guess that's just an opinion and not even a strong one.
My main advice would be to not make the argument optional at all. In my experience this will make the usage of such functions unclear, especially as the code grows and more arguments are added.
I am discovering that php is touchy when comparing types, especially when it comes to 0 because it is considered false and thus (I think) empty.
Is a variable containing 0 considered empty?
I have written a class in php that takes a stdClass and is able to manipulate it in different ways like edit, duplicate arrays, delete, etc. I have no idea of how the stdClass is made up. In the class I have a method that takes the address to the property in the stdClass and changes the property value to the new value and has to compare the property value with the new value at the end. Most of the values are entered or chosen by the user in the browser by selecting an array key. The key may be numerical or text.
In the method I have:
if (empty($address) || empty($new_value)){
$this->last_error = "Unknown error";
return false;
}
empty($new_value) does not work when the value is 0. 0 in this case has been the array key. Is there a way to make this work for all possible situations.
Somewhere at the very end I have:
if ($new_value==$this->get_property ($address)) { return true; } else { return false; }
I think php is seeing this as: if (0==0){} Is this true?
This does not work when the property is set to zero.
I have data that was imported from our mainframe and the last column in the table has null values. (varchar, allow nulls checked. obv.) The field name is all in caps (a result of the mainframe data dump... this is an important clue). When I try to retrieve and echo the data from that field, I get an "UNDEFINED INDEX" error if the field value is Null. If the field has data, I'm fine.
HOWEVER: if i rename the field to something with a lower case letter at the beginning, it works fine, nulls or not.
NOTE: if i put a number at the beginning of the field it doesn't work either.
Trying to find a way around this since I'm dealing with a LOT of tables that are going to get dumped and re-created on an almost daily basis from these mainframe extracts, id rather not have to change the field names. Any help would be GREATLY appreciated!
EDIT: I tried to use "isset" but you cannot check isset on a $XXX->fields('fieldname') line of code. Tried using if(!($XXX->fields('fieldname')) also doesn't work.
In the absence of your ability to use isset(), you can compare if it is exactly NULL with three equals signs (identical operator):
if ( $XXX->fields('fieldname') === NULL ) {
// NULL value, do something else...
} else {
// Not NULL, try something...
}
That doesn't solve your undefined index problem, however. Did you write the class that produces ->fields('fieldname')? If so, or even if not, you can modify the class function fields() to check isset() there and return NULL if not set, or the value if set.
Based on your code in the comments, you might try replacing:
return $this->fields[$this->bind[strtoupper($colname)]];
with:
return ( isset( $this->fields[$this->bind[strtoupper($colname)]] ) ? $this->fields[$this->bind[strtoupper($colname)]] : NULL );
This checks if $this->fields[ ... ] is set, and if so, returns it, and if not, returns null.
But, if that doesn't work, try replacing the same with:
return ( isset( $this->bind[strtoupper($colname)] ) ? $this->fields[$this->bind[strtoupper($colname)]] : NULL );
This checks if $this->bind[ ... ] is set, and if so, returns it, and if not, returns null.
I am working on a function that takes a string as an argument, which I need to use to get the value of a variable of the same name.
For example
If the string $foo = "$_POST['email']"; is passed I need to retrieve the value of $_POST['email'] and store it as $example, so that $_POST['email'] == $example would return true. There is no feasable way of passing the value directly to function (the variable has to be dynamic to make the function worthwhile).
I did think of using variable variables but they don't work with super globals which will be the primary source of the values I need.
Basically is there a way to get the value of a variable (usually in a superglobal array) with the needed variable as a string?
Let me know if more detail is needed.
Thanks
Variable variables would be the answer, but if you're after fetching values from $_POST array, why not pass just a key to a function?
Note: ths function is provided just for example, my actual recommendation is below.
function fetchFromPost($key) {
if(isset($_POST[$key]) {
return $_POST[$key];
} else {
return null; //or do whatever you want to do in case key is not found
}
}
In fact filter_input(), which allows you to chose an input array, a key and a sanitaion filter to be used, is already there for you
It is possible, although I seriously doubt you should use this. It allows for PHP injection and makes your source code very vulnerable if you don't know where the string came from:
<?
function eval_expression($expression)
{
eval("\$temp = $expression;");
return $temp;
}
// Usage:
echo eval_expression("\$_GET['plop']");