1

I have the following code to check user input:

if(isset($_POST['block_name']) && !empty($_POST['block_name'])) {
    $block->name = trim($_POST['block_name']);
}

But it accepts SPACE as valid input, so I changed to this:

if(isset($_POST['block_name']) && !empty($_POST['block_name']) && trim($_POST['block_name'])!='') {
        $block->name = trim($_POST['block_name']);
    }

I have found on web that I can use !=false as well. What is the difference and which is recommended.

1
  • Please read the documentation on empty first. Your question regarding false is also answered in the manual. Commented Jan 28, 2013 at 11:35

5 Answers 5

5

'' equals false in a loose comparison, so there's no real difference. Since '0' also equals false though, you may want to make your check more strict. empty is also just checking for false without triggering an error if the variable is not set. So isset && !empty is redundant to begin with.

Use:

if (isset($_POST['block_name']) && strlen(trim($_POST['block_name'])) > 0)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. I didn`t know trim($_POST['name']) do not accept 0. So now I am using if(isset($_POST['block_name']) && trim($_POST['block_name'])!='') so that 0 can also be accepted as a single character. Is there anything not so good in my code now?
0

you can check for the blank space with

  if( (strlen($string) == 1 ) && strpos(trim($string)," ")){

   echo "the string contains only a blank space";

  }

2 Comments

What is this supposed to achieve?
@Jon wants the OP to check if the string has only a blank space inside?
0

if(empty($_POST['block_name'])) should check:

"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)

So, this should be best to go with: if(!empty(trim($_POST['block_name']))) { //proceed...

6 Comments

I don't think empty works with expressions :) The manual says that only 5.5 supports them
Fatal error: Can't use function return value in write context
@jCloud .. it's good to assign $blockName = $_POST['block_name'] in a variable and then try empty actually if(!empty($blockName)).
what will happen if $_POST['block_name'] is not set
if it's not set, if condition will not satisfy.
|
0

Use this conditional statement. I hope it will work.

if `$custSTR` is a sample string having spaces.

$custSTR = '     ';
if (strlen(trim(preg_replace('/\xc2\xa0/',' ',$custSTR))) == 0)
{
    echo "custSTR have space or empty.\n";
}

Comments

0

After all I think this way is the best:

if(isset($_POST['block_name']) && strlen(trim($_POST['block_name'])) > 1) {
        $block->name = trim($_POST['block_name']);
    }
    else {
        $errors[] = "Please give a name for the new block. It should be at least 2 characters";
    }

Thanks for deceze for his answer.

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.