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

  • SEARCH
  • Home
  • 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 965377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:59:15+00:00 2026-05-16T01:59:15+00:00

Often I write functions whose operation depends depends on the type of some argument.

  • 0

Often I write functions whose operation depends depends on the type of some argument. An example would be a function that would help me to insert rows into a mysql database.

function insert($table, $columns, $values) {
    if(is_string($values)) {
        $values = "('" . $values . "')";
        }
    else if(is_array($values)) {
        $values = "('" . implode("','", $values) . "')";
        }

    //process $table and $columns here

    mysql_query("INSERT INTO $table $columns $values");
    }

The idea is that sometimes I will only want to insert a single value but sometimes I will want to insert an array of values. However, this means that the type of $values will depend on the situation and I notice that in general, the built-in PHP functions tend to avoid this. I feel like if the PHP core implemented such a function, it would instead be split into insertString and insertArray to handle both cases. The benefits of overloading the type are that the user doesn’t need to worry about what they put into $values as long as it is a string or an array. For example, if they forget that they can pass in a string, they can still pass in array('singleValue') without any problems. Therefore, the using the function becomes easier. Are there any pitfalls with this approach?

Furthermore, we could extend our function to insert multiple rows at once. These would be input as a multidimensional array and so would need a line that processed them as follows:

if(is_multi($values)) {
    foreach($values as $key => $value) {
        $values[$key] = "('" . implode("','", $value) . "')";
        }
    $values = implode(",", $values);
    }

Again we could either tack this on to the original function or create a new function, insertMulti. The difference between this case and the earlier case is that technically the type of $values doesn’t change between insertArray and insertMulti. However, the way in which we handle $values does change and this change is dependent on the structure of $values. So if there are benefits and pitfalls to type overloading, do they apply to this situation as well or should this be treated differently?

Edit: Thank you for your answers! I realise that there are many PHP functions that accept mixed values but there are also many that do not. For example, strlen and count seem to me to be analogous functions except that one handles strings and the other handles arrays and objects. Would it be better then to implement countAll which handles strings, arrays and objects? Are they only kept separate for historical reasons?

  • 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-16T01:59:16+00:00Added an answer on May 16, 2026 at 1:59 am

    Not only does PHP have core functions that allow this, it is common for popular PHP frameworks like CakePHP to supply methods that accept such mixed arguments.

    The only thing to watch out for is if your function behaves in an exceptional way when an empty value is passed to the function (perhaps, returning all records or something). Ensure that type of behavior is consistent among related functions and documented. Otherwise, callers inadvertently passing an empty array (perhaps the results of another function) could be in for a surprise.

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

Sidebar

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.