Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The right way to do this is to test for null first:</p> <pre><code>function ccdeleteuser($emailaddress) { $ConstantContact = new ConstantContact("basic", "apikey", "usr", "pwd"); $SearchContact = $ConstantContact-&gt;searchContactsByEmail($emailaddress); // first makes sure that the 0 index of of SearchContact is accessible at all // then it ensures that only something "truthy" will trigger delete -- this // means that if $SearchContact[0] is null, the function won't try to delete if( $SearchContact &amp;&amp; isset( $SearchContact[0] ) &amp;&amp; $SearchContact[0]) $DeleteContact = $ConstantContact-&gt;deleteContact($SearchContact[0]); else echo "Can't do nothin'"; // do something useful? } </code></pre> <p>With try... catch, you might make it look like this:</p> <pre><code>function ccdeleteuser($emailaddress) { $ConstantContact = new ConstantContact("basic", "apikey", "usr", "pwd"); $SearchContact = $ConstantContact-&gt;searchContactsByEmail($emailaddress); try { // keep this... it is still useful if( $SearchContact &amp;&amp; isset( $SearchContact[0] ) &amp;&amp; $SearchContact[0]) $DeleteContact = $ConstantContact-&gt;deleteContact($SearchContact[0]); else echo "Can't do nothin'"; } catch( Exception $e ) { // I'm making up a function "log" which will record that an error // has taken place. It is a good idea to always log all exceptions // so that you don't accidentally obfuscate something important log( $e-&gt;getMessage() ); // do something useful } } </code></pre> <p>As a general note, it is always best to act proactively to prevent exceptions and not simply catch them after the fact. I'll even go so far as to say that you should consider it a rule to always do everything in your power to prevent the possibility of an exception and only use try... catch as a last possible resort.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload