Welcome to the Cookbook

loading...

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.