Last active
February 19, 2022 02:52
-
-
Save hochun836/687fca59093d5e094da136c757739bfc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
>php --version | |
PHP 7.4.16 (cli) (built: Mar 2 2021 14:06:15) ( ZTS Visual C++ 2017 x64 ) | |
Copyright (c) The PHP Group | |
Zend Engine v3.4.0, Copyright (c) Zend Technologies | |
>composer --version | |
Composer version 2.2.5 2022-01-21 17:25:52 | |
--- | |
# base | |
cgi (common gateway interface) => php-cgi | |
fastcgi (fast common gateway interface) => php-fpm (fastcgi process manager) | |
php.exe | |
php-cgi.exe | |
php-win.exe | |
=> ref: https://www.bilibili.com/video/BV1ra4y1L7Wn | |
=> ref: https://www.itread01.com/content/1543133893.html | |
=> ref: https://cloud.tencent.com/developer/news/678417 | |
# php | |
php --version | |
php --ini | |
php -h // help | |
php -a // run as interactive shell | |
php -i // show php information (<=> builtin function phpinfo()) | |
php -m // show php modules (aka. extensions) | |
php <file.php> | |
php -l <file.php> // syntax check only (lint) | |
# composer | |
composer --version | |
composer --help // <=> composer help | |
composer <command> [options] [arguments] | |
composer list -h | |
composer list // list all commands | |
composer init -h // <=> composer help init | |
composer global require <package> | |
composer global remove <package> | |
composer global show | |
composer install // install dependencies by composer.json "require" (this will create "vendor" folder) | |
composer run <script> // run script by composer.json "scripts" | |
composer require <package> | |
composer remove <package> | |
# [note] how to install php + composer | |
- php | |
https://windows.php.net/download/ | |
- composer | |
https://getcomposer.org/download/ | |
IMPORTANT: | |
1. php is written by c language | |
2. composer requires that you have php already installed | |
# [note] ?> vs. ; | |
see 'end.php' | |
=> ref: https://www.bilibili.com/video/BV18x411H7qD?p=17 | |
# [note] php include vs. require | |
include: if the target file is not found, then warning | |
require: if the target file is not found, then error | |
IMPORTANT: include vs. include_once | |
include: do every time | |
include_once: do only when first time | |
IMPORTANT: using relative path | |
|- a1.php // include './xx/b1.php' | |
|- xx | |
|- b1.php // include './yy/c1.php' | |
|- yy | |
|- c1. php | |
php a1.php // something wrong | |
|- a1.php // include './xx/b1.php' | |
|- xx | |
|- b1.php // include './xx/yy/c1.php' // IMPORTANT | |
|- yy | |
|- c1. php | |
php a1.php // work | |
IMPORTANT: using return | |
|- a1.php // $b1 = include './xx/b1.php' // 100 | |
|- xx | |
|- b1.php // return 100 | |
php a1.php // work | |
# [note] php use (like python import) | |
TODO | |
=> ref: https://jaceju.net/php-include-path/ | |
=> ref: http://justericgg.logdown.com/posts/196891-php-series-autoload | |
# [note] type hint with nullable | |
=> ref: https://www.php.net/manual/en/migration71.new-features.php | |
# [note] how to create lumen project | |
composer global require laravel/installer // for laravel | |
composer global require laravel/lumen-installer // for lumen | |
lumen --version // version of Lumen Installer | |
lumen new <project-name> | |
composer create-project --prefer-dist laravel/lumen <project-name> | |
php artisan --version // version of Lumen | |
php artisan // show usage of Lumen | |
# [note] how to run lumen project | |
cd <project-name> | |
php -S localhost:8000 -t public | |
-S <addr>:<port> run with built-in web server | |
-t <docroot> specify document root <docroot> for built-in web server | |
# [note] php shell (enter by php -a) | |
echo 100; // ; needs | |
function a() {echo 200;} // cannot redeclare | |
a(); | |
exit | |
# [note] how to debug php in vscode | |
step1. php install "xdeubg.dll" // use https://xdebug.org/wizard | |
step2. setting xdeubg in php.ini // some settings are different from xdebug v2 and v3 | |
step3. vscode install "PHP Debug" | |
step4. start server in cmd | |
step5. start xdebug in vscode | |
=> ref: https://www.facebook.com/photo.php?fbid=4373264436058581 | |
=> ref: https://dotblogs.com.tw/ianchiu28/2017/06/15/180900 | |
# [note] how to debug php in phpstorm | |
=> ref: https://jsnwork.kiiuo.com/archives/3079/php-phpstorm-%E8%A8%AD%E5%AE%9A-debug/ | |
# [note] phpstorm recommended theme (vscode dark theme) | |
=> ref: https://plugins.jetbrains.com/plugin/12255-visual-studio-code-dark-plus-theme | |
# [note] phpstorm change current line color | |
step1. Settings | Editor | Color Scheme | General | |
step2. Editor | Caret row | |
step3. #04352f | |
=> ref: https://intellij-support.jetbrains.com/hc/en-us/community/posts/206335019-Change-current-line-highlight | |
# [note] phpstorm code inspection | |
Required parameter 'xxx' missing | |
Expected parameter of type 'xxx', 'int' provided | |
=> ref: https://www.jetbrains.com/help/phpstorm/php-parameter-type.html | |
Method call is provided 2 parameters, but the method signature uses 1 parameters | |
=> ref: https://www.jetbrains.com/help/phpstorm/php-parameters-number-mismatch-declaration.html | |
Property 'xxx' not found in yyy | |
=> ref: https://www.jetbrains.com/help/phpstorm/php-undefined-property.html | |
# [note] phpstorm add datasource and dialect | |
=> ref: https://www.jetbrains.com/help/idea/database-tool-window.html | |
=> ref: https://www.jetbrains.com/help/phpstorm/settings-languages-sql-dialects.html | |
# [note] phpstorm convert table to class (.php) | |
=> ref: https://blog.jetbrains.com/datagrip/2018/02/13/generate-pojos/ | |
=> ref: https://www.cnblogs.com/jiangfeilong/p/11253449.html | |
# [note] phpstorm activation code | |
=> ref: https://gist.github.com/shahadul878/5f33b90e55c1655aee05fa239ec1b978 | |
# [note] learn | |
=> ref: https://www.youtube.com/watch?v=rFDcGBO4PWc&list=PL2SrkGHjnWcyOquWJIEd7AYAJGMAUdp-a | |
=> ref: https://www.bilibili.com/video/BV18x411H7qD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo date('Y-m-d H:i:s'), PHP_EOL; | |
echo time(), PHP_EOL; | |
echo microtime(), PHP_EOL; | |
echo strtotime('now'), PHP_EOL; | |
echo strtotime('today'), PHP_EOL; | |
// -------------------------------------------------------- | |
echo function_exists('func'), PHP_EOL; // 1 | |
func(100, 200, 300); | |
function func($a, $b) { | |
// no need echo | |
var_dump(func_get_arg(0)); // int(100) | |
var_dump(func_get_arg(1)); // int(200) | |
var_dump(func_get_arg(2)); // int(300) | |
// var_dump(func_get_arg(3)); // warning: func_get_arg(): argument 3 not passed to function | |
var_dump(func_get_args()); // array(3) { [0]=>int(100), [1]=>int(200), [2]=>int(300) } | |
var_dump(func_num_args()); // int(3) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo 100; // ; cannot be omitted | |
echo 200; // ; can be omtted if | |
// 1. this line is last | |
// and 2. there is the ?> | |
?> // if ?> is omtted | |
// then the following will be treated as php code | |
// shortcoming: | |
// if ?> isn't omtted, then | |
// this line will be output | |
// this line will be output | |
// this line will be output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// 1. Built-in Variables | |
$GLOBALS | |
$_ENV | |
$_GET | |
$_POST | |
$_REQUEST | |
$_SERVER | |
$_SESSION | |
$_COOKIE | |
$_FILES | |
// 2. Variable Variables | |
$a = 'b'; | |
$b = 100; | |
echo $$a; // 100 | |
// 3. Pass by Value or Reference | |
$p = 10; | |
$q = $p; | |
$q = 20; | |
echo $p, ' ', $q; | |
$p = 10; | |
$q = &$p; // IMPORTANT | |
$q = 20; | |
echo $p, ' ', $q; | |
$p = ['name' => 'peter']; | |
$q = $p; // https://stackoverflow.com/questions/2030906/are-arrays-in-php-copied-as-value-or-as-reference-to-new-variables-and-when-pas | |
$q['name'] = 'kang'; | |
echo var_dump($p), ' ', var_dump($q); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// 1. Built-in Constants | |
PHP_VERSION | |
PHP_INT_SIZE | |
PHP_INT_MAX | |
PHP_EOL | |
// Magic Constants | |
// Def: user cannot change them, but they can be changed by different environments | |
__DIR__ | |
__FILE__ | |
__LINE__ | |
__NAMESPACE__ | |
__CLASS__ | |
__METHOD__ | |
__FUNCTION__ | |
// 2. Declare and Use | |
define('A1', 100); | |
const A2 = 200; // no need $ | |
echo A1, ' ', A2, PHP_EOL; | |
echo constant('A1'), PHP_EOL; | |
echo constant('A2'), PHP_EOL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// 1. base | |
integer | |
float | |
string | |
boolean | |
// 2. complex | |
object | |
array | |
// 3. special | |
resource | |
null | |
// the following type only be used in manual | |
callable | |
mixed | |
number | |
// exercise | |
$a = 'a1'; | |
$b = '1.2.3'; | |
echo (int)$a, ' ', (int)$b, PHP_EOL; | |
echo (float)$a, ' ', (float)$b, PHP_EOL; | |
$a = true; | |
$b = false; | |
echo $a, PHP_EOL; // 1 | |
echo var_dump($a); // bool(true) | |
echo $b, PHP_EOL; // | |
echo var_dump($b); // bool(false) | |
$a = null; | |
echo $a, PHP_EOL; // | |
echo var_dump($a); // NULL | |
$a; | |
echo $a; // notice: undefined variable: a | |
echo var_dump($a); // notice: undefined variable: a | |
// NULL | |
$a = '100'; | |
echo var_dump((float) $a); // float(100) | |
echo var_dump((double) $a); // float(100) | |
$a = '100'; | |
$b = (float) $a; | |
$c = (double) $a; | |
$d = 100; | |
$e = array(); | |
$f = true; | |
echo gettype($a), PHP_EOL; // string | |
echo gettype($b), PHP_EOL; // double | |
echo gettype($c), PHP_EOL; // double | |
echo gettype($d), PHP_EOL; // integer | |
echo gettype($e), PHP_EOL; // array | |
echo gettype($f), PHP_EOL; // boolean | |
settype($a, 'float'); | |
echo gettype($a), PHP_EOL; // double | |
$a = 0b111; // BIN (binary) | |
$b = 0111; // OCT (octal) | |
$c = 111; // DEC (decimal) | |
$d = 0x111; // HEX (hexadecimal) | |
echo $a, PHP_EOF; | |
echo $b, PHP_EOF; | |
echo $c, PHP_EOF; | |
echo $d, PHP_EOF; | |
$a = PHP_INT_MAX; | |
$b = PHP_INT_MAX + 1; | |
echo var_dump($a, $b); // $b is float | |
$a = 0.7; | |
$b = 2.1 / 3; | |
echo var_dump($a === $b); // bool(false) | |
$a = 100; | |
$b = '100'; | |
echo var_dump($a == $b); // bool(true) | |
echo var_dump($a === $b); // bool(false) | |
$a = true . '100'; | |
$b = false . '100'; | |
$c = null . '100'; | |
$d = 1234 . '100'; | |
$e = 1234; | |
$e .= '100'; | |
echo $a, PHP_EOL; // '1100' | |
echo $b, PHP_EOL; // '100' | |
echo $c, PHP_EOL; // '100' | |
echo $d, PHP_EOL; // '1234100' | |
echo $e, PHP_EOL; // '1234100' | |
$a = 100; | |
$b = $a > 100 ? 1 : 0; | |
echo $b; // 0 | |
$a = ['peter', 'kang']; | |
$b = ['name' => 'peter', 'age' => 27]; | |
echo var_dump($a); // array(2) { [0] => string(5) "peter" [1] => string(4) "kang" } | |
echo var_dump($b); // array(2) { ["name"] => string(5) "peter" ["age"] => int(27) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
E_ERROR | |
E_WARNING | |
E_PARSE | |
E_NOTICE | |
E_CORE_ERROR | |
E_CORE_WARNING | |
E_COMPILE_ERROR | |
E_COMPILE_WARNING | |
E_USER_ERROR | |
E_USER_WARNING | |
E_USER_NOTICE | |
E_STRICT | |
E_RECOVERABLE_ERROR | |
E_DEPRECATED | |
E_USER_DEPRECATED | |
E_ALL | |
=> ref: https://www.php.net/manual/en/errorfunc.constants.php | |
IMPORTANT: | |
You may use these constant names in php.ini but not outside of PHP, like in httpd.conf, where you'd use the bitmask values instead. | |
IMPORTANT: | |
in php.ini-development, | |
error_reporting = E_ALL | |
display_errors = On | |
log_errors = On | |
;error_log = ... | |
IMPORTANT: | |
there are two ways to edit config | |
1. edit php.ini | |
2. run xxx.php | |
ini_set(key, value); | |
// -------------------------------------------------------- | |
// trigger_error(string $message, int $error_level = E_USER_NOTICE): bool | |
// generates a 'user-level' error/warning/notice message | |
trigger_error('Wrong A...'); | |
echo 100, PHP_EOL; | |
trigger_error('Wrong B...', E_USER_ERROR); | |
echo 200, PHP_EOL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
try { | |
echo 100, PHP_EOL; | |
throw new Exception('xxx'); | |
echo 200, PHP_EOL; | |
} | |
catch (Exception $e) { | |
echo 'Caught exception: ', $e->getMessage(), PHP_EOL; | |
var_dump($e); | |
print_r($e); | |
} | |
// -------------------------------------------------------- | |
try { | |
1 / 0; | |
} | |
catch (Error | Exception $e) { | |
echo 'Go Here', PHP_EOL; // not reached | |
print_r($e); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$name = 'peter'; | |
$a = 'Hi $name'; | |
$b = "Hi $name"; | |
// $c = "Hi $name2"; // notice: undefined variable: name2 | |
$d = "Hi {$name}"; // work | |
echo $a, PHP_EOL; // Hi $name | |
echo $b, PHP_EOL; // Hi peter | |
echo $d, PHP_EOL; // Hi peter | |
// -------------------------------------------------------- | |
$name = 'peter'; | |
$a = <<<'NOW' | |
I'm $name | |
NOW; | |
$b = <<<HERE | |
I'm $name | |
HERE; | |
$c = <<<HERE | |
// a comment | |
I'm $name | |
HERE; | |
echo $a, PHP_EOL; // Hi $name | |
echo $b, PHP_EOL; // Hi peter | |
echo $c, PHP_EOL; // // a comment | |
// Hi peter | |
/* | |
IMPORTANT: | |
1. <<<KEY cannot have any spaces or letters after it | |
2. KEY cannot have any spaces or letters before it | |
*/ | |
// -------------------------------------------------------- | |
$a = 'peter'; | |
$b = '彼得'; | |
echo strlen($a), PHP_EOL; // 5 | |
echo strlen($b), PHP_EOL; // 6 | |
echo mb_strlen($a), PHP_EOL; // 5 | |
echo mb_strlen($b), PHP_EOL; // 2 | |
echo mb_strlen($b, 'ASCII'), PHP_EOL; // 6 | |
/* | |
IMPORTANT: php.ini | |
1. extension_dir = "ext" | |
2. extension=mbstring | |
*/ | |
// -------------------------------------------------------- | |
$arr = ['peter', 'kang']; | |
echo implode($arr), PHP_EOL; // peterkang | |
echo implode('-', $arr), PHP_EOL; // peter-kang | |
$arr = explode('/', '01/28'); | |
var_dump($arr); // array(2) { [0] => string(2) "01" [1] => string(2) "28" } | |
$arr1 = str_split('May'); | |
$arr2 = str_split('May', 2); | |
var_dump($arr1); // array(3) { [0] => string(1) "M" [1] => string(1) "a" [2] => string(1) "y" } | |
var_dump($arr2); // array(2) { [0] => string(1) "Ma" [1] => string(1) "y" } | |
// -------------------------------------------------------- | |
$a = ' Hello World '; | |
$b = 'Hello World'; | |
var_dump($a); // string(13) " Hello World " | |
var_dump($b); // string(11) "Hello World" | |
var_dump(trim($a)); // string(11) "Hello World" | |
var_dump(trim($b)); // string(11) "Hello World" | |
var_dump(trim($a, 'Hdle')); // string(13) " Hello World " | |
var_dump(trim($a, 'HdWr')); // string(13) " Hello World " | |
var_dump(trim($b, 'Hdle')); // string(5) "o Wor" | |
var_dump(trim($b, 'HdWr')); // string(9) "ello Worl" | |
$a = '01234c56c789'; | |
echo substr($a, 3), PHP_EOL; // 34c56c789 | |
echo substr($a, 3, 2), PHP_EOL; // 34 | |
echo strstr($a, 'c'), PHP_EOL; // c56c789 | |
// -------------------------------------------------------- | |
$a = 'abCdefGhIJ'; | |
echo strtoupper($a), PHP_EOL; // ABCDEFGHIJ | |
echo strtolower($a), PHP_EOL; // abcdefghij | |
echo ucfirst($a), PHP_EOL; // AbCdefGhIJ | |
// -------------------------------------------------------- | |
$a = 'abcdefabcdef'; | |
var_dump(str_replace('f', 'X', $a)); // string(12) "abcdeXabcdeX" | |
var_dump($a); // string(12) "abcdefabcdef" | |
// -------------------------------------------------------- | |
$a = 'abcdefabcdef'; | |
var_dump(strpos($a, 'a')); // int(0) | |
var_dump(strpos($a, 'b')); // int(1) | |
var_dump(strpos($a, 'g')); // bool(false) | |
var_dump(strrpos($a, 'a')); // int(6) | |
/* | |
IMPORTANT: | |
This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. | |
Please use the === operator for testing the return value of this function. | |
*/ | |
// -------------------------------------------------------- | |
$a = 'abcdef'; | |
var_dump(str_repeat($a, 3)); | |
var_dump(str_shuffle($a)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
PHP 數組 | |
1. "下標"可以為"整數"或"字符串" | |
若下標皆為整數,則稱"索引數組" | |
若下標皆為字符串,則稱"關連數組" | |
若下標有整數和字符串,則稱"混合數組" | |
2. "元素"的順序以"放入順序"為主 (與下標無關) | |
3. 元素的類型無限制 | |
4. 整數下標有"自增特性" | |
5. 特殊值下標會"自動轉換" | |
// -------------------------------------------------------- | |
$arr1 = array('peter', 'kang'); | |
$arr2 = ['peter', 'kang']; | |
$arr3[] = 'peter'; | |
$arr3[] = 'kang'; | |
$arr4[5] = 'peter'; | |
$arr5[5] = 'peter'; | |
$arr5['age'] = 27; | |
var_dump($arr1); // array(2) { [0] => string(5) "peter" [1] => string(4) "kang" } | |
var_dump($arr2); // array(2) { [0] => string(5) "peter" [1] => string(4) "kang" } | |
var_dump($arr3); // array(2) { [0] => string(5) "peter" [1] => string(4) "kang" } | |
var_dump($arr4); // array(1) { [5] => string(5) } | |
var_dump($arr5); // array(2) { [5] => string(5) "peter" ["age"] => int(27) } | |
// -------------------------------------------------------- | |
$arr1 = array('name' => 'peter'); | |
$arr2 = ['name' => 'peter']; | |
$arr3['name'] = 'peter'; | |
var_dump($arr1); // array(1) { ["name"] => string(5) "peter" } | |
var_dump($arr2); // array(1) { ["name"] => string(5) "peter" } | |
var_dump($arr3); // array(1) { ["name"] => string(5) "peter" } | |
// -------------------------------------------------------- | |
$arr = 100; | |
$arr[] = 200; // warning: cannot use a scalar value as an array | |
// -------------------------------------------------------- | |
$arr[] = 1; | |
$arr[10] = 2; | |
$arr[] = 3; | |
$arr['xx'] = 4; | |
$arr[] = 5; | |
$arr[3.14] = 6; | |
$arr[] = 7; | |
//$arr[11] = 300; // test this | |
var_dump($arr); | |
/* | |
array(5) { | |
[0]=>int(1) | |
[10]=>int(2) | |
[11]=>int(3) | |
["xx"]=>int(4) | |
[12]=>int(5) | |
[3]=>int(6) | |
[13]=>int(7) | |
} | |
*/ | |
// -------------------------------------------------------- | |
$arr[true] = true; | |
$arr[false] = false; | |
$arr[NULL] = NULL; | |
var_dump($arr); | |
/* | |
array(3) { | |
[1]=>bool(true) | |
[0]=>bool(false) | |
[""]=>NULL | |
} | |
*/ | |
// -------------------------------------------------------- | |
$arr = [ | |
['name' => 'peter', 'age' => 27], | |
['name' => 'kang', 'age' => 28], | |
['name' => 'alex', 'age' => 29], | |
]; | |
echo '-- 1 --', PHP_EOL; | |
var_dump($arr); | |
// print_r($arr); | |
echo '-- 2 --', PHP_EOL; | |
var_dump($arr[0]['name']); // string(5) "peter" | |
echo '-- 3 --', PHP_EOL; | |
foreach ($arr as $value) { | |
var_dump($value); | |
} | |
echo '-- 4 --', PHP_EOL; | |
foreach ($arr as $key => $value) { | |
var_dump($key, $value); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function func() { | |
echo '100'; | |
} | |
function func($name) { // error: cannot redeclare func() | |
echo $name; | |
} | |
// -------------------------------------------------------- | |
$a = 5; | |
func1($a); | |
echo $a, PHP_EOL; | |
func2($a); | |
echo $a, PHP_EOL; | |
function func1($a) { | |
$a = 100; | |
} | |
function func2(&$a) { // IMPORTANT | |
$a = 200; | |
} | |
// -------------------------------------------------------- | |
$arr = []; | |
func1($arr); | |
echo '--- test1 ---', PHP_EOL; | |
echo var_dump($arr); | |
$arr = []; | |
func2($arr); | |
echo '--- test2 ---', PHP_EOL; | |
echo var_dump($arr); | |
function func1($arr) { | |
$arr[0] = 100; | |
echo '--- func1 ---', PHP_EOL; | |
echo var_dump($arr); | |
} | |
function func2(&$arr) { // IMPORTANT | |
$arr[0] = 100; | |
echo '--- func2 ---', PHP_EOL; | |
echo var_dump($arr); | |
} | |
// -------------------------------------------------------- | |
func(1, 2); // error | |
func(1, 2, 3); | |
func(1, 2, 3, 4); // work | |
function func($a, $b, $c) { | |
echo $a, $b, $c; | |
} | |
// -------------------------------------------------------- | |
func(10, 5); // error: cannot pass parameter 2 by reference | |
function func($a, &$b) { | |
echo $a, $b; | |
} | |
// -------------------------------------------------------- | |
echo var_dump(func()); | |
function func() { | |
echo __FUNCTION__, PHP_EOL; | |
} | |
// -------------------------------------------------------- | |
$a = 100; | |
func(); | |
echo $a, PHP_EOL; // 110 | |
function func() { | |
// echo $a, PHP_EOL; // notice: undefined variable: a | |
$t = $GLOBALS['a']; | |
echo $t, PHP_EOL; // 100 | |
$t += 2; | |
echo $t, PHP_EOL; // 102 | |
$r = &$GLOBALS['a']; // IMPORTANT | |
echo $r, PHP_EOL; // 100 | |
$r += 3; | |
echo $r, PHP_EOL; // 103 | |
global $a, $a2; | |
echo $a, PHP_EOL; // 103 | |
echo $a2, PHP_EOL; // | |
echo var_dump($a2); // NULL | |
$a += 7; | |
echo $a, PHP_EOL; // 110 | |
} | |
// -------------------------------------------------------- | |
$arr = []; | |
func(); | |
echo var_dump($arr); // array(1) { [0] => int(300) } | |
function func() { | |
// echo var_dump($arr); // notice: undefined variable: arr | |
$t = $GLOBALS['arr']; | |
echo var_dump($t); // array(0) {} | |
$t[0] = 100; | |
echo var_dump($t); // array(1) { [0] => int(100) } | |
$r = &$GLOBALS['arr']; // IMPORTANT | |
echo var_dump($r); // array(0) {} | |
$r[0] = 200; | |
echo var_dump($r); // array(1) { [0] => int(200) } | |
global $arr; | |
echo var_dump($arr); // array(1) { [0] => int(200) } | |
$arr[0] = 300; | |
echo var_dump($arr); // array(1) { [0] => int(300) } | |
} | |
// -------------------------------------------------------- | |
func(); | |
echo $a, PHP_EOL; // 100 | |
echo $GLOBALS['a'], PHP_EOL; // 100 | |
function func() { | |
// global $a = 100; // error: syntax error, unexpected '=' | |
global $a; | |
$a = 100; | |
} | |
// -------------------------------------------------------- | |
func(); // 1 | |
func(); // 2 | |
func(); // 3 | |
function func() { | |
static $count = 1; | |
echo $count, PHP_EOL; | |
$count++; | |
} | |
// -------------------------------------------------------- | |
$str = 'func'; | |
$str(100); | |
function func($a, $b = 0) { | |
echo __LINE__, ' ', __FUNCTION__, PHP_EOL; | |
echo $a + $b; | |
} | |
// -------------------------------------------------------- | |
// $func(); // error: uncaught error: function name must be a string | |
$func = function() { | |
echo 'Hello', PHP_EOL; | |
}; // still need ; | |
$func(); | |
echo gettype($func), PHP_EOL; // object | |
echo var_dump($func); // object(Closure)#1 (0) {} | |
// -------------------------------------------------------- | |
$a = 100; | |
$func = function() use ($a) { | |
echo $a, PHP_EOL; | |
}; | |
$func(); // 100 | |
echo gettype($func), PHP_EOL; // object | |
echo var_dump($func); // object(Closure)#1 (1) { | |
// ["static"] => | |
// array(1) { ["a"] => int(100) } | |
// } | |
// -------------------------------------------------------- | |
func(); | |
function func() { | |
$a = 100; | |
// $innerFunc1 = function() { | |
// echo $a; // notice: undefined variable: a | |
// }; | |
// $innerFunc1(); | |
$innerFunc2 = function() use ($a) { | |
echo $a; // work | |
}; | |
$innerFunc2(); | |
} | |
// -------------------------------------------------------- | |
$inner = func(); | |
$inner(); // 100 | |
$inner(); // 101 | |
$inner(); // 102 | |
echo '--- vs. ---', PHP_EOL; | |
func()(); // 100 | |
func()(); // 100 | |
func()(); // 100 | |
function func() { | |
$a = 100; | |
$innerFunc = function() use (&$a) { // IMPORTANT | |
echo $a++, PHP_EOL; | |
}; | |
return $innerFunc; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// TODO | |
=> ref: https://www.bilibili.com/video/BV14x411o7SL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment