Home / Programming / 10. PHP Function Definition and Example

10. PHP Function Definition and Example


In this discussion, we will discuss about PHP Function. We really need PHP Function in certain circumstances when coding. The genuine intensity of PHP originates from its capacities. PHP has in excess of 1000 inherent capacities, and moreover you can make your own custom capacities.

Other than the inherent PHP capacities, it is conceivable to make your own capacities.

  • A capacity is a square of explanations that can be utilized over and again in a program.
  • A capacity won’t execute consequently when a page loads.
  • A capacity will be executed by a call to the capacity.

Create a User Defined Function in PHP

A user-defined function declaration starts with the word function:

Note: A function name must start with a letter or an underscore. Function names are NOT case-sensitive.

Tip: Give the function a name that reflects what the function does!

In the example below, we create a function named “writeMsg()”. The opening curly brace ( { ) indicates the beginning of the function code, and the closing curly brace ( } ) indicates the end of the function. The function outputs “Hello world!”. To call the function, just write its name followed by brackets ():

PHP Function Arguments

Data can be gone to capacities through contentions. A contention is much the same as a variable.

Contentions are determined after the capacity name, inside the enclosures. You can include the same number of contentions as you need, simply separate them with a comma.

The accompanying model has a capacity with one contention ($fname). At the point when the familyName() work is called, we likewise go along a name (for example Jani), and the name is utilized inside the capacity, which yields a few distinctive first names, yet an equivalent last name. See the example below:

The following example has a function with two arguments ($fname and $year):

PHP is a Loosely Typed Language

In the model above, notice that we didn’t need to disclose to PHP which information type the variable is.

PHP naturally relates an information type to the variable, contingent upon its worth. Since the information types are not set in an exacting sense, you can do things like adding a string to a whole number without causing a mistake.

In PHP 7, type assertions were included. This gives us an alternative to indicate the normal information type while pronouncing a capacity, and by including the severe announcement, it will toss a “Lethal Error” if the information type confuse.

In the accompanying model we attempt to send both a number and a string to the capacity without utilizing severe. See the example below:

To specify strict we need to set declare(strict_types=1);. This must be on the very first line of the PHP file.

In the following example we try to send both a number and a string to the function, but here we have added the strict declaration:

PHP Default Argument Value

The following example shows how to use a default parameter. If we call the function setHeight() without arguments it takes the default value as argument:

PHP Functions – Returning values

To let a function return a value, use the return statement:

PHP Return Type Declaration

PHP 7 additionally bolsters Type Declarations for the arrival proclamation. Like with the sort assertion for work contentions, by empowering the exacting prerequisite, it will toss a “Lethal Error” on a kind confound.

To proclaim a sort for the capacity return, include a colon ( : ) and the sort directly before the opening wavy ( { )section while announcing the capacity.

In the accompanying model we indicate the arrival type for the capacity:

You can specify a different return type, than the argument types, but make sure the return is the correct type:

Recommended  Easy to Identify Identity Card Number (NIK) Using PHP
  • Facebook
  • Twitter
  • Linkedin
  • Pinterest

Leave a Comment

Your email address will not be published. Required fields are marked *

This div height required for enabling the sticky sidebar
Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views :