Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shivamyadav's avatar

unable to validate the request?

Why it does not give an error when everything is okay? my code validator class

<?php 
class Validator
{
    public static function string($value)
    {
        return strlen($value == 0);
    }    
}

?>

my validate file

require 'Validator.php';
$config = require('config.php');
$db = new Database($config['database']);
// $validator = new Validator();
$heading = 'Create Note';
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    // echo var_dump($_POST['body']);

    $errors = [];
    if (Validator::string($_POST['body'])) { 
        $errors['body'] = 'A body is required';
        
    } 
0 likes
2 replies
thinkverse's avatar
Level 15

Place the condition outside of the strlen function call.

return strlen($value) == 0;
newbie360's avatar

be careful if $value isn't string, may be array XD

open tinker input

strlen([1,2,3]);

Please or to participate in this conversation.