0

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
1
  • You should be more specific. You should only create and use functions if you use the same routine more often. If certain routines can not be used in general, but only on certain 'sets of data' you should consider creating and using classes/objects. How you want to validate your fields is up to you. Using regex is one way, to validate email addresses for example. Commented Oct 14, 2012 at 14:23

1 Answer 1

1

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"))
Sign up to request clarification or add additional context in comments.

2 Comments

(bool)"0" === false and (bool)strlen("0") === true.
thank you, basically the reason i ask is i have already got a certain section of my site working and just wanted to as people who know more php then me. think its time i go read up on them, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.