odd
odd

Reputation: 449

PHP Functions / quick function example

This is more of a general question.

I currently don't write a lot of functions.

Is it best to use as many functions as possible?

Also one quick question, is this a food way to check if a function was met.

if (!validateField("route"))
$errorMsg.= '<li>Select a valid</li>';

function validateField($field){      

        if(strlen(getFieldValue($field)) <=0)
            return false;  
        else  
            return true;  
    A

Upvotes: 0

Views: 94

Answers (1)

JvdBerg
JvdBerg

Reputation: 21856

Functions are there to make code more readable and not repeating code over and over.

Your function could be rewritten like this:

function validateField($field)
{      
  return (bool) strlen(getFieldValue($field));
}

and that could be rewritten as

function validateField($field)
{      
  return (bool) getFieldValue($field));
}

So in this particular case you do not need a function at all, just write

if (!validateField(getFieldValue("route"))

Upvotes: 1

Related Questions