PHP learn - Functions

The true power of PHP came from its functions.

What is a function?

We have used a lot of functions in the study: var_dump, sprintf and a lot of array functions. What are their characteristics?

  • Can perform some specific functions
  • Have special names
  • There are more or less parameters
  • Most functions will have a return value, even if they do not perform a specific operation
  • No need to know the implementation logic inside the function
Functions exist in most programming languages ​​and are used to separate code that can perform independent and explicit tasks.

The function name must start with a letter or an underscore followed by a letter, number, or underscore, and the function name is case insensitive.

What is a function parameter

We can think of the function as a juicer, the parameters are like the fruit we put in, and the code executed in the function is like the stirring of the mixer.

The parameters are different, the results returned by the function are different, just like the apple juice does not give a reason after the orange is placed. The function can also accept multiple parameters, just like mixing juice

The function only leaves us with the function name to call, and we don't need to know how the code in the function is defined. Similarly, we don't need to understand how the juicer works.

PHP built-in functions

In PHP, more than 1000 built-in functions are available.
For a complete reference manual and examples of all array functions, please visit PHP reference manual

PHP function

In this chapter, we'll show you how to create your own functions.
To execute the script while the page is loading, you can put it in the function.
Functions are executed by calling functions.
You can call the function anywhere on the page.

Create a PHP function

Functions are executed by calling functions. 

Function function name (parameter 1, parameter 2, parameter 3...) {

    Code/statement;

    Return return value;

}

PHP functions do not have to explicitly retain the return statement. If there is no return, the function will automatically return null.

PHP function guidelines:

· The name of the function should indicate its function

· Function names begin with a letter or an underscore (cannot begin with a number)

For Instance

A simple function that outputs my name when it is called:

 
<?php
 Function writeName()
 {
 Echo "Kai Jim Refsnes";
 }
 Echo "My name is ";
 writeName();
 ?>
 
 
 

PHP function - add parameters

In order to add more functionality to the function, we can add parameters. The parameters are similar to variables.

The argument is specified in parentheses after the function name.

Example 1

The following example will output a different name, but the last name is the same:

<?php
Function writeName($fname) { Echo $fname . " Refsnes.
"
; } Echo "My name is "; writeName("Kai Jim"); Echo "My sister's name is "; writeName("Hege"); Echo "My brother's name is "; writeName("Stale"); ?>
 



Last Updated: 2018-07-06 12:32:44 By akshay

SHARE THIS

share_facebook share_twitter share_google_plus

Please Comment Here :