开发者

Trimming a parameter in the function header

A function header looks like this

function doit($theparam){
  //$theparam is untrimmed

  $theparam = trim($theparam);

  //$theparam is now trimmed

}

Is it possible to do the trimming on the first line itself? I tried those 2 but neither worked.

function doit( trim($theparam) ){
  //access trimmed version
}

function doit( $theparam = trim($theparam) ){
  //access trimmed version
}

I have several functions, all start their work by trimming. If I could do it in fewer lines, I'd be hap开发者_StackOverflowpy.


Its not possible in php 4.x or 5.2. You can't use a function as the default argument of another function. 5.3 supports closures which would look a lot like the snippet jitter wrote.

If you've got several methods in the same class which require a specially formatted string it would make sense to create a utility function for that purpose. If you discover later that additional formatting is required, you'll only need to change a single method.


No, you can't do that.

PS: Why would you want to do something like that?

EDIT: To save space you can do something like this:

function doit($theparam) { $theparam = trim($theparam);
  //access trimmed version
}


Just do.

function doit($theparam){$theparam=trim($theparam);
}

;-) Just joking. I don't think there is way to do it inside the function parameter braces


Where do the function parameters come from? If at all possible, then wrap them into an object that is able to return the already trimmed values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜