Function parameters and return values php

Function parameters and return values php

In PHP program development, user-declared functions can have user-defined function parameters and return values, making custom-declared functions more flexible and reusable, allowing for complex modular programming within functions.

Function parameters and return values php

So, let’s take a closer look at function parameters and return values ​​​​in PHP program development.

Function parameters in PHP.

Function parameters are custom values ​​​​declared by the programmer when calling a function. Function parameters declared by the programmer are dynamically created each time the program is called, accepting multiple data types.

Function example with parameters in PHP.

<?php

function emp_info($emp_name, $dept, $emp_age) {

echo “employee name is $emp_name, dept is $dept, and age is $emp_age year”;

}

?>

<?php

emp_info(“siddhi”,”It”, 41); // Result – Employee name is siddhi, dept is It, and age is 41 years

echo”\n”;

emp_info(“harry”, “Recruiting”, 22); // Result – Employee name is harry, dept is Recruiting, and age is 22 years

echo”\n”;

emp_info(“lalit”,”Hr”, 49); // Result – Employee name is lalit, dept is Hr, and age is 49 years

?>

In this program example.

  • In this program, the function parameters $emp_name, $dept, and $emp_age are declared.
  • Here, when calling the emp_info function in emp_info(“siddhi”,”It”, 41), “siddhi” is passed as the $emp_name, $dept is passed as the value, and it and 41 are passed as the $emp_age value.

Default Parameters in PHP.

In PHP program development, default values ​​can be provided for user-declared parameters. When calling a function parameter in PHP, no function arguments or parameters are passed. The function can be passed using the default parameter values.

Default Parameter Function Example.

<?php

function emp_info($emp_name =”kunal”, $dept=”hr”, $emp_age=19) {

    echo “employee name is $emp_name, dept is $dept, and age is $emp_age year”;

}

?>

<?php

emp_info(); // Result – employee name is kunal, dept is hr, and age is 19 year

echo”\n”;

emp_info(“siddhi”,”It”, 41);  // Result – employee name is siddhi, dept is It, and age is 41 year

echo”\n”;

emp_info(“harry”,”labour”, 22);  // Result – employee name is harry, dept is labour, and age is 22 year

echo”\n”;

emp_info(“lalit”,”ministry”, 49);  // Result – employee name is lalit, dept is ministry, and age is 49 year

?>

In this program.

  • If in this program emp_info(); The function uses the default values ​​”kunal”, “hr”, and 19 as the default parameter values ​​if no parameter values ​​are given for $emp_name, $dept, and $emp_age.
  • This program uses the default values ​​”kunal”, “hr”, and 19 as the default parameter values.
  • In this program, you can pass a value for only one parameter, and the other function parameter values ​​display their default values.

Return Values ​​​​in PHP.

In PHP program development, a user-declared function can return any value by using the return keyword. The return keyword is used to return function data to the place where the function is called in the program.

Example of a function with a return value.

<?php

function total($p, $q, $r) {

return $p + $q + $r;

}

?>

<?php

$output = total(1,2,3); // the final result is 6

echo $output; // Result – 6

?>

Here, in this condition, the total function returns the total value of $p, $q, and $r. Finally, the total value is stored in $output and printed to the console.

Returning multiple values ​​​​in PHP (using an array) Example.

In PHP program development, functions do not directly support returning multiple values, but array values ​​declared in the function can be returned. Here, a reference can be used to return the array value.

Multiple value returning array function example.

<?php

function dispemployeeInfo() {

return [“Bhavishi”, 19, “Engineer”];

}

?>

<?php

$employeeInfo = dispemployeeInfo();

echo “Name – ” . $employeeInfo[0] . “, Age – ” . $employeeInfo[1] . “, Department- ” . $employeeInfo[2]; // Result – Name – Bhavishi, Age – 19, Department- Engineer

?>

In this program, the dispemployeeInfo() function displays or returns multiple-valued array information, and programmers can access or display individual array element items using array indexing methods.

Returning a Value by Reference.

In PHP program development, reference functions can return a variable value. This means that modifications to the returned value in the function will affect the original variable value.

Example of returning by reference in PHP.

<?php

function &testvalue() {

static $increaser = 7;

$increaser++;

return $increaser;

}

?>

<?php

$increase1 = &testvalue();

echo $increase1; // Result – 8

$increase2 = &testvalue();

echo $increase2; // Result – 9

$increase1++; // Increase incrementer1

echo $increase2; // Result – 10 here

?>

In this program example.

Here, in this program condition, the &testvalue() function returns a value by reference, so both $increase1 and $increase2 point to the same variable.

Summary of function parameters and return values ​​in PHP.

  • In PHP program development, user-declared function parameters are values ​​that pass function values.
  • Where a value or argument is passed when calling a function in the program.
  • User-declared functions can have default function values.
  • Function return values ​​can be returned by using the return keyword to return a value in a function.
  • A single value in a function can be returned by an array or even by reference.

Leave a Reply