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, 2020 in PHP by kartik
• 37,520 points
3,288 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, 2020 by Niroj
• 82,840 points

Related Questions In PHP

0 votes
0 answers

What is the difference between Sessions and Cookies in PHP?

What is the distinction between Sessions and Cookies ...READ MORE

Jun 13, 2022 in PHP by narikkadan
• 63,600 points
409 views
0 votes
0 answers

What is the difference between ' and " in PHP?

What is the difference between ' and ...READ MORE

Jun 17, 2022 in PHP by narikkadan
• 63,600 points
497 views
0 votes
0 answers

What is the difference between POST and GET in HTML/PHP

I don't seem to understand what GET ...READ MORE

Jun 17, 2022 in PHP by narikkadan
• 63,600 points
396 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, 2020 in PHP by Niroj
• 82,840 points
1,326 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, 2020 in PHP by Niroj
• 82,840 points
3,399 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, 2020 in PHP by Niroj
• 82,840 points
3,804 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, 2020 in PHP by Niroj
• 82,840 points
1,665 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, 2020 in PHP by Niroj
• 82,840 points
3,330 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, 2020 in PHP by Niroj
• 82,840 points
7,208 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP