Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6673251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:36:20+00:00 2026-05-26T03:36:20+00:00

When i write down a function is a good practice to validate all the

  • 0

When i write down a function is a good practice to validate all the parameters of the function like this:

<?php
/**
 * foo - Test function
 *
 * @param int $paramA
 * @param str $paramB
 * @param array $paramC
 *
 * @return something
 */
function foo($paramA, $paramB, $paramC) {
    // verify all parameters
    if(!is_int($paramA)) {
        return 0; // or error msg
    }
    if(!is_string($paramB)) {
        return 1; // or error msg
    }
    if(!is_array($paramC)) {
        return 2; // or error msg
    }

    // some code in function scope that uses parameters 
    // and saves the results in $result
    return $result;
}

or should i rely on the user that will use the function and assume that he knows what he is doing and forget about parameter validation?

Personally i prefer to validate all the parameter of the function and return error codes for consistency of the code and make my life easier afterwords.

Thank you in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:36:21+00:00Added an answer on May 26, 2026 at 3:36 am

    If it is an internal function that only you will be using, it is probably okay to not validate every single parameter like that. If it is a function that has parameters that depend on user input (for example, you get information through $_GET, and then pass that to a function), then it needs to be validated.

    Never rely on a user or anyone else besides yourself to know what they are doing. Sometimes it can be helpful to validate things like this if you are writing code that you can re-use later (like if you were writing your own framework to use across all of your PHP projects). But if it is just one application-specific function that only you can call with parameters that only you define, I wouldn’t worry about it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three tables. This query will write down the right answer (x-lines for
I'm going to write a CMS, but right now I'm writing down all my
I'm using Log4Net to write logs to files. Can this serioulsy slow down my
Im trying to write a function that counts down to a date which works
I would like to know how to write my own floor function to round
I'm trying to write down a UTF-8 string (Vietnamese) into C# Console but no
I've installed Oracle XE with APEX, but forgot to write down the URL to
How would you write a regular expression to convert mark down into HTML? For
I've tried to write the smallest chunk of code to narrow down a problem.
Write a class ListNode which has the following properties: int value; ListNode *next; Provide

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.