How to Check if a String is Null in PHP - onlyxcodes

Friday 24 May 2024

How to Check if a String is Null in PHP

Hi PHP coder, In this tutorial I will explain how to check if a string is Null in PHP. Checking for null values in string variables is a common task in PHP development. 


For developers, handling null strings in PHP is essential. Knowing how to determine whether a string is null can save you a lot of trouble when handling user input, processing data from APIs, or working with databases.


how to check if string is null in php

Using the is_null() Function:

The simplest and most direct method for figuring out whether a string in PHP is null is to use is_null(). This function returns false otherwise; it returns true if a variable is deemed null. 


In this example, I used the is_null() method to check the null value after assigning true, false, and null parameters to the $str variable.


<?php 

//determine for true value of the $str variable for null;

$str = true;

if (is_null($str))
{
	echo 'Variable str blank is blank';
}
else
{
	echo 'Variable str is not blank';
}

echo nl2br("\n");


// check for false value of the $str variable for null";

$str = false;

if (is_null($str))
{
	echo 'Variable str is empty' ;
}
else
{
	echo 'Variable str is not empty';
}

echo nl2br("\n");


// Check for null value of the $str variable for null";

$str = null;

if(is_null($str)) 
{
    echo "The string is null";
} 
else 
{
    echo "The string is not null";
}

?>

Output:


Variable str is not blank
Variable str is not empty
The string is null

Note: The nl2br() function places HTML line breaks ("br />") at the start of each new line ("\n").


Using empty() Function:

Another method to check if any strings in PHP are null or empty is to use the empty() function. In addition to testing for null, this function considers values such as 0, false, ' ', array(), and null as empty. 


Example:


Three variables ($var1, $var2, $var3) in this PHP application have values of 0, ' ', and null.


The empty() function is used to check whether the values of these variables are blank; if they are, the program returns true.


<?php

$var1 = 0;

$var2 = "";

$var3 = null;

$check1 = empty($var1);
$check2 = empty($var2);
$check3 = empty($var3);

echo "Result of all variables is shown below.<br /><br />";

if ($check1) 
{
	echo "true.<br>";
}

if ($check2) 
{
	echo "true.<br>";
}

if ($check3) 
{
	echo "true.<br>";
}


?>

Output:


Result of all variables is shown below.

true.
true.
true.

In this PHP program, I also checked the variable value not blank using the empty() function.


<?php


// check variable value is not empty 

$newvar = 'hello';

if(empty($newvar))
{
	echo 'variable value is empty';
}
else
{
	echo 'variable value is not empty';
}

?>

Output:


variable value is not empty

Using Strict Comparison:

PHP includes two strict comparison operators (!== and ===) that compare not only values but also data types. The strict comparison operator can be used to reliably verify whether a string is blank without requiring the mixing of types. 


For instance: 


In this example, I checked to see if the two variables' numerical values matched by using the == operator. 


If it is found, it will return true; if not, it will return false. 


<?php

//compare only numerical values using two variables

$var1 = 2;
$var2 = 5;

if($var1==$var2)
{
	echo "TRUE : Because both variable's values are same \n";
}
else
{
	echo "FALSE : Because both variable's values are not same \n";
}


?>

Output:


FALSE : Because both variable's values are not same

I used the == operator in this PHP program to check if the string values of the two variables are equal. 


If it is located, it will return true; if not, it will return false. 


<?php


//check to compare two string values

$var1 = "google";
$var2 = "google";

if($var1==$var2)
{
	echo "TRUE : Because both variables' string values are the same \n";
}
else
{
	echo "FALSE : Because both variables' string values are not same \n";
}

?>

Output:


TRUE : Because both variables' string values are the same

Checking with strlen() Function:

Another method to identify null or missing strings is to use the strlen() function, which returns the length of the string. When a string has zero length, it means it is nothing or undefined.


For instance:


Using the strlen() method, I verified that the value of the $str variable in this PHP application is not blank.


In this PHP example, I used the === operator to determine whether the variable value is equal to zero or not. If found then it will return null otherwise it will return not null.


<?php

$str = '';

if (strlen($str) === 0) 
{
    echo "The string is null.";
} 
else 
{
    echo "The string is not null.";
}

?>

Output:


The string is null.

In this PHP program, the $str variable holds the value of 'hello world', so the output shows the string is not null.


<?php


$str = "hello world"; 

if (strlen($str) === 0) 
{
    echo "The string is null.";
} 
else 
{
    echo "The string is not null.";
}

?>

Output:


The string is not null.

Conclusion:

In conclusion, a key component of web development is using PHP to handle empty strings. Functions such as is_null()empty(), strict comparison, and strlen() allow you to precisely determine whether a string is blank and adjust the behavior of your program accordingly. Gaining knowledge of these techniques will enable you to develop PHP apps that are more reliable and error-free.

No comments:

Post a Comment

Post Bottom Ad