Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6358945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:27:14+00:00 2026-05-24T23:27:14+00:00

I was going to explain to our intern the difference between pass by reference

  • 0

I was going to explain to our intern the difference between “pass by reference” and “pass by value” in PHP, and did this simple script:

$a=5; 
$b=&$a; 
$a=8; 
echo $b;
// prints  8
$a=5; 
$b=$a;  //no &
$a=8; 
echo $b;
// prints 5

However, running this in php-cli using php -qa yields:

php >     $a=5; 
php >     $b=&$a; 
php >     $a=8; 
php >     echo $b;
8
php >     // prints  8
php >     $a=5; 
php >     $b=$a;  //no &
php >     $a=8; 
php >     echo $b;
8
php >     // prints 5

Should not the $b=$a; unlink $a and $b?

… so I got curius, and tried:

php > $b=3;
php > echo $a;
3

So, how did I get this wrong? What’s going on here? It seems the reference-setting is somehow sticking, even though it should be cleared at the line $b=$a? I also tried:

php >    $e=5; $f=$e; $e=6; echo $f; 
5

…Which works as expected.

$a and $b seems linked permanently? Am I missing some big point here? How do I “unlink” the $a and $b variable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:27:15+00:00Added an answer on May 24, 2026 at 11:27 pm

    Why should the reference be cleared if you assign a value to a variable? It works like this (with semi-simplified comments):

    $a = 5;    // creates a "slot", puts 5 in it, makes $a point to it
    $b =& $a;  // makes $b point to the same "slot" $a points to
    $c = 6;    // creates a "slot", puts 6 in it, makes $c point to it
    $a = $c;   // puts the value of the slot $c points to into the slot $a points to
    echo $b;   // outputs the value of the slot $b points to (6)
    

    It’s assigning a value to a variable. Whether the value is literal (5) or the value held by another variable doesn’t matter. The reference stays until you unset($b).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is going to be hard to explain but I'll try my best. I
This is the sample output Let me explain what's going on: The query returns
This is rather a syntax question I am going to explain it jQuery's ajax
Can anyone explain whats going on 'under the hood' with textRanges in IE/javascript? What
I'm going to start a new project - rewriting an existing system (PHP +
We would like to force encryption of the data going to and from our
Cannot explain what is going on the following program. GetType is returning the type
Can someone explain what is going on here? Howcome both these things are true?
Can anyone explain the (of Out T) component to this interface? I'm familar enough
I will try my best to explain what's going on. The app that I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.