Functions
Whenever you need to use a certain piece of code over and over again, it is very useful to put it in a function. By putting your code into functions, you make the code easy to reuse. Whenever you locate an error in your code, you only have to fix it once: in the function. Using functions also makes your code easier to read and understand, especially when you choose sensible names for functions. Let’s start with a simple function:
function foo($arg) {
if ($arg < 10) {
echo "$arg is smaller than 10";
} else {
echo "$arg is greater than or equal to 10";
}
}
This piece of code declares a function called foo
. It takes one
argument (that’s the bit between parentheses called $arg
above), and
it echoes different things, based on the value of that argument. You
call the foo
function lower down the page, in your other code like
this:
foo(15); /* Echoes "15 is greater than or equal to 10" */
Isn’t that simple? You can also make functions that don’t have any arguments, just add an empty pair of parentheses after the name of the function, both when you declare the function and when you use it.
You often need your functions to return a value you can use in your
programs, and here is how to do that: simply use the return
statement.
function bar() {
return (time() - mktime(0, 0, 0, 1, 21, 1982))/86400;
}
This function has no arguments and it returns the number of days since the 21st of January, 1982 — my birthday by the way. It uses a number of different built-in functions to get the time now, and then subtract the time I was born, and finally divide the time with 86400, the number of seconds in a day.
Under UNIX all times are measured as the number of seconds since the beginning of the Epoch, which was January 1st, 1970. So when you deal with times as I just did, you are just dealing with rather large integers (it’s been more than 1,000,000,000 seconds since January 1st 1970). Representing all timestamps as integers makes it very easy to find the time between two events.
Note that before we return the number, we could round it with the
round()
function, which will round the number to an integer: more on
round()
below!
In the body of the page you call into action that bar
function like
this:
$days_ages = bar();
If instead you wanted to round the floating-point number to a certain number of decimal places, then it’s easy to make your own function:
function smart_round($float, $dec) {
if ($dec == 0) {
return round($float);
} else {
return floor($float * pow(10, $dec) + 0.5) / pow(10, $dec);
}
}
$a = smart_round(2.317, 2); /* Now $a is equal to 2.32 */
This smart_round()
function returns $float
rounded to $dec
decimals. The built-in round() function has been changed in recent
versions of PHP to function exactly like our
smart_round()`.
You’ve now seen most of the basic features of PHP. There’s only some links left for you.
Jauhari:
Thanks for nice info tutorial about function this will help me for coding on PHP
Thanks again
29 June 2005, 11:37 amBart:
Thanks for this information. I find PHP a difficult language to code in, but your info on PHP’s basics helped me understand it a lot better.
2 July 2005, 2:58 pmThanks again.
qweree:
Very nice tutorial. It helped me a lot with understanding the code :)
26 July 2005, 9:54 pmThank You!
Tchuta Leonard:
Thanks for teaching me PHP basics in less than 24 hours ..
17 December 2005, 1:32 pmRed Frog Travel China:
It is cool having this guide. I am using it to design a site for our travel agency in china. Php is cool!
21 January 2006, 2:59 pmIonut Vancea:
I am not beginner in programming, but I found this tutorial and I wanted to read it to see how you explain the basics of PHP. It is a useful tutorial for newbies, and it will give them the necessary knowledge to start.
11 April 2006, 10:57 pmS4NT4:
Hmmm..nice dude!
4 July 2007, 10:27 amvirender sharma:
it is very easy to use
16 November 2007, 10:11 amsitesh agrawal:
very nece and esy way presentation
12 February 2008, 2:29 pmWill Knight:
Its good to know that functions also makes your code easier to read and understand, especially when you choose sensible names for functions.
17 January 2011, 12:15 pm