Thursday, July 24, 2008

Basic PHP Functions

Tutorial name: Functions with PHP
You're experience: Advanced
Difficulty: Average
Time Taken: 1-2 minutes
Plug-ins needed: none.


Functions with php are fairly simple.

First of all what is a function? it is a small tid bit of code that is ran each time it is called, and it allows for information to be returned, outputted, and inputted. In away, you can replace classes with functions, it ain't to hard, but it is possible. I use functions rather than classes because i feel more comfortable with them.

i will show you two things, how to use a function, and how to replace classes with them for developed coders, another time.

This will show you the basics to functions.
Code:
function repeatString($string, $times) // you can use what ever variables you want, and however many you want.
{
$count = 0;
while($count < $times)
{
echo"\n $string";
$count += 1;
}
}
?>
Blah blah blah...
repeatString("Hello!", 10); // this will echo "Hello!" 10 times
?>


returning variables
Code:
function 50Chance()
{
$rand = rand(0, 1);

return $rand;
]
?>
blah blah blah...
50Chance();
?>


//conditional functions
function ifGreaterThan0($num);
{
if($num > 0)
{
return 1;
}
else
{
return 0;
}
}

No comments: