PHP References: Assign, Pass, Return

$orig_array = [4,5,6];foreach ($orig_array as $elem) {
$orig_array[] = $elem + 1;
echo $elem;
}
class SimpleClass {
public $var1;
}
$sc = new SimpleClass();
$sc->var1 = 23;
var_dump($sc);php shell code:1:
class SimpleClass#1 (1) {
public $var1 =>
int(23)
}
$sd = $sc;$sd->var1 = 34;// reference like behaviourvar_dump($sc);php shell code:1:
class SimpleClass#1 (1) {
public $var1 =>
int(34)
}
$sd = 1001;// non-reference like behaviourvar_dump($sc);php shell code:1:
class SimpleClass#1 (1) {
public $var1 =>
int(34)
}
$se = &$sc;$se = 1002;// reference like behaviour?var_dump($sc);php shell code:1:
int(1002)
  1. Assign by reference
  2. Pass by reference
  3. Return by reference

Assignment by Reference

Typical variables

$a = 'xyz';$b = &$a;$b = 'zxy';var_dump($a);zxy
  1. type: IS_STRING, IS_LONG, IS_BOOL etc.
  2. value
  3. Is_ref__gc: Whether this variable is a reference or not
  4. ref_count__gc: The number of variables pointing to this zval
{
type: IS_STRING,
value: ‘zxy’,
is_ref__gc: 1,
ref_count__gc: 2
}

Arrays

$a = [1,2,3];$b = $a;$b[] = 4;var_dump($a);php shell code:1:
array(3) {
[0] =>
int(1)
[1] =>
int(2)
[2] =>
int(3)
}
$a = [1,2,3];$b = &$a;$b[] = 4;var_dump($a);php shell code:1:
array(4) {
[0] =>
int(1)
[1] =>
int(2)
[2] =>
int(3)
[3] =>
int(4)
}
$a = [1,2,3];foreach ($a as $item) {
$a[2] = 0;
echo $item."\n";
}
var_dump($a);
$a = [1,2,3,4,5];foreach ($a as $item1) {
foreach ($a as $item2) {
if (($item1 == 1) && ($item2 == 1)) {
unset($a[1]);
}
echo $item1.",".$item2." ; ";
}
echo "\n";
}
$a = [1,2,3,4,5];foreach ($a as &$item1) {
foreach ($a as &$item2) {
if (($item1 == 1) && ($item2 == 1)) {
unset($a[1]);
}
echo $item1.",".$item2." ; ";
}
echo "\n";
}

Objects

class SimpleClass {
public $var1;
}
$sc = new SimpleClass();
$sc->var1 = 23;
var_dump($sc);php shell code:1:
class SimpleClass#1 (1) {
public $var1 =>
int(23)
}
$sd = $sc;$sd->var1 = 34;// reference like behaviourvar_dump($sc);php shell code:1:
class SimpleClass#1 (1) {
public $var1 =>
int(34)
}
$sd = 1001;// non-reference like behaviourvar_dump($sc);php shell code:1:
class SimpleClass#1 (1) {
public $var1 =>
int(34)
}
$se = &$sc;$se = 1002;// reference like behaviour?var_dump($sc);php shell code:1:
int(1002)
// when we do $sd = $sc, both point to this zval structure
{
type: IS_OBJECT,
value: obj_id_1,
is_ref__gc: 0,
ref_count__gc: 2
}
// $sd = 1001 creates a new zval struct
// $sc points to the original zval
{
type: IS_LONG,
value: 1001,
is_ref__gc: 0,
ref_count__gc: 1
}

Passing by Reference

function foo(&$var) {
$var = 213;
}
$var1 = 312;foo($var1);var_dump($var1);213

Return by reference

class BarWrap {
private $barTest = 123;
function &bar() {
return $this->barTest;
}
function printBarTest() {
echo $this->barTest;
}
}
$bw = new BarWrap();$bt = &$bw->bar();$bt = 234;echo $bw->printBarTest();234

--

--

--

I code, among other things.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to perform CRUD operations using Blazor Server App Part-XI

Get this wrong and even the “right” answer will be wrong ….

Using Docker as an Admin tool on Windows

Install Java on Windows 10 Linux subsystem

Introducing Instant Rebalancing for a fully autonomous Kubernetes optimization platform

Running GUI Application in container

Navigating Through Duet Bond Module

5 Questions To Ask Before Choosing A New Programming Language

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Yasoob Haider

Yasoob Haider

I code, among other things.

More from Medium

Could Not Open Input File: Artisan In Laravel

Laravel 8: What are Its Latest Features and Is It Stable?

run artisan command in cpanel

php laravel artisan in cPanel

URLs manipulation in PHP (the easy way)

Change your URLs with a simple fluent PHP class