Php 101: Php Finding Keys Within Sessions Using Isset() And_Key_Exists()

Author: Rob Guida

PHP 101: PHP FINDING KEYS WITHIN SESSIONS USING ISSET() AND ARRAY_KEY_EXISTS()

Written by: Robert Guida, guidaMedia.com, 2009.

The isset function is a staple utility of PHP to make sure sessions variables exist before accessing them. Unless you are familiar with session variables, even after reading about them on php.net, this lesson will help you gain a better understanding on how to check the existence of a session key.

We will test different uses of isset to see the results. Some you may expect, while others may surprise you. After looking at isset the lesson tests array_key_exists, to show how the function can be utilized in your code. We will look at simple single-depth sessions and multi-dimension sessions. By single-depth session I am referring to a session key that is at the first level, $_SESSION["key"]. I also refer to this key as a parent key, as it can have a sub-key, like $_SESSION["key"]["sub-key"]. I refer to the sub-key as a child key. This is also what I mean by a mult-dimension session, as it has multiple layers.

Just a few notes about the tests: The full test is at the end of the article. You can copy and paste that into you editor and run it. You can also run each test separately. Running the full script provides formatting to easily review the results, while the individual test do not. You will notice that I provide two results for each test, however, only one is possible. I do this because I take more of an experimental approach to explaining the differences to provide a definitive understanding of how to access session keys.

Last note: We are testing sessions, so if you do not already know, this remains set until your session ends; when you close your browser. To avoid having to close the browser a unset function is included, so the key in the session is dropped.

Test 1

This first test examines the typical use of isset. However, test 1a uses isset with a session key that is not set, and 1b sets the key and runs the same test.

echo "

Test 1a: Testing ISSET on a key at the first level. The key is not set.

";
if(isset($_SESSION["PHP_SESSION"])){
echo "

Yes, the key PHP is set, however it was never set.

";
}else{
echo "

No, the key PHP is not set.

";
}
echo "

Test 1b: Testing ISSET on a key at the first level. The key is set.

";
$_SESSION["PHP_SESSION"] = "5";
if(isset($_SESSION["PHP_SESSION"])){
echo "

Yes, the key PHP is set.

";
}else{
echo "

No, the key PHP is not set, however it is set.

";
}
unset($_SESSION["PHP_SESSION"]);

The results work as you probably expect. Test 1a returns "No", while 1b returns "Yes". 1a returns "No" because we have not yet set the key "PHP_SESSION". When it is set for 1b, isset finds the key is set.

So, let's continue testing, and see what happens when use isset with a child key that has a value.

Test 2

In this test we look at the results of using ISSET on a child key. We test if the child key is set, as well, if it is not set. Here again we get some expected results.

echo "

Test 2a: Testing ISSET on a child key. The child key is set.

";
$_SESSION["PHP_SESSION"]["ISSET"] = "Setting the child key, 'PHP SESSION ISSET'";
if(isset($_SESSION["PHP_SESSION"]["ISSET"])){
echo "

Yes, it appears the child key is set. The result is the same as test 2a.

";
}else{
echo "

The result is false.

";
}
echo "

Test 2b: Testing NOT ISSET on a child key. The child key is set.

";
if(!isset($_SESSION["PHP_SESSION"]["ISSET"])){
echo "

The result is false.

";
}else{
echo "

Yes, it appears the child key is set. The result is the same as test 2b.

";
}
unset($_SESSION["PHP_SESSION"]);

Test 2a results in "Yes", and so does test 2b. This is expected. So why test if not isset? To answer this, lets look at Test 3, and mix it up a little.

Test 3

This test uses the same tests from Test 2, but we unset the child key. Let see what our results are.

echo "

Test 3a: Testing ISSET on a child key. The child key is not set.

";
$_SESSION["PHP_SESSION"] = "Setting the parent key PHP SESSION";
if(isset($_SESSION["PHP_SESSION"]["ISSET"])){
echo "

Yes, it appears the child key is set.

";
}else{
echo "

The result is false.

";
}
echo "

Test 3b: Testing NOT ISSET on a child key. The child key is not set.

";
if(!isset($_SESSION["PHP_SESSION"]["ISSET"])){
echo "

The result is false.

";
}else{
echo "

Yes, it appears the child key is set.

";
}
unset($_SESSION["PHP_SESSION"]);

The results we get in test 3 are the same as test 2. Why is this? Let's look at the definition of the isset function from php.net:

"Evaluation goes from left to right and stops as soon as an unset variable is encountered."

What this means is, the isset function checks the parent key, then it looks for the child key, which is to the right of the parent. Thus, it looks left to right. Since the child key is not set, the search stops, and the isset value for the parent key is returned rather than the child. Since the parent value is set, the result is "true".

Test 2 and 3 shows us that using isset on a child key is not reliable, since the results you get are the same when it is set, as when it is not? This is where array_key_exists come in to the rescue.

Again, to ensure you have a definitive understanding of array_key_exists, we examine different uses of this function. Once done, you should know how to use array_key_exists.

Test 4

This test looks at using array_key_exists on a parent key, and then a child key.

echo "

Test 4a: Testing ARRAY_KEY_EXISTS on a parent key. The parent key is set.

";
$_SESSION["PHP_SESSION"]["AKE"] = "Setting the child key, 'PHP SESSION AKE'";
if(array_key_exists("PHP_SESSION", $_SESSION)){
echo "

Yes, the parent key exists.

";
}else{
echo "

The result is false.

";
}
echo "

Test 4b: Testing ARRAY_KEY_EXISTS on a child key. The child key is set.

";
if(array_key_exists("AKE", $_SESSION)){
echo "

The result is true, but ISSET does not exist at the parent key level.

";
}else{
echo "

No, the child key does not exists because we are searching the wrong level.

";
}

Test 4a returns true, while test 4b doesn't. In order to find the child key, the session and the parent key, $_SESSION["PHP_SESSION"], must be used in the search. The next test shows us how to successfully use array_key_exists.

Test 5

This example shows how to test the session for a child key. By including the parent key in the search, we find the child key.

echo "

Test 5: Testing ARRAY_KEY_EXISTS on a child key, using the parent key in the search. The child key is set.

";
if(array_key_exists("AKE", $_SESSION["PHP_SESSION"])){
echo "

Yes, the child key exists, because we are searching the parent key, PHP SESSION.

";
}else{
echo "

The result is false.

";
}

$string = "This is a string to see the results when isset and array_key_exists are used on non array type variables, such as
a string.";
echo "

Test 6a: What happens when we use isset on a string.

";
if(isset($string)){
echo "

The variable is set.

";
}else{
echo "

The result is false.

";
}
echo "

Test 6b: What happens when we use array_key_exists on a string.

";
if(array_key_exists("results", $string)){
echo "

The variable is set.

";
}else{
echo "

The result is false. This will result in an error if error_reporting is not set to zero.

";
}

The result: "Yes". As mentioned, since the parent key is used in the search, the child is found. By adding the parent key, it directs the function to search inside the key.

So, now you may be wondering, what to use, array_key_exists, or isset. It really comes down to efficiency, reliability and choice. See, if you think for consistency sake using array_key_exists is the right choice, there are a few things to challenge that. One is, it appears the industry standard to use is isset. At least in all the code I have seen and worked with, that is the case. So there is a reason for that.

Could it be that if you end up passing a string into an array_key_exists function that the following error will be generated?

Warning: array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object in Testing_ISSET_AKE_on_SESSIONS.php on line 95

That alone may be a good reason not to solely use array_key_exists. Isset does not throw an error. This test is included in the complete test at the end of the article.

I personally am used to using isset, because it is much easier to type. The only times I uses array_key_exists is when I am testing a child key. My formula, and my conclusion to this lesson is, I use isset whenever possible, but as soon as I need to test a child key, I use array_key_exists.

I hope this has helped you.

Here is the full testing code, or click here to go the actual PHP code.