SlideShare ist ein Scribd-Unternehmen logo
1 von 43
Downloaden Sie, um offline zu lesen
PHP
PHP
PHP
PHP
By default, function arguments are passed by value (so that
if the value of the argument within the function is changed, it
does not get changed outside of the function).
http://php.net/manual/en/functions.arguments.php
PHP
<?php
function foo($var)
{
$var++;
}
$a=5;
foo($a);
// $a is 5 here
C
C ?
void foo(int var)
{
var++;
}
void main()
{
int a = 5;
foo(a);
/* a is 5 here */
}
PHP
You can pass a variable by reference to a function so the
function can modify the variable. The syntax is as follows:
http://php.net/manual/en/language.references.pass.php
PHP
<?php
function foo(&$var)
{
$var++;
}
$a=5;
foo($a);
// $a is 6 here
C
C ?
void foo(int* var)
{
(*var)++;
}
void main()
{
int a = 5;
foo(&a);
/* a is 6 here */
}
PHP
References in PHP are a means to access the same
variable content by different names. They are not like C
pointers; for instance, you cannot perform pointer arithmetic
using them, they are not actual memory addresses, and so
on.
http://php.net/manual/en/language.references.whatare.php
C ……
C ?
References in PHP are a means to access the same variable
content by different names. They are not like C pointers;
for instance, you cannot perform pointer arithmetic using
them, they are not actual memory addresses, and so on.
http://php.net/manual/en/language.references.whatare.php
C ?
PHP
C ???
PHP copy-on-write
PHP is a dynamic, loosely typed language, that uses copy-
on-write and reference counting.
http://php.net/manual/en/internals2.variables.intro.php
C ?
PHP
C ???
PHP
C
PHP :
:
PHP
typedef struct _zval_struct {
zvalue_value value; /* variable value */
zend_uint refcount__gc; /* reference counter
zend_uchar type; /* value type */
zend_uchar is_ref__gc; /* reference flag */
} zval;
is_ref__gc = PHP &
PHP
PHP
PHP copy-on-write
<?php
$a = 100;
xdebug_debug_zval('a');
//==> a: (refcount=1, is_ref=0)=100
<?php
$a = 100;
$b = $a;
xdebug_debug_zval('a');
xdebug_debug_zval('b');
//==> a: (refcount=2, is_ref=0)=100
//==> b: (refcount=2, is_ref=0)=100
$a $b (refcount )
<?php
$a = 100;
$b = $a;
$c = $a;
xdebug_debug_zval('a');
xdebug_debug_zval('b');
xdebug_debug_zval('c');
//==> a: (refcount=3, is_ref=0)=100
//==> b: (refcount=3, is_ref=0)=100
//==> c: (refcount=3, is_ref=0)=100
$a
<?php
$a = 100;
$b = $a;
$c = $a;
$a++;
xdebug_debug_zval('a');
xdebug_debug_zval('b');
xdebug_debug_zval('c');
//==> a: (refcount=1, is_ref=0)=101
//==> b: (refcount=2, is_ref=0)=100
//==> c: (refcount=2, is_ref=0)=100
$a
$b $c
copy-on-write
$c
<?php
$a = 100;
$b = $a;
$c = $a;
$a++;
unset($c);
xdebug_debug_zval('a');
xdebug_debug_zval('b');
xdebug_debug_zval('c');
//==> a: (refcount=1, is_ref=0)=101
//==> b: (refcount=1, is_ref=0)=100
//==> c: no such symbol
refcount
PHP
<?php
$a = 100;
xdebug_debug_zval('a');
//==> a: (refcount=1, is_ref=0)=100
<?php
$a = 100;
$b = &$a;
xdebug_debug_zval('a');
xdebug_debug_zval('b');
//==> a: (refcount=2, is_ref=1)=100
//==> b: (refcount=2, is_ref=1)=100
(refcount = 2)
PHP & (is_ref = 1)
$a
<?php
$a = 100;
$b = &$a;
$a++;
xdebug_debug_zval('a');
xdebug_debug_zval('b');
//==> a: (refcount=2, is_ref=1)=101
//==> b: (refcount=2, is_ref=1)=101
$a $b
PHP &
C
C
C …… PHP &
PHP
<?php
$a = 100;
$b = &$a;
$c = $a;
xdebug_debug_zval('a');
xdebug_debug_zval('b');
xdebug_debug_zval('c');
//==> a: (refcount=2, is_ref=1)=100
//==> b: (refcount=2, is_ref=1)=100
//==> c: (refcount=1, is_ref=0)=100
$c = $a
$a $b
$a $c (& )
is_ref
is_ref=1 is_ref=0
C ? ?
PHP
PHP : 1885
: 1753
: 132
PHP
<?php
calculareRankingScores($works, $users);
...
function calculareRankingScores(&$works, &$users)
{
for ($i = 0; $i < count($works);) {
...
count($array) O(1)
count($array_passed_by_reference) O(n)
calculareRankingScores() O(n^2)
(※ )
: PHP
<?php
$a = 100;
$b = &$a;
unset($b);
xdebug_debug_zval('a');
xdebug_debug_zval('b');
//==> a: (refcount=1, is_ref=0)=100
//==> b: no such symbol
refcount 1 is_ref 0
PHP & 1
copy-on-write is_ref = 0
PHP ……
PHP5
PHP7

Weitere ähnliche Inhalte

Was ist angesagt?

Just-In-Time Compiler in PHP 8
Just-In-Time Compiler in PHP 8Just-In-Time Compiler in PHP 8
Just-In-Time Compiler in PHP 8Nikita Popov
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - phpHung-yu Lin
 
Refactoring using Codeception
Refactoring using CodeceptionRefactoring using Codeception
Refactoring using CodeceptionJeroen van Dijk
 
Clear php reference
Clear php referenceClear php reference
Clear php referenceDamien Seguy
 
Gravity Forms Hooks & Filters
Gravity Forms Hooks & FiltersGravity Forms Hooks & Filters
Gravity Forms Hooks & Filtersiamdangavin
 
WordPress overloading Gravityforms using hooks, filters and extending classes
WordPress overloading Gravityforms using hooks, filters and extending classes WordPress overloading Gravityforms using hooks, filters and extending classes
WordPress overloading Gravityforms using hooks, filters and extending classes Paul Bearne
 
Advanced modulinos trial
Advanced modulinos trialAdvanced modulinos trial
Advanced modulinos trialbrian d foy
 
Introduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHPIntroduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHPwahidullah mudaser
 
Advanced modulinos
Advanced modulinosAdvanced modulinos
Advanced modulinosbrian d foy
 
Flying under the radar
Flying under the radarFlying under the radar
Flying under the radarMark Baker
 
Decent exposure: Controladores sin @ivars
Decent exposure: Controladores sin @ivarsDecent exposure: Controladores sin @ivars
Decent exposure: Controladores sin @ivarsLeonardo Soto
 
PHP Tips for certification - OdW13
PHP Tips for certification - OdW13PHP Tips for certification - OdW13
PHP Tips for certification - OdW13julien pauli
 
Zend Server: Not just a PHP stack
Zend Server: Not just a PHP stackZend Server: Not just a PHP stack
Zend Server: Not just a PHP stackJeroen van Dijk
 

Was ist angesagt? (20)

Just-In-Time Compiler in PHP 8
Just-In-Time Compiler in PHP 8Just-In-Time Compiler in PHP 8
Just-In-Time Compiler in PHP 8
 
2014 database - course 2 - php
2014 database - course 2 - php2014 database - course 2 - php
2014 database - course 2 - php
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
Refactoring using Codeception
Refactoring using CodeceptionRefactoring using Codeception
Refactoring using Codeception
 
Clear php reference
Clear php referenceClear php reference
Clear php reference
 
symfony & jQuery (PUG)
symfony & jQuery (PUG)symfony & jQuery (PUG)
symfony & jQuery (PUG)
 
Zero to SOLID
Zero to SOLIDZero to SOLID
Zero to SOLID
 
Gravity Forms Hooks & Filters
Gravity Forms Hooks & FiltersGravity Forms Hooks & Filters
Gravity Forms Hooks & Filters
 
WordPress overloading Gravityforms using hooks, filters and extending classes
WordPress overloading Gravityforms using hooks, filters and extending classes WordPress overloading Gravityforms using hooks, filters and extending classes
WordPress overloading Gravityforms using hooks, filters and extending classes
 
Advanced modulinos trial
Advanced modulinos trialAdvanced modulinos trial
Advanced modulinos trial
 
Introduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHPIntroduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHP
 
Advanced modulinos
Advanced modulinosAdvanced modulinos
Advanced modulinos
 
Session1+2
Session1+2Session1+2
Session1+2
 
Flying under the radar
Flying under the radarFlying under the radar
Flying under the radar
 
PHPSpec BDD Framework
PHPSpec BDD FrameworkPHPSpec BDD Framework
PHPSpec BDD Framework
 
Session3
Session3Session3
Session3
 
Decent exposure: Controladores sin @ivars
Decent exposure: Controladores sin @ivarsDecent exposure: Controladores sin @ivars
Decent exposure: Controladores sin @ivars
 
PHP Tips for certification - OdW13
PHP Tips for certification - OdW13PHP Tips for certification - OdW13
PHP Tips for certification - OdW13
 
Zend Server: Not just a PHP stack
Zend Server: Not just a PHP stackZend Server: Not just a PHP stack
Zend Server: Not just a PHP stack
 
slidesharenew1
slidesharenew1slidesharenew1
slidesharenew1
 

Ähnlich wie PHP Pass by Value and Reference Explained

PHP7 - Scalar Type Hints & Return Types
PHP7 - Scalar Type Hints & Return TypesPHP7 - Scalar Type Hints & Return Types
PHP7 - Scalar Type Hints & Return TypesEric Poe
 
Mike King - Storytelling by Numbers MKTFEST 2014
Mike King - Storytelling by Numbers MKTFEST 2014Mike King - Storytelling by Numbers MKTFEST 2014
Mike King - Storytelling by Numbers MKTFEST 2014Marketing Festival
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?Nikita Popov
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHPBradley Holt
 
GettingStartedWithPHP
GettingStartedWithPHPGettingStartedWithPHP
GettingStartedWithPHPNat Weerawan
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐいHisateru Tanaka
 
php programming.pptx
php programming.pptxphp programming.pptx
php programming.pptxrani marri
 
Electrify your code with PHP Generators
Electrify your code with PHP GeneratorsElectrify your code with PHP Generators
Electrify your code with PHP GeneratorsMark Baker
 
Expressions and Operators.pptx
Expressions and Operators.pptxExpressions and Operators.pptx
Expressions and Operators.pptxJapneet9
 
Functions in PHP.pptx
Functions in PHP.pptxFunctions in PHP.pptx
Functions in PHP.pptxJapneet9
 
[PL] Jak nie zostać "programistą" PHP?
[PL] Jak nie zostać "programistą" PHP?[PL] Jak nie zostać "programistą" PHP?
[PL] Jak nie zostać "programistą" PHP?Radek Benkel
 
Introducation to php for beginners
Introducation to php for beginners Introducation to php for beginners
Introducation to php for beginners musrath mohammad
 

Ähnlich wie PHP Pass by Value and Reference Explained (20)

PHP7 - Scalar Type Hints & Return Types
PHP7 - Scalar Type Hints & Return TypesPHP7 - Scalar Type Hints & Return Types
PHP7 - Scalar Type Hints & Return Types
 
Mike King - Storytelling by Numbers MKTFEST 2014
Mike King - Storytelling by Numbers MKTFEST 2014Mike King - Storytelling by Numbers MKTFEST 2014
Mike King - Storytelling by Numbers MKTFEST 2014
 
What's new in PHP 8.0?
What's new in PHP 8.0?What's new in PHP 8.0?
What's new in PHP 8.0?
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Basic PHP
Basic PHPBasic PHP
Basic PHP
 
php AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdfphp AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdf
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
Php
PhpPhp
Php
 
GettingStartedWithPHP
GettingStartedWithPHPGettingStartedWithPHP
GettingStartedWithPHP
 
My cool new Slideshow!
My cool new Slideshow!My cool new Slideshow!
My cool new Slideshow!
 
Php Basic
Php BasicPhp Basic
Php Basic
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
 
php programming.pptx
php programming.pptxphp programming.pptx
php programming.pptx
 
Electrify your code with PHP Generators
Electrify your code with PHP GeneratorsElectrify your code with PHP Generators
Electrify your code with PHP Generators
 
Expressions and Operators.pptx
Expressions and Operators.pptxExpressions and Operators.pptx
Expressions and Operators.pptx
 
Functions in PHP.pptx
Functions in PHP.pptxFunctions in PHP.pptx
Functions in PHP.pptx
 
[PL] Jak nie zostać "programistą" PHP?
[PL] Jak nie zostać "programistą" PHP?[PL] Jak nie zostać "programistą" PHP?
[PL] Jak nie zostać "programistą" PHP?
 
PHP Basic
PHP BasicPHP Basic
PHP Basic
 
Introducation to php for beginners
Introducation to php for beginners Introducation to php for beginners
Introducation to php for beginners
 

Kürzlich hochgeladen

Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 

Kürzlich hochgeladen (20)

Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 

PHP Pass by Value and Reference Explained

  • 2. PHP
  • 3. PHP By default, function arguments are passed by value (so that if the value of the argument within the function is changed, it does not get changed outside of the function). http://php.net/manual/en/functions.arguments.php
  • 5. C
  • 6. C ? void foo(int var) { var++; } void main() { int a = 5; foo(a); /* a is 5 here */ }
  • 7. PHP You can pass a variable by reference to a function so the function can modify the variable. The syntax is as follows: http://php.net/manual/en/language.references.pass.php
  • 9. C
  • 10. C ? void foo(int* var) { (*var)++; } void main() { int a = 5; foo(&a); /* a is 6 here */ }
  • 11. PHP References in PHP are a means to access the same variable content by different names. They are not like C pointers; for instance, you cannot perform pointer arithmetic using them, they are not actual memory addresses, and so on. http://php.net/manual/en/language.references.whatare.php
  • 13. C ? References in PHP are a means to access the same variable content by different names. They are not like C pointers; for instance, you cannot perform pointer arithmetic using them, they are not actual memory addresses, and so on. http://php.net/manual/en/language.references.whatare.php
  • 14. C ?
  • 15. PHP
  • 16. C ???
  • 17. PHP copy-on-write PHP is a dynamic, loosely typed language, that uses copy- on-write and reference counting. http://php.net/manual/en/internals2.variables.intro.php
  • 18. C ?
  • 19. PHP
  • 20. C ???
  • 21. PHP
  • 23. PHP typedef struct _zval_struct { zvalue_value value; /* variable value */ zend_uint refcount__gc; /* reference counter zend_uchar type; /* value type */ zend_uchar is_ref__gc; /* reference flag */ } zval; is_ref__gc = PHP &
  • 25. <?php $a = 100; xdebug_debug_zval('a'); //==> a: (refcount=1, is_ref=0)=100
  • 26. <?php $a = 100; $b = $a; xdebug_debug_zval('a'); xdebug_debug_zval('b'); //==> a: (refcount=2, is_ref=0)=100 //==> b: (refcount=2, is_ref=0)=100 $a $b (refcount )
  • 27. <?php $a = 100; $b = $a; $c = $a; xdebug_debug_zval('a'); xdebug_debug_zval('b'); xdebug_debug_zval('c'); //==> a: (refcount=3, is_ref=0)=100 //==> b: (refcount=3, is_ref=0)=100 //==> c: (refcount=3, is_ref=0)=100
  • 28. $a <?php $a = 100; $b = $a; $c = $a; $a++; xdebug_debug_zval('a'); xdebug_debug_zval('b'); xdebug_debug_zval('c'); //==> a: (refcount=1, is_ref=0)=101 //==> b: (refcount=2, is_ref=0)=100 //==> c: (refcount=2, is_ref=0)=100 $a $b $c copy-on-write
  • 29. $c <?php $a = 100; $b = $a; $c = $a; $a++; unset($c); xdebug_debug_zval('a'); xdebug_debug_zval('b'); xdebug_debug_zval('c'); //==> a: (refcount=1, is_ref=0)=101 //==> b: (refcount=1, is_ref=0)=100 //==> c: no such symbol refcount
  • 30. PHP
  • 31. <?php $a = 100; xdebug_debug_zval('a'); //==> a: (refcount=1, is_ref=0)=100
  • 32. <?php $a = 100; $b = &$a; xdebug_debug_zval('a'); xdebug_debug_zval('b'); //==> a: (refcount=2, is_ref=1)=100 //==> b: (refcount=2, is_ref=1)=100 (refcount = 2) PHP & (is_ref = 1)
  • 33. $a <?php $a = 100; $b = &$a; $a++; xdebug_debug_zval('a'); xdebug_debug_zval('b'); //==> a: (refcount=2, is_ref=1)=101 //==> b: (refcount=2, is_ref=1)=101 $a $b PHP &
  • 34. C
  • 35. C
  • 37. PHP <?php $a = 100; $b = &$a; $c = $a; xdebug_debug_zval('a'); xdebug_debug_zval('b'); xdebug_debug_zval('c'); //==> a: (refcount=2, is_ref=1)=100 //==> b: (refcount=2, is_ref=1)=100 //==> c: (refcount=1, is_ref=0)=100 $c = $a $a $b $a $c (& ) is_ref is_ref=1 is_ref=0
  • 38. C ? ?
  • 39. PHP PHP : 1885 : 1753 : 132
  • 40. PHP
  • 41. <?php calculareRankingScores($works, $users); ... function calculareRankingScores(&$works, &$users) { for ($i = 0; $i < count($works);) { ... count($array) O(1) count($array_passed_by_reference) O(n) calculareRankingScores() O(n^2) (※ )
  • 42. : PHP <?php $a = 100; $b = &$a; unset($b); xdebug_debug_zval('a'); xdebug_debug_zval('b'); //==> a: (refcount=1, is_ref=0)=100 //==> b: no such symbol refcount 1 is_ref 0 PHP & 1 copy-on-write is_ref = 0