php

How to check if a variable is undefined in PHP

In this tutorial, we are going to see how to check if a variable is undefined in PHP. You can use PHP’s isset() function to check if a variable is set or not. isset() will return FALSE if you are testing a variable that has been set to NULL.

 

How to check if a variable is undefined in PHP
<?php
	$foo = 'lorem ipsum';

	if(isset($foo)){
		echo 'foo is defined' . '<br />';
	}else{
		echo "foo is undefined" . '<br />';
	}

	if(isset($bar)){
		echo 'bar is defined' . '<br />';
	}else{
		echo "bar is undefined" . '<br />';
	}
?>

Output:

foo is defined
bar is undefined

 
[st_adsense] 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 *