php

How to check if a variable is empty in PHP

In this tutorial, we are going to see how to check if a variable is empty in PHP. You can use PHP’s empty() function to find out if a variable is empty or not. A variable is considered empty if it does not exist or if its value is equal to FALSE.

 

How to check if a variable is empty in PHP
<?php
	$v1 = '';
	$v2 = 0;
	$v3 = FALSE;
	$v4 = NULL;
	 
	if(empty($v1)){
		echo '$v1 is empty.'. '<br />';
	}
	 
	if(empty($v2)){
		echo '$v2 is empty.'. '<br />';
	}
	 
	if(empty($v3)){
		echo '$v3 is empty.'. '<br />';
	}
	 
	if(empty($v4)){
		echo '$v4 is empty.'. '<br />';
	}
?>

Output:

$v1 is empty.
$v2 is empty.
$v3 is empty.
$v4 is empty.

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *