View Full Version : Function Question...

01-02-2008, 06:21 PM
I'm pretty good with php and I use functions a lot... they save so much confusion and mess...
I've seen function validate($username) and i can't figure out how i can use the variable in the parenthesis... it looks like a useful piece of code... what exactly does it do? and how does it work is what i'm wondering about...


01-02-2008, 06:59 PM
validate [line 46]
mixed validate( mixed Structures_Form_ElementInterface$element, object $element)

Validates a value against the rule.

If the value is not valid according to the rule, this method should return an error message. It is up to the rule to make sure the message is meaningful.

* Return: true if the value is ok, an error message otherwise.
* Access: public

object $element The form element.

That's what I found on it. There are other forms of validate() but this is the closest I think.

01-02-2008, 07:06 PM
is that your own function, or what blizzard stated? If it yours you call it by
and you use it just like a variable. for example:
function output($string) {
echo $string;
output("Hello world!");
that would echo Hello World!

01-02-2008, 07:40 PM
See.. that's what I don't get. Why make things hard? It is 100 times easier to type "Hello World!" than 4 lines of code to say "Hello World!"

Or even better, if it MUST be php

echo 'Hello World!'

and be done with it...

I just won't ever get these weird languages.

01-02-2008, 07:59 PM
See the use that Master_script_maker gave was just a simple usage. you probably wouldn't actually use it to display "Hello World". You use functions to do things that would be repetitive. Like lets say you have to calculate what the total income would be in one year with a monthly payment of x.

function payment($payment){
$total = $payment * 12;

echo "$".$total."<BR>";


Output would be:

Also this is a simple equation but this comes in handy when you are doing things like calculating the distance between to zip codes by the ark between there latitude and longitude.

I hope this helps a little more.

01-02-2008, 10:49 PM
They're also a method of code organisation and self-documentation. There's a principle in coding that says "never document with comments what can be described in code." E.G. $c = sqrt($a * $a + $b * $b);is a piece of code. Non-mathematicians might not realise what this does, so you could comment it:
// obtain the hypotenuse given the other two sides.
$c = sqrt($a * $a + $b * $b);Or, you could write a function:
function get_hypotenuse($side_a, $side_b) {
return sqrt($side_a * $side_a + $side_b * $side_b);
}Here there's no need to write a comment because the function explains itself. The function is also completely independent from the rest of the code, so you can move it to another file for organisation purposes and it will still make sense. Plus, there are things that you can do with functions that you simply can't do with flat procedural code. Go and learn a functional programming language (Lisp, Haskell, Erlang, Javascript in some cases) for some examples. The LoC is a pretty useless measure in this case when 1/3 lines contains only a single character :-) In something approaching Lisp style:
function get_hypotenuse($side_a, $side_b)
{ return sqrt($side_a * $side_a + $side_b * $side_b); }

01-03-2008, 07:20 AM
I get the point, and the "need" I just think it ridiculous to write 500 characters of code to "print" 12 characters I could do another way with 12 characters of code.

If I already have to write the number (in the pay example above) and then have to write "payment" instead of "12 *" is my point.

I am a coder, and some of the codes I do to print a meaningless small character is needed, I just think it is a tad excessive at times.

It's also getting to the point the more "advanced" a language becomes the harder it is to do simple things. php3 had small easy functions that php5 now takes 3 times the coding to do the same thing. Why?

01-03-2008, 10:20 AM
php3 had small easy functions that php5 now takes 3 times the coding to do the same thing.Example?

01-03-2008, 10:24 AM
Isn't it the other way around?