Welcome to the Cookbook

loading...

{4804} - Method definition

Example of a function definition:

<?php 
function someFunction($arg1, $arg2 = '') {
    if (expr) {
        statement;
    }
    return $var;
}
?>
  1. <?php
  2. function someFunction($arg1, $arg2 = '') {
  3. if (expr) {
  4. statement;
  5. }
  6. return $var;
  7. }
  8. ?>

Parameters with a default value, should be placed last in function definition. Try to make your functions return something, at least true or false - so it can be determined whether the function call was successful.

<?php 
function connection(&$dns, $persistent = false) {
    if (is_array($dns)) {
        $dns_info = &$dns;
    } else {
        $dns_info = BD::parseDNS($dns);
    }

    if (!($dns_info) || !($dns_info['phpType'])) {
        return $this->addError();
    }
    return true;
}
?>
  1. <?php
  2. function connection(&$dns, $persistent = false) {
  3. if (is_array($dns)) {
  4. $dns_info = &$dns;
  5. } else {
  6. $dns_info = BD::parseDNS($dns);
  7. }
  8. if (!($dns_info) || !($dns_info['phpType'])) {
  9. return $this->addError();
  10. }
  11. return true;
  12. }
  13. ?>

There are spaces on both side of the equals sign.

{5021} - Method definition

Example of a function definition:

<?php 
function someFunction($arg1, $arg2 = '') {
    if (expr) {
        statement;
    }
    return $var;
}
?>
  1. <?php
  2. function someFunction($arg1, $arg2 = '') {
  3. if (expr) {
  4. statement;
  5. }
  6. return $var;
  7. }
  8. ?>

Parameters with a default value, should be placed last in function definition. Try to make your functions return something, at least true or false - so it can be determined whether the function call was successful.

<?php 
function connection(&$dns, $persistent = false) {
    if (is_array($dns)) {
        $dnsInfo = &$dns;
    } else {
        $dnsInfo = BD::parseDNS($dns);
    }

    if (!($dnsInfo) || !($dnsInfo['phpType'])) {
        return $this->addError();
    }
    return true;
}
?>
  1. <?php
  2. function connection(&$dns, $persistent = false) {
  3. if (is_array($dns)) {
  4. $dnsInfo = &$dns;
  5. } else {
  6. $dnsInfo = BD::parseDNS($dns);
  7. }
  8. if (!($dnsInfo) || !($dnsInfo['phpType'])) {
  9. return $this->addError();
  10. }
  11. return true;
  12. }
  13. ?>

There are spaces on both side of the equals sign.

Differences

Lines: 11-24Lines: 11-24
 <p>Parameters with a default value, should be placed last in function definition. Try to make your functions return something, at least true or false - so it can be determined whether the function call was successful. </p> <p>Parameters with a default value, should be placed last in function definition. Try to make your functions return something, at least true or false - so it can be determined whether the function call was successful. </p>
 <pre>&lt;?php  <pre>&lt;?php
 function connection(&$dns, $persistent = false) { function connection(&$dns, $persistent = false) {
  if (is_array($dns)) {  if (is_array($dns)) {
- $dnsInfo = &$dns; + $dns_info = &$dns;
  } else {  } else {
- $dnsInfo = BD::parseDNS($dns); + $dns_info = BD::parseDNS($dns);
  }  }
- if (!($dnsInfo) || !($dnsInfo['phpType'])) { + if (!($dns_info) || !($dns_info['phpType'])) {
  return $this-&gt;addError();  return $this-&gt;addError();
  }  }
  return true;  return true;
 } }