...

View Full Version : Check if a variable exists



Dalziel
04-10-2003, 05:24 PM
if (FILE_EXISTS)

Is there any way to check "if (VARIABLE_EXISTS)"?

Weirdan
04-10-2003, 05:40 PM
if(isset($var)) do_something();

missing-score
04-10-2003, 06:20 PM
You can just put

if($var)

to check if a variable exists. :thumbsup:

Dalziel
04-10-2003, 06:25 PM
Thanks

brothercake
04-10-2003, 08:37 PM
Originally posted by missing-score
You can just put

if($var)

to check if a variable exists. :thumbsup:
That's only true if you have "~E_NOTICE" in your error handling config. If you are showing notices then you still get a warning for non-existent vars, unless you test for isset()

I don't know for a fact, but I presume that strict coding is more efficient than loose, as it is with JS. Can anyone confirm or refute that?

missing-score
04-10-2003, 08:51 PM
I use strict coding aswell, and I do always use isset(), because it makes it easier to read.

I was just saying that you can test for variables like that. :thumbsup:

Sorry if you misunderstood! Its not my day today :(

TFP
04-10-2003, 11:32 PM
If you want to look if the field of a form is filled out you should use the empty-function


if (empty($var))

because the variable is set but the value is NULL


TFP

missing-score
04-10-2003, 11:57 PM
Nice one, I forgot all about empty function. :rolleyes:

firepages
04-11-2003, 05:46 AM
Originally posted by brothercake
I don't know for a fact, but I presume that strict coding is more efficient than loose, as it is with JS. Can anyone confirm or refute that?

Yes , as you say PHP will generate a warning if you go if($var) and $var is undefined.
The fact that PHP has to generate that warning (printed to screen or otherwiase) technically makes the script less efficient, we are talking millisecond or part of I am sure, but true non the less.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum