Saturday, November 16, 2024
Google search engine
HomeLanguagesHow to declare a function that receives one parameter in PHP ?

How to declare a function that receives one parameter in PHP ?

In this article, we will know how to declare the function that receives the single parameter in PHP. Function calling is an important aspect of any programming language. Functions(methods) can be called within the script to simplify the performance of operations and eliminate the redundancy of writing the same code snippet at multiple points. Functions can be declared with user-defined names and user-defined signatures.

This article will give the idea about calling a parameterized function, that takes only one argument as input, named “hello”. The function is then called bypassing the arbitrary boolean value and assigning it to the function argument value that is being called. The calling function is also known as the parent function. The following figure illustrates the methodology being used: 

The following term used in the function will help to understand the working of the function.

Parameter passing: The required parameters can be passed as arguments in the parent function. The value can be received by the receiving function, which can be used to perform the major operation & displays the passed parameter. The value that is passed from the parent function is assigned to the variable declared in the receiving function & it can be used in the further part of the code.

Parameter receiving: The parameter can either be manipulated and displayed in the receiving function, or the final output can be returned as a value from there. This is mostly done in the cases, where the output is required for further analysis by the calling/parent method. The value is then received in the form of the declared variable. For instance, in the above diagram, the resp variable is assigned the returned value x

Based on the value received, the function can then display the output, which is in the form of a string. The method prints “Hello” in case true is received as the argument value, else it will return the false. The return value is the output of the method. However, the function called, that is the child method may sometimes display the output within its own code snippet, as illustrated below.

PHP




<?php
  
function param_hello($hello=false){
    if($hello){
      echo("Hello </br>");
    }
    else{
      echo("No hello </br>");
    }
      
}
  
// Calling function first time
param_hello(true);
param_hello(false);
  
?>


Output:

Hello
No hello

The function may also return an output object, instead of printing it there. The response can then be captured from the calling statement and then displayed on the script.

Example: This example describes displaying the function return value in the script, based on the applied condition.

PHP




<?php
function param_hello($hello=false){
    if($hello){
      return "Hello </br>";
    }
    else{
      return "No hello </br>";
    }
}
  
$flag = true;
  
// Calling function first time
$res = param_hello($flag);
echo($res);
?>


Output:

Hello

RELATED ARTICLES

Most Popular

Recent Comments