Jump to content

Shorthand variable condition


Go to solution Solved by Christian F.,

Recommended Posts

Basically, that is testing to see if the $variable has a value which is considered to have a value of true. However, this doesn't mean that the $variable can only have a boolean (true/false) value.

 

The following values will NOT execute the code within the {}

 

false - boolean

0 - int

"0" - string

0.0 - float

 

If I've missed any then I'm sure others will pitch in. I personally prefer empty() and will only use the code above if I know it can only be true or false.

What I have noticed is that if the variable is not set it will throw a notice error. Is it concidered unprofessional or incorrect if I use the following method instead of using isset() first?

if(@$variable){
    // do stuff
}
Edited by php_nub_qq
  • Solution

Yes, to both, actually. You will never want to suppress errors like that. Not only because the errors can be important, but also because it doesn't actually stop them being thrown, just displayed. Meaning you're wasting resources.

 

If there is a possibility that a variable can not be set before testing it, always pre-empt the testing of the value with isset. This tells not only the PHP engine that the variable may not be set, but also anyone else looking at the code will know that this is by design and that you're aware of what you're doing. (More or less, at least. :P)

 

That said, there aren't many times that it should be necessary to use isset like that. Mostly it'll only be used for the $_GET, $_COOKIE and $_POST arrays (and their indices). Seeing as it's the user who chooses whether or not these are set, and not you. All other variables are in your complete control, and should thus be initialized before used/tested. That way you ensure that the variable exists, and that it has a predicable state which causes no error/warnings/notices to be thrown.

The $_SESSION array is a slight deviation from the above rule, as its content is controlled by your code, but whether or not it's populated might depend upon user-action. In those cases you'd probably want to use isset to verify it first.

The variable in question is indeed the post variable, and I asked the previous question exactly because I intend to optimize resource usage and thus I thought error suppressing was less consuming than executing an isset function. Apparently the lack of education has spoken and your more than great explanation has proved that. Big appreciation!

I have been using it quite a lot and it seems to be working well but I'm not aware of what exactly does it do?

 

If you see something you don't understand, take the time to figure out what it is doing instead of using it "quite a lot".

 

However, I personally don't like using "@" to suppress errors and instead to handle them gracefully. In this case you could use empty() which works very similar to just using the variable as the condition - but empty() won't generate an error if the variable doesn't exist.

 

Here is the list of things that will cause empty() to return true

 

  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • $var; (a variable declared, but without a value)

 

I believe most/all of those would generate the same result as how you are using it now - not 100% sure on the last two or the "0" as a string. But, those would probably not be applicable in 99% of scenarios.

You're most welcome, glad I could help. :)

 

Added:

Slight update, in response to Psycho's post:

php > $var;
php > var_dump (empty ($var));
bool(true)
php > var_dump ($var);
Notice: Undefined variable: var in php shell code on line 1
NULL

php > $var = ;
Parse error: syntax error, unexpected ';' in php shell code on line 1

php > $var = NULL;
php > var_dump ($var);var_dump (empty ($var));
NULL
bool(true)

php > $var = '0';
php > var_dump ($var);var_dump (empty ($var));
string(1) "0"
bool(true)

php > $var = array ();
php > var_dump ($var);var_dump (empty ($var));
array(0) {
}
bool(true)
As you can see, all the conditions listed are indeed true. Though, there is no way (that I'm aware over) to declare a variable without giving it content in PHP.

Unless we're talking about properties, but that's a whole different ballpark. :P

Edited by Christian F.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.