Defining Functions

Author Topic: Defining Functions  (Read 1324 times)

Offline chinmay.sahooTopic starter

  • Trade Count: (0)
  • Full Member
  • ***
  • Thank You 2
  • Posts: 159
  • Karma: 0
    • Hot Shaper
Defining Functions
« on: 01-04-2016, 01:19:41 »
There are already many functions built into PHP. However, you can define your own and organize your code into functions. To define your own functions, start out with the function statement:

Quote
function some_function([arguments]) { code to execute; }

The brackets ([ ]) mean optional. The code could also be written with optional_ arguments in place of [arguments]. The function keyword is followed by the function name. Function names abide by the same rules as other named objects, such as variables, in PHP. A pair of parentheses (()) must come next. If your function has parameters, they’re specified within the parentheses. Finally, the code to execute is listed between curly braces, as seen in the previous code example.


You can define functions anywhere in your code and call themfromvirtually anywhere. For the most part, all PHP variables . have only a single scope. A single scope spans included and required files as well. The function is defined on the same file or included in an include file. Functions can have parameters and return values that allow you to reuse code.

To create your own function that simply displays a different hello message, you would write:

Quote
<?php
function hi( )
{
echo ("Hello from function-land!");
}
//Call the function
hi( );
?>

which displays:

Hello from function-land!

The hi function doesn’t take any parameters, so you don’t list anything between the parentheses. Now that you’ve defined a simple function, let’s mix in some parameters.


Offline TomClarke

  • Trade Count: (0)
  • Hero Member
  • *****
  • Thank You 44
  • Posts: 893
  • Karma: 0
  • Gender: Male
    • Custom Writing Service & Content Marketplace
Re: Defining Functions
« Reply #1 on: 04-27-2016, 04:51:59 »
Hello! Here is another good article which explains user-defined functions!
I hope that this information would be helpful for you!
Your ideas, our writings - Writology.com

 

Related Topics

  Subject / Started by Replies Last post
0 Replies
1306 Views
Last post 12-24-2015, 00:40:20
by chinmay.sahoo
1 Replies
1659 Views
Last post 05-14-2016, 01:55:23
by TomClarke
1 Replies
1887 Views
Last post 05-14-2016, 02:20:24
by TomClarke
0 Replies
434 Views
Last post 04-04-2016, 04:22:57
by chinmay.sahoo
0 Replies
453 Views
Last post 12-28-2016, 00:47:12
by chinmay.sahoo