PHP FUNCTION a Function Is a Group of Reusable Code Which Can Be Called Anywhere in the Program

PHP FUNCTION a Function Is a Group of Reusable Code Which Can Be Called Anywhere in the Program

PHP FUNCTION A function is a group of reusable code which can be called anywhere in the program. This eliminates the need of writing same code again and again. In other words, you can implement the concept of reusability through functions. Because, once you have defined a function block in your program, thereafter you can call it for number of times at any point in the program. Function will help programmers to write modular code. You can divide your big program into a number of small and manageable functions. This process is called modularity. Like any other advance programming language, PHP also supports all the features required to write modular code using functions. PHP provides a number of built-in functions that you can use to enhance and empower your applications. At the same time, PHP allows you to write your own functions as well. 1. Creating User-defined Functions in PHP It is very easy to create your own PHP function. A user-defined function declaration starts with the keyword "function". Suppose you want to create a PHP function which will simply display a message “Good Morning” on your browser when you will call it. Following example creates a function called Message() and then calls it just after creating it: <html> <head> <title>User-defined Function</title> </head> <body> <?php /* Defining a PHP Function */ function Message() { echo "Good Morning"; } /* Calling a PHP Function */ Message(); ?> </body> </html> After the execution of above code, the output would be look like: 2. Functions with Arguments and Returning Values In PHP, information can be passed to functions through arguments. An argument is just like a variable. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma. A PHP function can also return a value using the return statement. Example: <html> <head> <title>Function with Arguments and Return Value</title> </head> <body> <?php /* Defining a PHP Function */ function addFunction($num1, $num2) { $sum = $num1 + $num2; return $sum; } /* Calling a PHP Function */ $result = addFunction(11,22); echo "The result is ".$result; ?> </body> </html> After the execution of above code, the output would be look like: .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    3 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us