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.
Related
So I've got a form with a modal, that modal has 3 rows with 2 text fields each, if the user (me in this prod case) fills out only 2 rows, and leave the other row empty, that 3rd value should be NULL.
In my script I've got:
if (!is_null($_POST['packageDependencies']['bundle'][2])) {
$packageDependency3 = $_POST['packageDependencies']['bundle'][2] . "|" . $_POST['packageDependencies']['version'][2] . "|" . $_POST['packageDependencies']['repository'][2];
$depends = "<key>dependencies</key>
<array>
<string>$packageDependency1</string>
<string>$packageDependency2</string>
<string>$packageDependency3</string>
</array>
";
}
So I'm checking if (!is_null($3rdRow)) { //Do this }, but the variable $_POST['packageDependencies']['bundle'][2] is in fact NULL, as I use var_dump($_POST['packageDependencies']['bundle'][2]); and I get NULL printed to the page, but the if statement is still processing as if it isn't NULL.
$depends gets fwrite() to an XML file, and when I open it, I only see || and but that shouldn't be there as the variable is NULL as I entered no values into those input fields.
Given my advice, a more complete solution would be:
if (!empty(trim($_POST['packageDependencies']['bundle'][2]))) {
NULL is a specific state of a variable that involves the way PHP associates the name of a variable with a variable location. You can think of it like a flag, that indicates a variable name exists, but there is no storage location associated with it. There are a number of situations that empty with trim will catch that will bypass a check against null.
Even though !empty() did the trick, I've decided to use == to be less ambiguous. The answers found here are quite intuitive.
EDIT: As per #gview, adding (!empty(trim($var))) is the best bet as if a user accidentally presses the space key after a tab, it will avoid any errors.
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 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.
I have a method that takes an array as an argument, and returns true or false depending on the presence of a particular value.
In this scenario how many test cases should be written?
I think 3:
If the value is present
If the value is not present
If the array is empty (could be covered by 2 though?? )
I can think of 3 test cases:
If the array is not empty (or not null)
If the value is valid or not (I can pass an object where it expects a string :) )
If the value is present in array
It is the code of the function you want to test, so you cannot tell how many test cases are useful. Think again what your code does, how will the value be found?
An example: If your code tries to find a value with a certain name, and you make a string comparison, then think of the problems that can arise with string comparisons -> should the key be found case (in)sensitive, is null equal to an empty string, how does it handle duplicates and are other types converted correctly to strings (type juggling)?
I have some code that pulls database primary keys and iterates through them by calling a function.
When I get the key from the database and do a is_int($key) it returns true.
I then call a function: thisfunction($key)
In the calling function, I made it so that you could pass in the $key and that function loads the row for that key or you can pass the row as an object. At the beginning of the called function, it checks to see if $key is_int. It is returning false when I am calling it with an integer value.
Everything you get from database is string.
That means, even if you have database with INTs for columns, you are going to get them like:
id, name, age
array("43", "Rok", "19");
Since everything you fetch from the database is a type string, you could try using is_numeric() instead of is_int().
Perhaps try the ctype_digit() function.