tags:

views:

44

answers:

4

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 happy.

A: 

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

jitter
+1  A: 

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.

txyoji
+1  A: 

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
}
Alix Axel
Just a space saving thing. I have 10 functions all of which start with that one line, I thought the code could be tidied.
drummer
A: 

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.

Anti Veeranna