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
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
Share on other sites

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.