- array_push: documentation ( source)
- var_dump: documentation ( source)
- fgets: documentation ( source)
- fwrite: documentation ( source)
- rewind: documentation ( source)
- fopen: documentation ( source)
<?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);