1

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.

5
  • If you are just checking to see if there isn't user input then try empty() Commented May 18, 2018 at 17:22
  • 1
    try empty() instead Commented May 18, 2018 at 17:22
  • @WillardSolutions !empty() seems to do the trick, but what's up with !is_null() not working? Commented May 18, 2018 at 17:25
  • There are some great answers here that will settle your query Commented May 18, 2018 at 17:26
  • @Martin Thanks for the info, I've decided to use == operands to be less ambiguous as one of those posts states, and I'll get in the practice of doing that instead of using is_null() and empty() from now on. Commented May 18, 2018 at 17:30

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This makes sense, as trim() removes whitespaces, and if a user accidentally hits the space key within one of the fields but doesn't notice, it won't cause issues. I'll add the trim() function in, thank you so much! I learn something new everyday!
0

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.

Comments

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.