...

View Full Version : Learning about Function, got a question.



Ultragames
09-24-2004, 12:53 AM
I knew from my java past that you can make a function, and call it when ever needed. So i looked it up, and PHP has that too. Heres my question.


create_function('$x,$y', ' print"This is a test!"; ');

What is the $x $y for? How do i use them? How do i know what to put there extra.

Also. How can i later call this function? Would:

function($x);
do it? Cant seem to find a simple tut on this.

Kurashu
09-24-2004, 12:59 AM
<?php
function function_name($x) {
return $x;
}


That's a basic function, but that's how you set it up. And when you call it can look like:


<?php
function_name("hi!"); // returns hi!
?>



<?php
$y = "hi!";
function_name($y); // also returns hi!
?>

Sk8er9547
09-24-2004, 01:02 AM
Perhaps you might take a look at this link:
http://www.phpfreaks.com/phpmanual/page/function.create-function.html

Ultragames
09-24-2004, 09:00 PM
if someone could explain this for me. I've read the PHP, Zend, all that stuff. And none of them use non-technical language to explain what anything is. I know the fist variable is the string arg, but i dont know what that is.

Basicly, im looking for a quick, Functions for dummies tut.

Sorry for not understanding this. Thanks for your help.

firepages
09-24-2004, 09:20 PM
look again at Kurashu's post , thats a simple PHP function , and if you look at the manual at www.php.net (better still download it) there is more information than you need, including a whole section for functions (http://www.php.net/functions)

create_function() (http://www.php.net/create_function) does indeed create a function but makes it at runtime which is quite different from `normal` static functions which you will be using 99% of the time, I would suggest getting used to normal functions before looking at dynamic ones.

Kurashu
09-24-2004, 11:00 PM
if someone could explain this for me. I've read the PHP, Zend, all that stuff. And none of them use non-technical language to explain what anything is. I know the fist variable is the string arg, but i dont know what that is.

Basicly, im looking for a quick, Functions for dummies tut.

Sorry for not understanding this. Thanks for your help.

$args could be anything, it could be $look_at_me_i_am_a_variable_and_i_contain_stuff_but_my_name_is_insanely_long. It just tells the function: as far as you are concerned, anything in this postion is named $varaible_name. So, going back to my previous post:


<?php
function function_name($x,$y,$z) {
print "$y <br>\n $z <br>\n $x <br>\n";
}
?>

That function will print the contents of $x, $y, and $z. How you want to place the contents of them is all up to you; variables, hardcoding it, or a definition (are they allowed in functions?) it is up to you. You can use (almost) any langauge construct in your function. Functions are just another way of oragnizing your code, so be creative and explore them; and, if you have a problem, that is what this board is for.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum