Please ensure Javascript is enabled for purposes of website accessibility knowledgebase.co.il - מדריכים - קטגוריה - PHP - כיצד לבדוק אם משתנה מוגדר או לא ב-PHP
אתם צופים ב: כיצד לבדוק אם משתנה מוגדר או לא ב-PHP

כיצד לבדוק אם משתנה מוגדר או לא ב-PHP

במדריך זה נלמד איך לבדוק אם משתנה מוגדר או לא ב-PHP.


פונקציות בשימוש:

()isset


ניתן להשתמש בפונקציה ()isset כדי לבדוק אם משתנה מוגדר או לא. ה-()isset תחזיר FALSE אם בודקים משתנה שהוגדר ל-NULL.

בפניכם דוגמא על אופן הביצוע:

<?php

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

echo "<br>";
 
$variable3 = NULL;

if(isset($variable3)){
    echo 'This line is printed, because the $variable3 is set.';
} else{
    echo 'This line is printed, because the $variable3 is not set.';
}

?>

פלט:

This line is printed, because the $variable1 is set.
This line is printed, because the $variable2 is set.
This line is printed, because the $variable2 is not set.
This line is printed, because the $variable3 is not set.

עליכם להתחבר על מנת להגיב בעמוד זה.