tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
28
Task/Pointers-and-references/PHP/pointers-and-references.php
Normal file
28
Task/Pointers-and-references/PHP/pointers-and-references.php
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
/* Assignment of scalar variables */
|
||||
$a = 1;
|
||||
$b =& $a; // $b and $a are now linked together
|
||||
$b = 2; //both $b and $a now equal 2
|
||||
$c = $b;
|
||||
$c = 7; //$c is not a reference; no change to $a or $b
|
||||
unset($a); //won't unset $b, just $a.
|
||||
|
||||
/* Passing by Reference in and out of functions */
|
||||
function &pass_out() {
|
||||
global $filestr; //$exactly equivalent to: $filestr =& $_GLOBALS['filestr'];
|
||||
|
||||
$filestr = get_file_contents("./bigfile.txt");
|
||||
return $_GLOBALS['filestr'];
|
||||
}
|
||||
function pass_in(&$in_filestr) {
|
||||
echo "File Content Length: ". strlen($in_filestr);
|
||||
|
||||
/* Changing $in_filestr also changes the global $filestr and $tmp */
|
||||
$in_filestr .= "EDIT";
|
||||
echo "File Content Length is now longer: ". strlen($in_filestr);
|
||||
}
|
||||
|
||||
$tmp = &pass_out(); // now $tmp and the global variable $filestr are linked
|
||||
pass_in($tmp); // changes $tmp and prints the length
|
||||
|
||||
?>
|
||||
Loading…
Add table
Add a link
Reference in a new issue