What is meant by passing the variable by value and reference in PHP?

0 votes
Explain me about the difference between passing the variable by value and reference with any sample code.

Thank you!!
Mar 27 in PHP by kartik
• 10,990 points
36 views

1 answer to this question.

0 votes

Hello,

When the variable is passed as value then it is called pass variable by value.

Here, the main variable remains unchanged even when the passed variable changes.

Sample code:

function test($n) {
$n=$n+10;
}

$m=5;
test($m);
echo $m;

When the variable is passed as a reference then it is called pass variable by reference

Here, both the main variable and the passed variable share the same memory location and is used for reference.

So, if one variable changes then the other will also change.

Sample code:

function test(&$n) {
    $n=$n+10;
}
$m=5;
test($m);
echo $m;

Thank You!

answered Mar 27 by Niroj
• 22,110 points

Related Questions In PHP

0 votes
0 answers

What are the differences in die() and exit() in PHP?

I think both have the same functionality, ...READ MORE

Apr 8 in PHP by kartik
• 10,990 points
23 views
0 votes
1 answer

What is the use of the @ symbol in PHP?

Hello @kartik, The @ symbol is the error control operator ("silence" or "shut-up" ...READ MORE

answered Apr 8 in PHP by Niroj
• 22,110 points
39 views
0 votes
0 answers
0 votes
1 answer

What is the best way to allow plugins for a PHP application?

Hello @kartik, I've used, it's an attempt to ...READ MORE

answered Apr 15 in PHP by Niroj
• 22,110 points
14 views
0 votes
1 answer

Connection with MySQL server using PHP. How can we do that?

Hey @kartik, You have to provide MySQL hostname, ...READ MORE

answered Mar 27 in PHP by Niroj
• 22,110 points
40 views
0 votes
1 answer

How to retrieve or obtain data from the MySQL database using PHP?

Hello kartik,  Actually there are many functions that  ...READ MORE

answered Mar 27 in PHP by Niroj
• 22,110 points
33 views
0 votes
1 answer

What are the differences between mysqli_connect and mysqli_pconnect?

Hello, mysqli_pconnect() function is used for making a persistence ...READ MORE

answered Mar 27 in PHP by Niroj
• 22,110 points
69 views
0 votes
1 answer

How to get thumbnail of youtube video from youtube API?

Hii @kartik, If you want the biggest image ...READ MORE

answered Apr 1 in PHP by Niroj
• 22,110 points
108 views
0 votes
1 answer

What is the use of $_REQUEST variable in php?

Hii @kartik, The $_REQUEST variable is used to read the ...READ MORE

answered Mar 27 in PHP by Niroj
• 22,110 points
49 views
0 votes
1 answer

What is type casting and type juggling in php?

Hey, The way by which PHP can assign ...READ MORE

answered Mar 27 in PHP by Niroj
• 22,110 points
48 views