PHP has a server-side programming language provides a plentiful number of functions to play with. These internal functions are useful in many ways and act as an effective utility tool while development and also developers can define custom functions which will even enhance its effectiveness.

So in this tutorial, You’ll be learning Top 15 PHP Function Secrets Developers Must Know.

Define Function in PHP

For those who are new to PHP programming, this is how functions are defined. You need to use the keyword function and followed by the function name. Parameters to functions are passed inside brackets and to call function simple use its name and followed by brackets.

<?php
// Defining Function here
function function_name(arguments){
    // statements
}

function_name(); //calling function here

Example:

<?php

function show_name(){
    echo "My name is: Sumit";
}

show_name();  // My name is: Sumit

PHP Function Naming Convention

There are certain common programming standards which must be followed every programming language and these same rules of naming functions are also applied to PHP language and they are listed below.

  • No Special Characters must be used however underscore _ are an exception.
  • Self Explanatory Function Name: The user must get an idea of what the purpose of the function is just by reading its name.
  • Avoid Duplicate or Similar Names: Each function name must be different from one another as it eliminates ambiguity.
  • No Numerical Values: Functions should not contains and numerical values such as get1stUser[wrong], get_first_user[right and acceptable].
  • Lower Case Names are recommended: You are free to use the function name as Full Upper case or Capitalized. But for readability purpose, it is recommended to use function names as lower case and underscore to separate the words for ease of readability.

1 – Functions with Argument

Our First secret starts with passing arguments to function. You can specify as many numbers of arguments you want to be followed by a comma.

function show_name($name, $age){
    echo "My name is ".$name." and i'm ".$age." years old";
}

show_name("Sumit", 21); //My name is: Sumit and i'm 21 years old

2 – Functions with Default Arguments

If you would like to keep a particular function argument as optional which calling it then you can give it a default value. While calling function if the argument is not passed that the default value of an argument is assigned to parameter just like $interest=1.5. If interest value is not passed than PHP will consider argument $interest value as 1.5.

function loan_information($amount, $interest=1.5){
    echo "For amount ".$amount." interest = ".$interest."/yearly will be charged.";
}

loan_information(1000); //For amount 1000 interest = 1.5/yearly will be charged.

Argument Type Hinting

By default, PHP is a weak type and if you want to force PHP for strict type checking of function arguments than specify the datatype of argument within the brackets followed by parameter name.

function loan_information(float $amount, float $interest=1.5){
    var_dump($amount);
    var_dump($interest);
}

loan_information("1", "1.25"); 

//OUTPUT
/var/www/html/TestProject/examples/php/functions.php:12:float 1
/var/www/html/TestProject/examples/php/functions.php:13:float 1.25

Even if you have passed string arguments because of strict check PHP implicitly type casts numerical string into float. But when an alphabetical string is passed it throws a Notice: A non well formed numeric value encountered .

3 – Pass Function as Argument

This is a very simple way as you can passing function name as an argument to the function and call that argument inside the function as regular function like $func();.

function func_a($func){
    $func();
}

function func_b(){
    echo "Function B is called";
}

func_a('func_b'); // prints Function B is called

There is also another way where you can pass anonyms function directly into a function parameter.

function func_a($func){
    $func();
}

func_a(function(){
    echo "Function B is called";
}); // prints Function B is called

However, this method is not so practical and to not possible to reuse it.

#php #php

Top 15 PHP Function Secrets Developers Must Know PHP
6.60 GEEK