Passing Arguments by Reference in PHP

Simple passing arguments by reference in php. Pass arguments by reference in php program. Arguments are typically passed by value, which means that the function uses a copy of the value and that the variable passed into the function cannot be changed. Changes to the argument change the variable that was passed in when a function argument is passed by reference. The & operator is used to convert a function argument into a reference.

<!DOCTYPE html>
<html>
<body>
<?php
function add_three(&$value)
{
$value += 3;
}
$num = 2;
add_five($num);
echo $num;
?>
</body>
</html>

 

Output:

3

Functions in PHP

In PHP, a function is a reusable chunk of code that performs a specified task. It takes user input in the form of parameters, executes specified operations, and then outputs the results. When called, functions can either return values or just conduct an operation without returning anything.

There are two sorts of functions available in PHP: Built-in library functions: PHP comes with a large number of built-in library functions. Functions that are defined by the user: In addition to the built-in functions, PHP allows us to write our own unique functions, which are known as user-defined functions. Using this kind of functions we can execute the program passing arguments by reference in php.

Echo in PHP Programs

The echo command in PHP can be used to print a string, a multi-line string, escaping characters, a variable, an array, and so on. echo is a statement that displays the output of a command. 

echo() and echo can be used with or without parentheses. echo does not produce a result. The echo() function in PHP is a crucial string function. Its purpose is to show one or more strings. To put it another way, the echo() function returns one or more strings.