0

I have a page that loads and creates an empty array:

$_SESSION['selectedItemIDs'] = array();

If the user hasn't added any selections that get stored in the array I then check the array and branch accordingly, but there appears to be some error in my logic/syntax that is failing here.

Here's where I test if the $_SESSION['selectedItemIDs'] is set but empty:

if (isset($_SESSION['selectedItemIDs']) && $_SESSION['selectedItemIDs'] !== '') {
    // update the selections in a database
} else {
    // nothing selection so just record this in a variable
$selectedItems = 'no selected items were made';
}

When testing this with no selections if I print the $_SESSION['selectedItemIDs'] array I get this:

[selectedItemIDs] => Array
    (
    )

however the $selectedItems variable is not being set - it's evaluating the if test as true when I would expect it to be false, but I'm obviously misunderstanding something here.

2
  • 1
    You can use count function to check array length Commented Oct 26, 2016 at 11:54
  • 1
    Well, array() is obviously not identical to '' Commented Oct 26, 2016 at 11:56

4 Answers 4

7

Use empty() function.

empty() - it will return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable.

Syntax

empty(var_name)

Return value

FALSE if var_name has a non-empty and non-zero value.

Value Type :

Boolean

List of empty things :

  • "0" (0 as a string)
  • 0 (0 as an integer)
  • "" (an empty string)
  • NULL
  • FALSE
  • "" (an empty string)
  • array() (an empty array)
  • $var_name; (a variable declared but without a value in a class)

Code

if (!empty($_SESSION['selectedItemIDs']) ) {
    // update the selections in a database
} else {
    // nothing selection so just record this in a variable
$selectedItems = 'no selected items were made';
}
Sign up to request clarification or add additional context in comments.

Comments

0
$_SESSION['selectedItemIDs'] = array();

this variable is already set, but is empty. isset($_SESSION['selectedItemIDs']) check if variable exists, even if it's nothing in.

To check if there is anything just use

empty($_SESSION['selectedItemIDs']);

PHP DOC - empty

Comments

0
if (!empty($_SESSION['selectedItemIDs'])) {
    // update the selections in a database
} else {
    // nothing selection so just record this in a variable
    $selectedItems = 'no selected items were made';
}

isset($_SESSION['selectedItemIDs']) => it will check is this parameter is set or not which is correct.

$_SESSION['selectedItemIDs'] !== '' => this will exactly compare that your parameter type is not '' but here i guess your selectedItemIDs is array so it let you go inside

4 Comments

Why should the OP "try this"? A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO.
isset($_SESSION['selectedItemIDs']) && $_SESSION['selectedItemIDs'] !== ''
You need to add an explanation to your answer, not in comments.
isset($_SESSION['selectedItemIDs']) => it will check is this parameter is set or not which is correct. $_SESSION['selectedItemIDs'] !== '' => this will exectly compare that your parameter type is not '' but here i guess your selectedItemIDs is array so it let you go inside Thanks
-1

You can check the count of this array.

if (isset($_SESSION['selectedItemIDs']) && count($_SESSION['selectedItemIDs']) > 0) {
    // update the selections in a database
} else {
    // nothing selection so just record this in a variable
$selectedItems = 'no selected items were made';
}

1 Comment

empty() does the same thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.