Jump to content

[SOLVED] IF Statements


rhyspaterson

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/112686-solved-if-statements/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/112686-solved-if-statements/#findComment-578725
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.