개발 꿀팁/PHP

[PHP] Check for the existence of a variable, Isset() Empty()

Jammie 2018. 1. 19. 12:29
반응형

Let's look at how to check the value of a variable in the PHP language. What if you want to check if a variable has a value? In this case, use the isset() or empty() function as a usable function.


isset (name of the variable to check);

empty (the name of the variable to check);


* Find isset() and empty() functions

Both of the above functions allow you to determine whether or not a variable is value...it means whether you have it. What is the difference between the two? It's as follows.



The isset() function returns the variable to a boolean value of value of value of value and, if it is, true if it is present and not a null value. The empty() function returns true if no value exists, or if the value of the variable is equal to 0 or false, or a null value.


* isset() View example source code


The following example uses isset() to check the value of a variable.

<? php

   if (isset ($test)) {

     echo '$test variable has a value;

   }

   else {

     echo '$test variable does not have a value!

   }

?>


Now we will learn about the empty() function.


* empty() View example source code


empty() Similar to the role, but the empty() function has a distinct difference from isset() because it returns true even if the variable has a value of false or zero.


<? php

   if (empty ($test2)) {

     echo 'The $test2 variable has no value or has a value of 0, false, or null;

   }

   else {

     echo '$test2 variable has a value!

   }

?>


So far we have just briefly covered an example of a function of empty().



* Note

What if I want to test the opposite case?


!empty ();

!isset ();


If you want to apply the opposite value to the if statement, you can use it like this:

if (!empty ('test')) {

   ...

}

if (!isset ('test')) {

   ...

}


You can use! To get the opposite value. If you use an exclamation point in front of a function like this, you can see the opposite case.



반응형