@ 2025-08-11T20:20:02Z <?php
/*
Illustrating PHP Function parameters.
*/
class o {
private $v = 0;
public function inc() {
$this->v++;
}
public function getV() {
return $this->v;
}
}
function testParams(o $obj) {
for ($x=0; $x < 10; $x++) {
$obj->inc();
}
return $obj->getV();
}
function testParams2($fp, $text) {
$text = "1." . $text . PHP_EOL;
fwrite($fp, $text);
}
// Pass Array by Reference
function testParams3(Array &$globArray) {
array_push($globArray, 'grape');
}
// Globally scoped variables
$globArray = ['apple', 'banana', 'peach'];
$obj = new o();
$r = fopen("/tmp/resource.txt", "w+");
$text = "This is some text.";
//
$retVal = testParams($obj);
echo $retVal . PHP_EOL;
echo $obj->getV() . PHP_EOL;
echo PHP_EOL;
echo "text before testParams2: \n";
echo "\t$text" . PHP_EOL;
testParams2($r, $text);
rewind($r);
$fileData = fgets($r, 4096);
echo "File Data:\n";
echo "\t$fileData";
echo "text After testParams2::\n";
echo "\t$text" . PHP_EOL;
echo PHP_EOL;
echo "Array Before testParams3:\n";
var_dump($globArray);
echo PHP_EOL;
testParams3($globArray);
echo "Array After testParams3:\n";
var_dump($globArray);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.2.0 - 8.2.29 , 8.3.0 - 8.3.25 , 8.4.1 - 8.4.12 10
10
text before testParams2:
This is some text.
File Data:
1.This is some text.
text After testParams2::
This is some text.
Array Before testParams3:
array(3) {
[0]=>
string(5) "apple"
[1]=>
string(6) "banana"
[2]=>
string(5) "peach"
}
Array After testParams3:
array(4) {
[0]=>
string(5) "apple"
[1]=>
string(6) "banana"
[2]=>
string(5) "peach"
[3]=>
string(5) "grape"
}
preferences:dark mode live preview ace vim emacs key bindings
43.61 ms | 407 KiB | 5 Q