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 6689649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:32:39+00:00 2026-05-26T05:32:39+00:00

For string functions, I find myself doing this kind of thing a lot: $string

  • 0

For string functions, I find myself doing this kind of thing a lot:

$string = trim($string);

I would like to easily have functions where I can pass the string by reference, so I can instead do function($string);.

I was thinking about writing wrappers for all the string functions I want to use, and prefix it with a special character, like so:

 function _trim(&$string) {
   trim($string);
 }

 function _str_replace($a, $b, &$string) {
    str_replace($a, $b, $string);
 }

But I wonder if there’s a way that’s easier than writing a wrapper for each function? (Has someone written this library already?)

  • 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-26T05:32:40+00:00Added an answer on May 26, 2026 at 5:32 am

    For functions that can take only one argument (the string) like trim(), strtolower() etc, you could do something like this maybe…?

    function modify_str (&$str, $funcName) {
      if (function_exists($funcName)) $str = $funcName($str);
    }
    
    // So you can now do
    modify_str($str, 'trim');
    modify_str($str, 'strtolower');
    

    …but to be perfectly honest, I cant really see the point – for one thing, you can’t chain functions that take an argument by reference (i.e. you can’t do trim(strtolower($str)))

    I tend not to write $str = trim($str); very much, because I am probably going to use the result fairly immediately in some other operation. Instead of this:

    $str = trim($str);
    some_func($str, $someOtherVar);
    

    I just do this:

    some_func(trim($str), $someOtherVar);
    

    And in the event that I need to do something like this:

    $str = trim($str);
    some_func($str, $someOtherVar);
    another_func($str, $yetAnotherVar);
    

    I would do this:

    some_func($str = trim($str), $someOtherVar);
    another_func($str, $yetAnotherVar);
    

    …but whatever you are trying to do by doing any of the above, what you are effectively achieving is making your code less readable, and probably nothing else.

    EDIT

    After doing something completely unrelated today, I realised there is a way to chain functions and call it by reference:

    function modify_str (&$str) {
      $args = func_get_args();
      for ($i = 1; isset($args[$i]); $i++) {
        if (function_exists($funcName = $args[$i])) $str = $funcName($str);
      }
    }
    
    // So you could do
    modify_str($str,'trim','strtolower');
    

    …but I still maintain this is not the way to go.

    ANOTHER EDIT

    You could pass to functions that uses multiple arguments by doing something like this (untested):

    function modify_str (&$str) {
      $str_identifier = '$$$'; // This identifies the argument where $str should be used
      $ops = func_get_args();
      for ($i = 1; isset($args[$i]); $i++) { // Loop functions to be applied
        if (function_exists($ops[$i]['function'])) {
          $args = array();
          for ($j = 0; isset($ops[$i][$j]); $j++) { // Loop arguments for this function and build an argument list in PHP syntax
            $args[] = ($ops[$i][$j] === $str_identifier) ? '$str' : "\$ops[\$i][$j]";
          }
          // eval() it (as if you had written it as straight PHP)
          eval("\$str = {$ops[$i]['function']}(".implode(',',$args).");");
        }
      }
    }
    
    // So you can call it like this
    modify_str($str,array('function'=>'str_replace','&','&','$$$'),array('function'=>'explode',"\n",'$$$'));
    // ..which should have the same effect as
    $str = explode("\n",str_replace('&','&',$str));
    
    // As you can see, the not-by-reference way is is much shorter and more readable
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find myself writing code that looks like this a lot: set<int> affected_items; while
I find myself writing code like this in ColdFusion a lot: <cfset inputval =
I have little doubt about string reading in C. string reading functions like gets,
I have a string that looks like this: Name1=Value1;Name2=Value2;Name3=Value3 Is there a built-in class/function
I find myself writing a lot of functions in PHP that return HTML code.
I have been trying everything to try and solve this myself, but cannot find
I find myself writing a lot of code like putStr foo (bar 1) (bar
I can't seem to find a function that captures and returns a regex'ed string.
Never sure where to place functions like: String PrettyPhone( String phoneNumber ) // return
Is there something like serialize/unserialize PHP functions in jQuery? These functions return a string

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.