function test1($i) { echo $i, '<br/>'; //1 ++$i; if ($i <= 4) { test1($i); //test1(2) /* echo $i,'<br/>';//2 ++$i; if($i<=10){ test1($i);//test1(3) echo $i,'<br/>';//3 ++$i; if($i<=4){ test1($i);//test1(4) echo $i,'<br/>';//4 ++$i; if($i<=4){ test1($i); } } } */ } }
public function run() { $r = 0; for ($i = 0; $i < self::DEFAULT_ITERATIONS; $i++) { $r += test1() + test2(1, 2, 3) + self::test1() + self::test2(1, 2, 3); } }
function content_5236ae0304b8c2_40162505($_smarty_tpl) { ?> <h1>自定义函数yu 以block 方式显示</h1> <?php $_smarty_tpl->smarty->_tag_stack[] = array('yu2', array('times' => "5", 'size' => "5", 'color' => "blue")); $_block_repeat = true; echo test2(array('times' => "5", 'size' => "5", 'color' => "blue"), null, $_smarty_tpl, $_block_repeat); while ($_block_repeat) { ob_start(); ?> hello world<?php $_block_content = ob_get_clean(); $_block_repeat = false; echo test2(array('times' => "5", 'size' => "5", 'color' => "blue"), $_block_content, $_smarty_tpl, $_block_repeat); } array_pop($_smarty_tpl->smarty->_tag_stack); ?> <h1>自定义函数yu 以function 方式显示</h1> <?php echo test1(array('times' => "5", 'size' => "5", 'content' => "hello 栩坚", 'color' => "red"), $_smarty_tpl); ?> <?php }
function test($test) { test1(); TeSt1(); $test(); $test = strtolower($test); $test(1, 2, 3); }
function test1($path = '', $lv = 0) { file_exists($path) or die($path . ' is not find'); $head = opendir($path) or die('open the dir is error'); while (false !== ($file = readdir($head))) { $sub_dir = $path . '\\' . $file; if ($file == '.' || $file == '..') { continue; } else { if (is_dir($sub_dir)) { echo str_repeat('-', $lv) . $file . "\n"; test1($sub_dir, $lv + 1); } else { echo str_repeat('-', $lv) . $file . "\n"; } } } }
//5 echo '<hr/>'; $j = 10; echo $i, '<br/>'; //10 unset($i); // var_dump($i); //NULL echo $j, '<br/>'; function test1(&$i) { $i = 15; } $i = 2; test1($i); var_dump($i); //15 echo '<hr/>'; /* Fatal error: Only variables can be passed by reference in F:\psd1507\study\PHPCORE\day08\demo\func4.php on line 50 */ // test1(3); $filename = '1.txt.jpeg'; /* Strict Standards: Only variables should be passed by reference in F:\psd1507\study\PHPCORE\day08\demo\func4.php on line 58 jpeg */ $arr = explode('.', $filename); echo end($arr);
[file] <?php // test1 echo "test1 (PassedByCopy):\n"; function test1($x) { $x[] = 4; var_dump($x); return $x; } $x = array(); $x[0] = 1; $x[1] = 2; $x[2] = 3; unset($x[2]); $x = test1($x); $x[] = 5; var_dump($x); // test2 echo "\n\ntest2 (ReturnedByCopy):\n"; function test2() { $x = array(1, 2, 3); unset($x[2]); var_dump($x); return $x; } $x = test2($x); $x[] = 4; var_dump($x); // test3
$favorites = array('favcolor' => 'red', 'favmovie' => 'shawshank', 'favsong' => 'Yellow submarine'); function test1($favorites) { $buf = ''; foreach ($favorites as $fav) { $buf .= $fav; } echo 'Test foreach($arr as $val)' . "<br>\n"; echo 'result: ' . ($buf === 'redshawshankYellow submarine' ? 'pass' : 'fail') . "<br><br>\n\n"; } function test2($favorites) { $buf = ''; foreach ($favorites as $key => $val) { $buf .= "{$key}: {$val},"; } echo 'Test foreach($arr as $key => $val)' . "<br>\n"; echo 'result: ' . ($buf === 'favcolor: red,favmovie: shawshank,favsong: Yellow submarine,' ? 'pass' : 'fail') . "<br><br>\n\n"; } function test3() { $arr = array(1, 2, 3); foreach ($arr as $i) { $i += 1; } echo 'Test foreach($arr as $val) { $val += 1 }' . "<br>\n"; echo 'result: ' . ($i == 4 ? 'pass' : 'fail') . "<br><br>\n\n"; } test1($favorites); test2($favorites); test3();
<?php function test1(&$abc) : string { return $abc; } function &test2(int $abc) : string { return $abc; } function &test3(int &$abc) : string { return $abc; } $a = 123; var_dump(test1($a)); var_dump($a); var_dump(test2($a)); var_dump($a); var_dump(test3($a)); var_dump($a);
<?php use Lavoiesl\PhpBenchmark\Benchmark; use Ovr\Bench\AssignOrNot\ReturnAfterAssignProperty; use Ovr\Bench\AssignOrNot\ReturnWithoutAfterAssignProperty; include_once __DIR__ . '/vendor/autoload.php'; $benchmark = new Benchmark(); function proxy($a, $b, $c) { return true; } function test1() { return proxy(...func_get_args()); } function test2(...$args) { return proxy(...$args); } $benchmark->add('...func_get_args()', function () use(&$str) { return test1(1, 2, 3); }); $benchmark->add('....$args', function () use(&$str) { return test2(1, 2, 3); }); $benchmark->setCount(10000000); $benchmark->run();
function test2($a, $b) { test1($b, $a); }
<?php /* Smarty version 2.6.27, created on 2013-09-16 20:17:57 compiled from example7.tpl */ require_once SMARTY_CORE_DIR . 'core.load_plugins.php'; smarty_core_load_plugins(array('plugins' => array(array('function', 'yu', 'example7.tpl', 3, false), array('block', 'yu2', 'example7.tpl', 6, false))), $this); ?> <h1>自定义函数yu 以function 方式显示</h1> <?php echo test1(array('times' => '5', 'size' => '5', 'content' => "hello 栩坚", 'color' => 'red'), $this); ?> <h1>自定义函数yu 以block 方式显示</h1> <?php $this->_tag_stack[] = array('yu2', array('times' => '5', 'size' => '5', 'color' => 'blue')); $_block_repeat = true; test2($this->_tag_stack[count($this->_tag_stack) - 1][1], null, $this, $_block_repeat); while ($_block_repeat) { ob_start(); ?> hello world<?php $_block_content = ob_get_contents(); ob_end_clean(); $_block_repeat = false; echo test2($this->_tag_stack[count($this->_tag_stack) - 1][1], $_block_content, $this, $_block_repeat); } array_pop($this->_tag_stack);
<?php function sumArray($intArray) { $total = 0; foreach ($intArray as $value) { $total += $value; } return $total; } function test1(Form $form) { return 1; } function test2(array $array) { return sumArray($array); } $test1 = test1(null); assert($test1, 1); $testArray = array(1, 2, 3); $test2 = test2($testArray); assert($test2, 6); testEnd();
function main() { var_dump(test1()); }
<?php function test1() { $a = array(__FUNCTION__, __LINE__); return $a; } function test2() { $a = array(__FUNCTION__, __LINE__); return $a; } var_dump(test1()); var_dump(test2());
} test1(...[1, 2, 3]); $array = [1, 2, 3]; test1(...$array); var_dump($array); $array1 = [1, 2]; $array2 = [3, 4]; test1(...$array1, ...$array2); var_dump($array1, $array2); function test2($val1, &$ref1, $val2, &$ref2) { $ref1++; $ref2++; } $array = [0, 0, 0, 0]; test2(...$array); var_dump($array); $array1 = [1, 2]; $array2 = [4, 5]; test1(...$array1, ...$array2); var_dump($array1, $array2); $a = $b = $c = $d = 0; $array = [0, 0, 0, 0]; test2($a, ...$array); var_dump($a, $array); test2($a, $b, ...$array); var_dump($a, $b, $array); test2($a, $b, $c, ...$array); var_dump($a, $b, $c, $array); test2($a, $b, $c, $d, ...$array); var_dump($a, $b, $c, $d, $array);
/** * @ProfileGroup("Foo Group") * @ProfileCaption("Caption of bar2 #1 #2.") */ function bar2($lambda, $object) { return test1(); }
<?php function test1() : Generator { (yield 1); } function test2() : Iterator { (yield 2); } function test3() : Traversable { (yield 3); } var_dump(test1(), test2(), test3());
<?php function test() { $GLOBALS['a']['b'] = 1; $GLOBALS['c'] = 1; } function test1(array $arr) { } function test2(int $arr) { } test(); test1($c); test2($a);
<?php function test1(...$args) { var_dump($args); } test1(); test1(1); test1(1, 2, 3); function test2($arg1, $arg2, ...$args) { var_dump($arg1, $arg2, $args); } test2(1, 2); test2(1, 2, 3); test2(1, 2, 3, 4, 5);
<?php header('Content-Type:text/html; charset=utf-8'); function test1(&$a){ $a += 1; } $b = 5; test1($b); echo $b; ?>
function test2($param) { echo $param . ' test2'; } function test3($param) { echo $param . ' test3'; } for ($i = 0; $i < count($argv); $i++) { if ($argv[$i] == '-s') { $param = $argv[$i + 1]; run($param); } else { if ($argv[$i] == '-d') { $param = $argv[$i + 1]; test1($param); } else { if ($argv[$i] == '-f') { $param = $argv[$i + 1]; test2($param); } else { if ($argv[$i] == '--override') { test3('override dan gelen '); } } } } } if ($argc < 2) { $file = basename(__FILE__); print "STNC Autoincrement Generator" . CRLF;
<?php require "tests.php"; require "grouping.php"; check::functions(array("test1", "test2", "do_unary", "negate")); check::equal(5, test1(5), "5==test1(5)"); check::resource(test2(7), "_p_int", "_p_int==test2(7)"); check::globals(array(test3)); //check::equal(37,test3_get(),'37==test3_get()'); check::equal(37, check::get("test3"), '37==get(test3)'); //test3_set(38); check::set(test3, 38); //check::equal(38,test3_get(),'38==test3_get() after test3_set(37)'); check::equal(38, check::get(test3), '38==get(test3) after set(test)'); check::equal(-5, negate(5), "-5==negate(5)"); check::equal(7, do_unary(-7, NEGATE), "7=do_unary(-7,NEGATE)"); check::done();
class bar { } function test1(bar $bar) { echo "ok\n"; } function test2(\foo\bar $bar) { echo "ok\n"; } function test3(\foo\bar $bar) { echo "ok\n"; } function test4(\Exception $e) { echo "ok\n"; } function test5(\bar $bar) { echo "bug\n"; } $x = new bar(); $y = new \Exception(); test1($x); test2($x); test3($x); test4($y); test5($x);
<?php function test1() { var_dump(func_get_args()); } function test2($a) { var_dump(func_get_args()); } function test3($a, $b) { var_dump(func_get_args()); } test1(); test1(10); test2(1); test2(); test3(1, 2); call_user_func("test1"); call_user_func("test3", 1); call_user_func("test3", 1, 2); class test { static function test1($a) { var_dump(func_get_args()); } } test::test1(1); var_dump(func_get_args());
<?php // I have no idea what is the difference between using // $o = new A(); and $o =& new A(); ... // Drew: "Historically, '$a =& new A();' used to get special treatment // because of how objects worked on PHP 4. HipHop currently has a bug // where it converts '$a =& new A();' to '$a = new A();' class A { } function test1() { $o = new A(); var_dump($o); return $o; } $o = test1(); var_dump($o); function &test2() { $o =& new A(); var_dump($o); return $o; } $o2 = test2(); var_dump($o2);
} function g() { } function test1($a) { $buf = ''; foreach ($a as $s) { $buf .= f() . g() . 'h' . f() . 'h' . g(); } foreach ($a as $s) { $buf .= $s . 'h' . $s; } return $buf; } var_dump(test1(array(1))); function test2() { return f() . g() . f() . g(); } var_dump(test2()); function test3() { return f() . g() . f() . g() . f() . g() . f() . g() . f(); } var_dump(test3()); function test4() { $s = f(); $s .= 'foo' . 'bar' . f() . 'foo' . 'baz' . f() . 'fuz' . 'boo' . f() . 'fiz' . 'faz'; $s .= f();
<?php function test1($a) { $a = 100; } function test2(&$b) { $b = 200; } $a = 10; echo $a; //10 test1($a); echo $a; //10 echo "<br/>"; $b = 20; echo $b = 20; //20 test2($b); echo $b; ///200
<head> <title>Variable Scope</title> </head> <body> <?php // Set variable at global scope $var = 1; // Define function containing local variable function test1() { $var = 2; echo $var; echo "<br />"; } // Call function test1(); // Echo global echo $var . "<br />"; echo "<hr />"; // Define function scope global variable function test2() { global $var; $var = 2; echo $var; echo "<br />"; } // Call function test2(); echo $var . "<br />"; echo "<hr />";
<?php function test1() { $x = 20; $y = 30; $z = 10; $func1 = function () use(&$x, &$y, $z) { $x = 40; $y = 50; $z = 60; }; $func1(); return $x + $y + $z; } $x = 20; $y = 30; $func2 = function () use(&$x, $y) { $x = 50; $y = 50; }; if (($r = test1()) !== 100) { return "fail_1: {$r} != 100"; } $func2(); if (($r = $x + $y) !== 80) { return "fail_2: {$r} != 80"; } return 'success';