/    Sign up×
Community /Pin to ProfileBookmark

Is This How You Check If $Var Value Exists Or Not ?

Hi,

Like this I just checked if a $var value exists or not.

1

[code]

//Result: Warning: Undefined variable $var in C:xampphtdocsWorkgulfTest.php on line 10
if(!$var)
{
die(‘No VAR’); echo ‘<br>’; //Triggered.
}
else
{
die(‘VAR’); echo ‘<br>’;
}

[/code]

2

[code]

$var = ”;

if(!$var)
{
die(‘No VAR’); echo ‘<br>’; //Triggered.
}
else
{
die(‘VAR’); echo ‘<br>’;
}

[/code]

3

[code]

$var = “”;

if(!$var)
{
die(‘No VAR’); echo ‘<br>’; //Triggered.
}
else
{
die(‘VAR’); echo ‘<br>’;
}

[/code]

4

[code]

$var = NULL;

if(!$var)
{
die(‘No VAR’); echo ‘<br>’; //Triggered.
}
else
{
die(‘VAR’); echo ‘<br>’;
}

[/code]

5

[code]

$var = ‘NULL’;

if(!$var)
{
die(‘No VAR’); echo ‘<br>’;
}
else
{
die(‘VAR’); echo ‘<br>’; //Triggered.
}

[/code]

6

[code]

$var = “NULL”;

if(!$var)
{
die(‘No VAR’); echo ‘<br>’;
}
else
{
die(‘VAR’); echo ‘<br>’; //Triggered.
}

[/code]

From the looks of the first example, it seems like this you can test if the $var itself exists or not. Meaning, does a var exist in that name or not. Right ?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmMay 17.2022 — Instead of presenting us with this beautiful display of code that does nothing why not go to google and entering "php test if var exists"? You'll get your answer.
Copy linkTweet thisAlerts:
@novice2022authorMay 17.2022 — @ginerjm#1644111

I presented so you can confirm my test was correctly done.

And if you confirm then other newbies can use my method to learn how to test aswell.

I had forgotten the following two-three times before and only get reminded after re-tests.

Just retested and releanred for the 3-5th time that these are the same ...
<i>
</i>if(!$var)



<i>
</i>if(EMPTY($var)


With the abpve you will get TRUE if the $var either does not exist or exists but has no value.

But if you want to solely test if a $var exists or not then you test with this ....

<i>
</i>if(ISSET($var)


Correct ?

Now gonna test with these ....

<i>
</i>if($var==FALSE)



<i>
</i>if($var===FALSE)


But I will need some help on how to test the final one. How to create the figures. The equation.
Copy linkTweet thisAlerts:
@novice2022authorMay 17.2022 — Hi,

I am testing ===FALSE.

This is how I did it ....

<i>
</i>
//Warning: Undefined variable $var in C:xampphtdocsWorkgulfTest.php on line 15
if($var===FALSE)
{
die('NO VAR'); echo '&lt;br&gt;';
}
else
{
die('VAR'); echo '&lt;br&gt;'; //Triggered.
}



<i>
</i>
$var = '';

if($var===FALSE)
{
die('NO VAR'); echo '&lt;br&gt;';
}
else
{
die('VAR'); echo '&lt;br&gt;'; //Triggered.
}



<i>
</i>
$var = "";

if($var===FALSE)
{
die('NO VAR'); echo '&lt;br&gt;';
}
else
{
die('VAR'); echo '&lt;br&gt;'; //Triggered.
}



<i>
</i>
$var = NULL;

if($var===FALSE)
{
die('NO VAR'); echo '&lt;br&gt;';
}
else
{
die('VAR'); echo '&lt;br&gt;'; //Triggered.
}



<i>
</i>
$var = 'NULL';

if($var===FALSE)
{
die('NO VAR'); echo '&lt;br&gt;';
}
else
{
die('VAR'); echo '&lt;br&gt;'; //Triggered.
}



<i>
</i>
$var = "NULL";

if($var===FALSE)
{
die('NO VAR'); echo '&lt;br&gt;';
}
else
{
die('VAR'); echo '&lt;br&gt;'; //Triggered.
}



Did I test correctly ?

I do not understand why all test result of the 6 are the same.

Since on the first test, $var doesn't exist, then should not the === FALSE trigger ? It did not become TRUE.

And, since the remaining 5 tests got their $var undefined then should not they show TRUE for $var ===FALSE.

??? Result proved otherwise.

I get the feeling I did the test wrong.

I know with the 3 equal signs you not only test if the equation is equal irnot but whether the equated vars are same data type or not. That's all I know abut the: three equal sign operator.
Copy linkTweet thisAlerts:
@NogDogMay 17.2022 — Start by deciding and defining exactly what you want to know. There is no one way that works in every situation. You also have to be careful dealing with PHP's "loose typing", and understand the difference between the == and != loosely typed operators and the strictly typed === and !== operators.

PHP's isset() function will return true if a variable is set _and is not explicitly set to null,_, and otherwise will return false. The empty() function will return true if it is not set at all, is null, is false...but also if "false-y", as it is loosely typed. For example, if you set $x = 0; then call if(!empty($x)) {...}, that if block will not execute since integer 0 is "false-y" in PHP, as is a zero-length string.

> @novice2022#1644113 $var = 'NULL';

Note that that does not set $var to be an actual null type, it just sets it to the string that has those letters in it -- it is not actually a null.
×

Success!

Help @novice2022 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...