Call by value in php with example

PHP allows you to call function by value and reference both. In case of PHP call by value, actual value is not modified if it is modified inside the function.

Let's understand the concept of call by value by the help of examples.

Example 1

In this example, variable $str is passed to the adder function where it is concatenated with 'Call By Value' string. But, printing $str variable results 'Hello' only. It is because changes are done in the local variable $str2 only. It doesn't reflect to $str variable.

Output:

Example 2

Let's understand PHP call by value concept through another example.

Output:

For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to [email protected]

Help Others, Please Share

Call By Value

In this method, only values of actual parameters are passing to the function. So there are two addresses stored in memory. Making changes in the passing parameter does not affect the actual parameter.

Example

  1.    

Call by Reference

In this method, the address of actual parameters is passing to the function. So any change made by function affects actual parameter value.

Example

  1.   

What’s the difference between call by value & call by reference in PHP? This is the most important concept used in programming and sometime asked in interviews. Don’t worry if you are new to this terminology, you’ll learn this concept in this tutorial.

Call by value & call by reference in PHP

Call by Value & Call by Reference in PHP

Example 1: Let’s understand this concept using some programming examples.

/**

* Passed value using call by value

*/

functionincrement[$num]

{

$num=$num+1;

echo $num."\n";

}

$n=1;

increment[$n];/* Output 2 */

echo$n;/* Output 1 */

Example 2: Now again write this script with minimal modification in an argument.

/**

* passed value using call by reference.

*/

functionincrement[&$num]

{

$num=$num+1;

echo$num."\n";

}

$n=1;

increment[$n];/* Output 2 */

echo$n;/* Output 2 */

We have seen the examples and their differences in output. Let’s understand what exactly is call by value & call by reference.

PHP MCQ for practice

Call by Value in PHP

In call by value, the value of a variable is passed directly. It means, if the value of a variable within the function is changed, it doesn’t get changed outside of the function. As we seen in our first example.

Call by Reference in PHP

In call by reference, the address of a variable [their memory location] is passed. In the case of call by reference, we prepend an ampersand [&] to the argument name in the function definition. Any change in variable value within a function can reflect the change in the original value of a variable which we have seen in our second example.
Magic methods in PHP

Swap Two Numbers using Call By Value in PHP

In call by value, the actual value of a variable is passed. In this example, we have swap two numbers using call by value.

Swap two numbers in PHP

PHP code – Find first non-repeated character in a string

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

/**

* Swap two numbers using call by value

* @param type $first

* @param type $second

*/

functionswap[$first,$second]

{

$temp= $first;

$first=$second;

$second=$temp;

echo"First number ".$first." Second number ".$second."\n";

}

$a=5;

$b=7;

/* Output : First number 7 Second number 5 */

swap[$a, $b];

/*

   Output : First number 5 Second number 7

   original value is not changed even after calling swap method.

*/

echo"First number ".$a." Second number ".$b;

Swap Two Numbers using Call By Reference in PHP

In call by reference,  Address of a variable is passed.  Address represents where the value of a variable is stored. In this example, we swap two numbers using call by reference in PHP.

How to sort a string in PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

/**

* Swap two numbers using call by reference

* @param type $first

* @param type $second

*/

functionswap[&$first,&$second]

{

$temp =$first;

$first=$second;

$second=$temp;

echo"First number ".$first." Second number ".$second."\n";

}

$a=5;

$b=7;

/* Output : First number 7 Second number 5 */

swap [$a,$b];

/* Output : First number 7 Second number 5 */

echo"First number ".$a." Second number ".$b;

In the above example, we have passed the value of $a = 5 and $b = 7 in swap[] method. In swap[] method, we are passing the value by reference. It means the address of a variable is passed. So any change in the value of variables in a swap[] will reflect them in original values of a variable.

PHP 7 features

For further you can check PHP documentation.

What is call by value explain with example in PHP?

PHP allows you to call function by value and reference both. In case of PHP call by value, actual value is not modified if it is modified inside the function. Let's understand the concept of call by value by the help of examples.

What is Call by reference in PHP?

In case of PHP call by reference, actual value is modified if it is modified inside the function. In such case, you need to use & [ampersand] symbol with formal arguments. The & represents reference of the variable. Let's understand the concept of call by reference by the help of examples.

What is the difference between call by value and call by reference in PHP?

In the case of Call by Value, when we pass the value of the parameter during the calling of the function, it copies them to the function's actual local argument. In the case of Call by Reference, when we pass the parameter's location reference/address, it copies and assigns them to the function's local argument.

What is the difference between call by value and call by reference with example?

Example of a call by value method. Example of a call by reference method. ... Call by Value vs. Call by Reference..

Chủ Đề