10

How to check whether a variable is set or not in PHP

 1 year ago
source link: https://www.laravelcode.com/post/how-to-check-whether-a-variable-is-set-or-not-in-php
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to check whether a variable is set or not in PHP

  1600 views

  2 years ago

PHP

Use the PHP isset() function

You can use the PHP isset() function to test whether a variable is set or not. The isset() will return FALSE if testing a variable that has been set to NULL.

Let's check out an example to understand how this function basically works:

<?php
$var1 = '';
if(isset($var1)){
    echo 'This line is printed, because the $var1 is set.';
}
echo "<br>";
 
$var2 = 'Hello World!';
if(isset($var2)){
    echo 'This line is printed, because the $var2 is set.';
}
echo "<br>";
 
// Unset the variable
unset($var2);
 
if(isset($var2)){
    echo 'This line is printed, because the $var2 is set.';
} else{
    echo 'This line is printed, because the $var2 is not set.';
}
echo "<br>";
 
$var3 = NULL;
if(isset($var3)){
    echo 'This line is printed, because the $var3 is set.';
} else{
    echo 'This line is printed, because the $var3 is not set.';
}
?>

i hope you like this small article.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK