PHP: why is this null different from the other null - php

hi i tried 2 things what should be the same however it my testing says different does anyone know why the only thing i do is put it in a variable...
if ($_SESSION[$something] === null)
echo("this is null");
$_SESSION[$something] does not exists so it indeed says: "this is null".
now look at this
$theSession = $_SESSION[$something];
if ($theSession === null)
echo("this is null");
now it does not say "this is null" while it should be exactly the same right?

You need a $ in front of theSession in the second block of code.
You do may not need the $ in front of something. You only need it if $something is holding a string of the session variable name. Otherwise if something is the session variable name you dont need the $.

You should also consider using is_null to check is a variable contains a null value.

Related

Why does gettype() output "NULL" for an undefined variable?

I have tried the following code:
<?php
echo gettype($x);
?>
And I got the following output:
Why did gettype() outputted "NULL" after the error was displayed? I mean an undefined variable is a variable that doesn't exist and not a NULL variable, right?
The documentation of NULL explains:
The special NULL value represents a variable with no value. NULL is the only possible value of type null.
A variable is considered to be null if:
it has been assigned the constant NULL.
it has not been set to any value yet.
it has been unset().
Also check the table "Comparisons of $x with PHP functions".
You kinda did answer your own question.
At the moment you're trying to get the type of nothing it will be null by default.
For example
X = 5
It will return a integer
But in your case x wasn't initiated and therefore it stays null.
Vars work on the run and can be anything.
I hope this is somehow useful.
(if this contains a misconception please let me know what is wrong)

Why should Null value exist PHP?

I find it's really hard to understand the Null datatype in PHP. Why does it even need to exist? If it's only represent an empty value why PHP doesn't just offer a way to remove it from the code since the variable contains it doesn't have any useful thing to do in our application?
Consider this:
if (some_condition) $var1 = "Bob";
Now if that condition is false, then $var1 would be null because you did not define it. So, you could have the decision to check if it is null to proceed with what you want to do with it.
if (!is_null($var1)) {
// do something
}
Hope that helps.

Assigning NULL to a variable in PHP: what does that do?

In maintaining code, I'm encountering loops, where at the end of the loop several variables are set to NULL like so: $var = NULL;. From what I understand in the manual, NULL is meant mostly as something to compare against in PHP code. Since NULL has no type and is not a string or number, outputting it makes no sense.
I unfortunately cannot provide an example, but I think the NULL values are being written to a file in our code. My question is: does $var have a value after the assignment, and will echoing/writing it produce output?
EDIT: I have read the PHP manual entry on NULL. There is no need to post this: http://php.net/manual/en/language.types.null.php in a comment or answer, or top downvote me for not having RTM. Thank you!
[ghoti#pc ~]$ php -r '$i="foo"; print "ONE\n"; var_dump($i); unset($i); print "TWO\n"; var_dump($i); $i=NULL; print "THREE\n"; var_dump($i); print "\n"; if (isset($i)) print "Set.\n"; if (is_null($i)) print "is_null\n";'
ONE
string(3) "foo"
TWO
NULL
THREE
NULL
is_null
[ghoti#pc ~]$
The result of isset() will be boolean false, but the variable is still defined. The isset() function would be better named isnotnull(). :-P
Note that is_null() will also return true for a value that has never been set.
Yay PHP.
null is pretty much just like any other value in PHP (actually, it's also a different data type than string, int, etc.).
However, there is one important difference: isset($var) checks for the var to exist and have a non-null value.
If you plan to read the variable ever again before assigning a new value, unset() is the wrong way to do but assigning null is perfectly fine:
php > $a = null;
php > if($a) echo 'x';
php > unset($a);
php > if($a) echo 'x';
Notice: Undefined variable: a in php shell code on line 1
php >
As you can see, unset() actually deletes the variable, just like it never existed, while assigning null sets it to a specific value (and creates the variable if necessary).
A useful use-case of null is in default arguments when you want to know if it was provided or not and empty strings, zero, etc. are valid, too:
function foo($bar = null) {
if($bar === null) { ... }
}
Null in PHP means a variable were no value was assigned.
http://php.net/manual/en/language.types.null.php
A variable could be set to NULL to indicate that it does not contain a value. It makes sense if at some later point in the code the variable is checked for being NULL.
A variable might be explicitly set to NULL to release memory used by it. This makes sense if the variable consumes lots of memory (see this question).
Dry run the code and you might be able to figure out the exact reason.
It appears that the purpose of the null implementation based off of the information provide is to clear the variable.
You can unset a variable in PHP by setting it to NULL or using the function unset().
unset() destroys the specified variables.
The behavior of unset() inside of a function can vary depending on
what type of variable you are attempting to destroy.
If a globalized variable is unset() inside of a function, only the
local variable is destroyed. The variable in the calling environment
will retain the same value as before unset() was called.
Null is a special data type which can have only one value, which is itself. Which is to say null is not only a data type but also a keyword literal a variable of data type null is a variable that has no value assigned to it when a variable is created without a value it is automatically assigned a value of null this is so that whatever garbage was in that memory location before is cleared out otherwise the program may try to process it

In PHP, if a variable does not have a value, why does isset() still return TRUE?

Here is my code:
<?php
$ja = '';
if(isset($ja))
echo "cool!";
?>
I get a "cool!" when running this simple piece of code in my browser. I learned from php.net that
isset — Determine if a variable is set
and is not NULL
Well, in my code, I did declare the variable $ja, but I didn't add any value to it, so shouldn't it be "NULL"?
Even though '' seems like nothing, it still has a value (a NULL character at the end of the string).
isset() checks if the variable is set or not, which in the case (to ''), it is. You may want to set $ja to NULL first beforehand, instead of setting it to an empty string... or use empty() ;)
The empty string is still a value. so you did give it a value which is not null - '' is a perfectly normal string value. perhaps you want ! empty($ja)
Isset is used to tell whether a variable is set or not:
isset($notDefined) //false
$notDefined = 0;
isset($notDefined) //true
(Assuming that $notDefined hasn't been defined before)
To check whether the variable is empty you can use if(empty($var)) or if($var==0)
You did add value to $ja - you set it to an empty string. An empty string is not null.
What you may be confused with is that an empty string and null both evaluate to "false" in PHP when you cast it to Boolean.
PHP's documentation is fairly clear on usage of isset.
The isset function does determine whether or not an object has a value. "NULL" is truly the only way to give an object a value of nothing. $s = '' simply gives an output of nothing. BOOL values(true/false) says that it's yes or no... 0 simply gives the object a int value of 0.
As the name implies of the function, it checks if some variable has been set, in a sense not that it has some value, but in a sense that it has been created. I think the name could be a bit confusing so I will bring a javascript analogy. In javascript to check if the variable exists you do the following:
if (typeof(somevar) == "undefined")
alert("Sorry, the variable has not been set already")
else
alert("Congratulations, the variable has not been set")
So, what you are doing is that you are making a variable $ja, and since by doing so, the variable already exists and therefore has been set.
Hope this helps

Not set or empty string -- which is better for if it ends up displaying "none" to the user?

I am wondering if you use unset variables, empty strings (or 0's), or "None" to determine if a variable is "None"?
The case I'm thinking of is, I'm retrieving something from the database, but find that the value is not set for the record, usually determined by the fact that there are no records or a null value. This will display to the user as "None" or "Not Set".
So the question is, when passing this value to another part of the script (ie, another function, farter on the script, template, etc), do I:
not set the variable (and therefore check if it's set in the template)
set the variable to an empty string or 0 (and check for the empty string in the template)
set the variable to "None" or "Not Set" and just echo the variable
Is there one that you usually do and why do you do it?
(I'm using PHP, so the type of the variable is somewhat unimportant.)
I'm looking for a general answer; I know that it won't always be true, but a general rule to follow.
Where possible I would normally use the nil value in the language to map to NULL.
The other options you mention all have the potential for the same ambiguity problem you'd have in the database if the value were set to empty string, None, or Not Set, when you really mean NULL.
There is also the risk of this propagating back to the database if the user can update values.
The only option that I think is something I would not do is the third option. "None" or "Not Set" really seems like a UI descriptor and not really appropriate to set as the value to be interpreted by code later.
The only exception to this would be if you have a set of known values. If they are more or less constants.
NOT_SET
NO
YES
Where NOT_SET might be the default. In this case I would still not simply echo out "not set" to the user, though.
Generally, I find it's best to handle data structures consistently throughout the program. This means leaving variables that are unset in the database unchanged when I pass them to other parts of the program. I cast or check for boundary conditions as I go if a particular function is expecting the data in a differing format.
As you said yourself, the value "None" or "Not set" is only relevant as far as displaying it to the user. For all internal use the value should be the native NULL value. The human readable values should only be substituted by whichever functions render values into the output stream
In PHP, you can set the variable to FALSE or NULL and then say:
if($var === FALSE)
...
(Notice the three equal signs)
You should explicitly set the value.
I would recommend setting it to NULL. If you're fetching the value from a database, and it's NULL (SQL) there, most likely the PHP function to grab the record will return NULL (PHP) for that field.
You can compare your string with NULL in PHP as such:
if($myString === NULL) {
/* It's NULL! */
}
If you want a certain string to show instead of NULL (which, when casted to a string, equals ""), you can set it as such:
if($myString === NULL) {
$myString = 'None';
}
Take into consideration you may need to refactor your code for internationalization later on. If you use a user string throughout your code, it will be much harder to do this.
Simply not setting a variable is bad, because you could make a typo of the variable name and wonder why when you set it "None" still shows.

Categories