rhyspaterson Posted July 1, 2008 Share Posted July 1, 2008 Hey guys, I'm doing some SNMP stuff and i have to catch for errors as such: // Printer type if(snmp2_get($printers[$x]['name'], $community, $standard_printer_variables['type'], $timeout, $retry) == FALSE){ // Do nothing }else{ $printers[$x]['type'] = snmp2_get($printers[$x]['name'], $community, $standard_printer_variables['type'], $timeout, $retry); } This works fine, and if the SNMP reponse returns nothing it just skips it. However, i have to run the SNMP request twice as you can see. Is there any way check, in one statement, if the variable == FALSE and if not, store it? Say something like: // Printer type if(snmp2_get($printers[$x]['name'], $community, $standard_printer_variables['type'], $timeout, $retry) == FALSE (else store whatever was returned in $variable_returned)){ // Do nothing }else{ $printers[$x]['type'] = $variable_returned; } I was looking at this because the SNMP requests are quite intensive and would like to reduce them as much as possible. Cheers Quote Link to comment Share on other sites More sharing options...
btherl Posted July 1, 2008 Share Posted July 1, 2008 I often do this: // Printer type $ret = snmp2_get($printers[$x]['name'], $community, $standard_printer_variables['type'], $timeout, $retry); if ($ret === FALSE) { // Do nothing }else{ $printers[$x]['type'] = $ret; } While it is possible to assign the variable at the same time as doing the comparison, I find it leads to messy code. The two step approach is much clearer. I also changed your "==" to "===" .. while in most cases it makes no difference, there's a few cases that it does (eg, 0 == false will succeed, but 0 === false will fail). It's a good practice to use === when checking for a return value of false. Quote Link to comment Share on other sites More sharing options...
rhyspaterson Posted July 1, 2008 Author Share Posted July 1, 2008 Cheers mate Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.