STRINGS

Check String Value is Empty

This code Check String Value is Empty or not and Prints the value accordingly.

empty() method is a good way to check if the string is null or empty value.

About the Code:

  • Check String Value is Empty
  • This code will check if the input string value is empty
  • Execute this code in console using > php <filename>
  • We will use empty() method to check if string value is empty
  • empty() method can check empty string and null also.
  • PHP_EOL is used to print new line on the console


    $firstname = "John";
    $lastname = "";
    $address = null;

    echo "FirstName: $firstname and  LastName: $lastname and Address: $address" . PHP_EOL;

    if( empty( $firstname ) ) {
        echo "Input value FirstName: $firstname is Empty" . PHP_EOL;
    }else{
        echo "Input value FirstName: $firstname is NOT Empty" . PHP_EOL;
    }


    if( empty( $lastname ) ) {
        echo "Input value LastName: $lastname is Empty" . PHP_EOL;
    }else{
        echo "Input value LastName: $lastname is NOT Empty" . PHP_EOL;
    }

    if( empty( $address ) ) {
        echo "Input value Address: $address is Empty" . PHP_EOL;
    }else{
        echo "Input value Address: $address is NOT Empty" . PHP_EOL;
    }

Output:


Input Values FirstName: John and  LastName:  and Address:
Input value FirstName: John is NOT Empty
Input value LastName:  is Empty
Input value Address:  is Empty


Other Similar Functions:

  • is_null() – Finds whether a variable is NULL
  • is_float() – Finds whether the type of a variable is float
  • is_int() – Find whether the type of a variable is integer
  • is_string() – Find whether the type of a variable is string
  • is_object() – Finds whether a variable is an object
  • is_array() – Finds whether a variable is an array
  • is_bool() – Finds out whether a variable is a boolean
Tags

About the author

PHPTraining.com

Do you know we teach Free PHP Course which has tons of Exercise, Assignments, Code Editor, Quiz and Certification. Access the course from here.

If you liked this article, then please subscribe to our YouTube Channel for PHP video tutorials. You can also find us on Twitter and Facebook.

Add Comment

Click here to post a comment