php_nub_qq Posted April 5, 2013 Share Posted April 5, 2013 Hello I want to ask what does the following code do if($variable){ // do stuff } 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? Link to comment https://forums.phpfreaks.com/topic/276581-shorthand-variable-condition/ Share on other sites More sharing options...
exeTrix Posted April 5, 2013 Share Posted April 5, 2013 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. Link to comment https://forums.phpfreaks.com/topic/276581-shorthand-variable-condition/#findComment-1423119 Share on other sites More sharing options...
php_nub_qq Posted April 5, 2013 Author Share Posted April 5, 2013 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 } Link to comment https://forums.phpfreaks.com/topic/276581-shorthand-variable-condition/#findComment-1423125 Share on other sites More sharing options...
Christian F. Posted April 5, 2013 Share Posted April 5, 2013 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. ) 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. Link to comment https://forums.phpfreaks.com/topic/276581-shorthand-variable-condition/#findComment-1423133 Share on other sites More sharing options...
php_nub_qq Posted April 5, 2013 Author Share Posted April 5, 2013 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! Link to comment https://forums.phpfreaks.com/topic/276581-shorthand-variable-condition/#findComment-1423138 Share on other sites More sharing options...
Psycho Posted April 5, 2013 Share Posted April 5, 2013 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. Link to comment https://forums.phpfreaks.com/topic/276581-shorthand-variable-condition/#findComment-1423140 Share on other sites More sharing options...
Christian F. Posted April 5, 2013 Share Posted April 5, 2013 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. Link to comment https://forums.phpfreaks.com/topic/276581-shorthand-variable-condition/#findComment-1423141 Share on other sites More sharing options...
Jessica Posted April 5, 2013 Share Posted April 5, 2013 Though, there is no way (that I'm aware over) to declare a variable without giving it content in PHP.Psycho's post showed it. <?php $var; ?> Link to comment https://forums.phpfreaks.com/topic/276581-shorthand-variable-condition/#findComment-1423143 Share on other sites More sharing options...
Christian F. Posted April 5, 2013 Share Posted April 5, 2013 Already covered that in the first part of the code block I posted, and as you can see it results in an undefined variable. It is a simple statement, after all, not a declaration or assignment. Link to comment https://forums.phpfreaks.com/topic/276581-shorthand-variable-condition/#findComment-1423148 Share on other sites More sharing options...
Jessica Posted April 5, 2013 Share Posted April 5, 2013 FINE! Link to comment https://forums.phpfreaks.com/topic/276581-shorthand-variable-condition/#findComment-1423149 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.