Online
 
Friday, 09 January 2009
 
 
More article:
Related Content:

Website PHP - Functions
 

PHP - PHP Language Programing 

PHP - Functions: Despite the fact that PHP comes with such a large selection of functions to perform all sorts of tasks, you will want to create your own functions when the need arises. If you find yourself doing the same thing repeatedly, or you want to share code across projects, user functions are for you.

Writing monolithic programscode that starts at the beginning and runs straight through to the endis considered very bad for program maintainability, as you are not able to reuse code. By writing functions, you make your code shorter, easier to control and maintain, and less prone to bugs.


A Simple User Function

You can give your functions whatever name you like; they follow the same guidelines (without the $) as PHP's variables. You may not redefine PHP's built-in functions, and care should be taken to ensure that your function names do not collide with existing PHP functionsjust because you don't have the imagepng( ) function available, it doesn't mean others also won't.

The simplest user function in PHP looks something like this:

Return Values

You're allowed to return one (and only one) value back from functions, and you do this by using the return statement. In our example, we could have used "return 'foo';" or "return 10 + 10;" to pass other values back, but return 1; is easiest and usually the most common, as it is the same as return true;.

You can return any variable you want, as long as it is just one variableit can be an integer, a string, a database connection, etc. The return keyword sets up the function return value to be whatever variable you use with it, then exits the function immediately. You can also just use return;, which means "exit without sending a value back." If you try to assign to a variable the return value of a function that has no return value (e.g., it uses return; rather than return $someval;), your variable will be set to NULL.

That will output 720, although you can easily edit the factorial( ) function call to pass in 20 rather than 6, for example. Factorials increase in value very quickly ("7!" is 5040, "8!" is 40320, etc.), so you will eventually hit a processing limitnot time, but merely recursive complexity; PHP will only allow you to have a certain level of recursion ("18!" is about the max you are likely to be able to calculate using the above code).

As you can see, recursive functions make programming certain tasks particularly easy, and it is not all math, eitherconsider how easy it is to write a function showchildren( ) for a forum, which automatically shows all replies to a message, and all replies to those replies, and all replies to the replies to the replies, and so on.

 

Read More PHP:PHP - Functions | PHP - Including Other Files | PHP - Comments Tutorial | PHP - Example Whitespace | PHP - Variables of PHP | PHP - Basics of PHP | PHP Abnormal Script | PHP - Output Control


Links to this article:"PHP - Functions"

 

Tags: Add more tags...,
This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment. Tags: Simple PHP, Pear, Easy PHP, PHP Tutorial, PHP MySQL, XSLT, Sap Tutorial, CSS Tutorial, XSL FO Java, SQL Tutorial.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Natural 189
Statistic


Last Post

 
Top! Top!