B asic
📦 Package Manager
?
composer 下载composer.pharsudo mv composer.phar /usr/local/bin/composer
npm npm install some_garbage
pip
-
-
🔍📦 Search Packages
?
packagist.org
npmjs
pypi
📄 Package Configure File
?
composer.json
package.json
-
Create Configure File
?
Automatic generate when install package
npm init (-y)
-
🔩 Install Package By Configure file
?
php composer.phar install sudo composer install
npm install | npm i
-
🔩 Install Package
?
php composer.phar require geoip2/geoip2:~2.0 or composer require geoip2/geoip2
npm i (uninstall/update) vue-g: Global --save-dev或-D 开发时依赖 --save(默认) 生产环境运行时依赖
(linux)sudo pip install pymysql (win)python -m pip install pymysql
Print Version
java -version
composer -V
node -vnpm -v
pip -V
Upgrade Package Manager
?
composer self-update
npm install npm -g
(linux)sudo pip install -U pip (win)python -m pip install -U pip
块分隔符
{ }
{ }
{ }
用缩进
if关闭fi do关闭done
{%- liquid -%}{{- {%- 有减号代表去掉Whitespace
语句分隔符
;
;
newline 或 ;
newline
newline 或 ;
{%- -%}
单行注释
// comment
// comment
// comment
# comment
# comment
{%- comment -%}注释内容{%- endcomment -%}
多行注释
/* comment line another line */
/* comment line another line */
/* comment line another line */
""" comment line another line """
-
{% comment %} {% endcomment %}
🚢 资源进出口最终的程序一般是很多个代码文件的拼接 import, export, require...
?
include|include_once|require|require_once,__autoload
require 'vendor/autoload.php';use GeoIp2\Database\Reader; $reader = new Reader('vendor\geoip2\geoip2\maxmind-db\GeoIP2-Country.mmdb');
<script type="text/javascript">js code</script >
import math # math.py脚本 math.pifrom math import pi,ceil #可以只导入pi或某些函数 pi #3.141592653589793 ceil (2.4) #3 Python的import并非像php include, 查看wiki
source other.sh
render 'product-grid-item', product: product, grid_item_width: 'medium-up--one-third', per_row: 2
Namespace
?
namespace GeoIp2\Database; ...class Reader implements ProviderInterface
-
?
-
-
使用命名空间
?
require 'vendor/autoload.php';use GeoIp2\Database\Reader; $reader = new Reader('
-
?
V alue & Type
基础数据类型
整型 $i = 1;浮点型 $f = 1.234string $str = '';Boolean $b = FALSE ;null $v = NULL ;Object $bar = new foo ;
number型 var x = 0; // typeof(x)的值是"number" string型 var str = "";Boolean型 var b=false;undefined型 var a = undefined object型 var car = null ; // typeof(car)的值是"object",null值是一个空的对象指针 var obj = {}; // 同= new Object();typeof值object var arr = []; // 同= new Array(); typeof值object var re=/regexp/g; // typeof值Object 同= new RegExp("regexp","模式修饰符g全局模式应用所有字符串,i区分大小写,m多行匹配") var foo = function () {} // typeof值function 引用(reference)值 分配给变量时,空间需要在堆(heap)上分配, 引用值不能像原始(primitive)值 直接分配在栈(stack)上,因为它没有固定的尺寸,所以它将内存地址存在栈上, 作为一个指针
number型 x=0string型 str=""Boolean型 b = True # True/False 空型 NoneObject型 Ferrari458 = Car ('Ferrari',570)
bash中的所有变量都是string ,无论有没有加引号
String assign name = "mark"Number assign my_int = 25assign my_float = 39.756Nil 错误时的空值 Tracking number: {{ fulfillment .tracking_numbers }} >> Tracking number:Array 你无法直接使用Liquid初始化一个数组,但可以用split 字符串的方式构造数组EmptyDrop 如果访问已删除的对象,则返回EmptyDrop对象assign page = pages ["does-not-exist"]assign page2 = pages .this-handle-does-not-exist检查是否empty unless pages.about == empty 除了Nil和false,所有值都为真
数据类型判断
typeof value==="undefined"var theHobbit = new Book ('07122-4', 'The Hobbit', 'J. R. R. Tolkein'); theHobbit instanceof Book; //值为true, 某类的实例
-
🌍 作用域
类/函数的内外变量被类/函数隔开而互为局部 ;可使用超全局变量$GLOBALS或$_SESSION $GLOBALS['isInCYOCategory'] = in_array('7', $categoryIds);在view.phtml中定义,在sharing.phtml也可使用global 关键字可实现在函数内使用外部变量 $country['AU']='Australia';function create_pdf($increment_ids) { global $country; $ct=$country[$row['country_id']]; }
无var 申明, 都是全局的window/self (browser)/self (web worker)/global (node)对象 myglobal="hello"; myglobal; // 值为hello this .myglobal; // 值为hello this ["myglobal"]; // 值为hello window.myglobal; // 值为hello window["myglobal"]; // 值为hello 有var 局部变量,父作用域可以在子作用域里调用-比如函数外的局部变量可在函数内使用值变参分割变量作用域 for (var i=0;i<10;i++) { (function (i) {setTimeout (function( ) {console.log (i);},i*1000); })(i); }let 设置的变量在块级作用域内使用 块级作用域围墙有{} | while (){} | if (){} | try {} catch (e){}| for (let i=0;i<10;i++){setTimeout (function (){console.log (i);},i*1000)} for循环非常适合用let,下一轮循环会根据上轮结果分配一个新的i,每轮循环间属平行作用域,循环体是{}的父作用域,父作用域里的变量可用于子作用域 var a = [];for (let i = 0; i < 10; i++) { a[i] = function () { console.log (i); }; } a[6](); // output: 6
local 变量(默认) 生命周期在函数内nonlocal 变量live in an enclosing scope global 变量在全局范围内生存 有些变量被shell环境和OS环境来存储一些特殊的值,叫环境变量echo $PWD 还有PATH,USER,SHELL,HOME,UID(root为0)等常用环境变量
判断变量是否存在
if (!empty ($v)){}//null 或未定义或''或0 或FALSE 为TRUE if (is_null ($v)){}//null 或未定义为TRUE if (isset ($v)) {}// 非null或已定义为TRUE
if (typeof (var_name)=='undefined') { //不存在 }
? if [ ! -n "$1" ]; then # 变量是否存在
if section .settings.bannerImg != blank
获取资源类型
get_resource_type() typeof (a) == "undefined"? -
-
可变变量
$a='hello'; $$a='world';echo ${$a}; //等同于echo $hello; - - -
const常量是在程序运行时不会被修改的量
define定义的常量可跨脚本跨函数define ("PI", 3.14); // define()在函数里定义的常量在外面依然可使用 const PI='3.1415'; //const定义的必须处于最顶端的作用区域
const PI = 3.1415; // ES6 引入 PI = 3; // TypeError: Assignment to constant variable. const foo;// Uncaught SyntaxError: Missing initializer in const declaration if (true ) { const MAX = 5; } console.log (MAX) // Uncaught ReferenceError: MAX is not defined const的作用域与let相同,只在声明所在的块级作用域内有效 const events = require ('events'); 对象的属性能改变,但对象的结构不能变,const保能保证这个指针是固定的
-
set value
$v = 1;$a=$b=3; var v = 1;var a=b=3建议使用下面的single var模式 a=1;b=c=3 myname=mark 注意等号=两边不能有空格,不然会被当成命令,如果值中间有空格就要加引号如"mark li" echo $myname echo 后面的字符中有空格可以不加引号
assign a = 3
并行set value
list ($y,$z)=array (2,3); //把数组中的值赋给一些变量
var x=1,y=2,z=3ES6 const [a, b, c, d, e] = 'hello'; // 字符串的解构赋值 let {length : len} = 'hello'; // 属性的解构赋值, len=>5 let [, b, c] = [1, 2, 3]; // 数组的解构赋值 let [a,,c]=new Set ([1,2,3]); // Set 结构的解构赋值 let {x,y} = {y:3,x:4} // 对象的解构赋值 let { id, status, data: number } = {id: 42, status: "OK",data: [867, 5309]}; // 嵌套赋值 let [head, ...tail] = [1, 2, 3, 4];// tail=>[2,3,4]
a, b = b, a+b它不等于a=b;b=a+b或b=a+b;a=b
-
🤹♀️ swap
list ($x, $y) = array ($y, $x);
var temp=a a=b b=tempES6 [a,b]=[b,a];
-
-
copy
原始值拷贝的是数据
数组/对象copy
$a = array (1,2,array (3,4)); $a2 =& $a; //浅拷贝 $a4 = $a; //深拷贝
引用值拷贝的是指针地址,所以两个对象指向相同的堆 var a = [1,2,[3,4]];var a2=a; //浅拷贝 var a4=a.slice (0); 或 var a8=a.concat (); //深拷贝
a = [1,2,[3,4]] b=a#地址拷贝 c=a.copy ()#深拷贝
🧊 Freezes an object
var a = [1,2,[3,4]];var a2=a;Object .freeze (a); a2[0]=9; console.log(a2[0]); // output:1 console.log(a[0]); // output:1
S tring
字符串数据
$str = "don't say \"no\"";echo 'don\'t say "no"';
str = "don't say \"no\""; console.log ('don\'t say "no"')
s = "don't say \"no\""print ('don\'t say "no"')
echo "hi,$var\!" echo 'hi,mark!'变量不能用于单引号中,!用不能直接用在双引号中 echo还可以打印彩色文件
assign s = "don't say & quot;no& quot;"assign s = 'don& apos;t say "no"'
变量in字符串
$count = 3; $item = "ball";echo "$count ${item}s\n";
ES6+ const count=3const item = "ball" str = `${count} ${item}s`或 str = String.raw `$(count) ${item}s` `foo ${fn()} bar`函数in 字符串
-
echo "hi,$var\!"
-
格式化输出
echo sprintf ("lorem %s %d %f","ipsum", 13, 3.7);
-
print ('Hi, %s, you have $%d.' % ('Mark', 1000000))
printf "%-5s %-10s %-4.2f\n" 3 Mark 61.563
📐 length
strlen ("lorem")
str.length len (str)
echo ${#varname}
str | size
get字符串中第n个☝ 字符
$s[6]
s[6] s.charAt (6)
s[6] s[-1] # 倒数第一个字符 s[6:-1] # 输出ipsu 还有s[6:]和s[:-1}
-
set字符串中第n个☝ 字符
$s = "bar"; $s[2]="z";echo $s; //输出baz
s.slice(0, index) + 'z' + s.slice(index+1)
-
-
字符串遍历
ES6 for (let char of 'foo') {
-
字符串是否包含/在字符串中的位置
if(strpos ('mark li', " ")===false)右边字符串在左边字符串中首次出现的位置,没有返回false 注意有可能在第一个位置返回0
var st = "do re re"; if (st.indexOf ("re") >= 0 )// lastIndexOf()从未尾开始查找 ES6 "mark".includes ("ar") // output: true url.startsWith ("https://markbuild.com") location.hostname.endsWith ('markbuild.com')
用正则的 re.findall (r're',st)
customer.email contains "shopify.com"
合并字符串
$s2 = $s ."World!"; $s .= "World!";
s += 'World!' s = s.concat ('World')
'hello' + 'world'
assign linkkey = 'navImg' | append : forloop.index | append : '_link' 插在头部 prepend
repeat
-
'oo'.repeat (2) // "oooo" 3 * 'un' # ununun
✂ 字符串定位切割子字符串 (slice,sub string)
substr ("lorem ipsum", 6, 5)第6+1个字符串起,往后切5个
str.slice (6,11)或 str.substr (6,5)官方不建议使用
s[6:11]
str=markliecho ${str:0:4} // 显示mark echo ${str:4} // 显示li
str | slice 0 返回第一个字符 str | slice 1, 3 返2-4字符 product .title | truncatewords: 6, ''
全部大小写
strtoupper ("lorem")strtolower ("LOREM")"lorem".toUpperCase () "LOREM".toLowerCase () s.upper () s.lower ()
product .title | upcase 'UPPERCASE' | downcase
字符串首,单词首大写
ucfirst ("lorem")ucwords ("lorem ipsum")
-
s.capitalize () s.title ()
str | capitalize
去掉两边的空格(默认),或其它字符(非串)
trim (" lorem ")ltrim ("/category/page/5","/")rtrim ("12,4,6,",",") 同 chop ("12,4,6,",",")
" lorem ".trim() .trimLeft () .trimStart () .trimRight () .trimEnd ()
" lorem ".strip()
' too many spaces ' | strip lstrip rstrip
Reverse
str.split ('').reverse ().join ('')
str | split : '' | reverse | join : ''
数字转字符串
"value: ". 8 num2 = num.toString () str (4)
字符串转数字
7 + "12" 73.9 + ".037" Number ("73.9")int ('4')
[Array ] 数组转字符串
$arr = array ("do",re","mi","fa");implode (",", $arr) ["do", "re", "mi", "fa"].join (","); ["do", "re", "mi", "fa"].toString (); // Output: "do,re,mi,fa" lista=["do","re","mi","fa"] s = ','.join (lista) listb=[1,2,3] s=','.join (str (i) for i in b)
product .tags | join : ', '
[Array ] 字符串转数组
explode (",","do,re,mi,fa")\n是指换行符
var arr = "do,re,mi,fa".split (',');// str.split(',',3) 只保留前3段 'm d z'.split (/\s+/) // 支持正则字符
s="do,re,mi,fa" arr=s.split (',') list('123')
OLD_IFS="$IFS" # 备份默认分隔符 IFS="/" # 变量$IFS存储着分隔符 arr=($systems) # 将字符串分割到数组$arr IFS="$OLD_IFS" # 重置默认分隔符 for s in ${arr[@]}do echo "$s"done
str | split : ' '
[Array ] 字符串中每一个字符分割到数组
str_split ("abcd")str.split (''); l=list (s) # 字符串切割显示 echo 'do,re,mi,fa' | cut -d ',' -f1
str | split : ''
非正则替换
echo str_replace ('world', "shenzhen", "hello world");
product .title | remove : "Awesome" 'mark mark li' | remove_first 'mark'product .title | replace : 'Awesome', 'Mega'replace_first
R egular Express
匹配
if (preg_match ('/1999/', $s)) { echo "party!\n"; }
if (s.match (/1999/)) { console.log ("party!"); } "<i>markli".search (/>M/i) //2 ,没有匹配返回-1 /\sli/g.exec ("mark li") // [" li", index: 4, input: "mark li"], 不匹配则返回null "2 3 4 5".split (/\s+/); // ["2", "3", "4", "5"]
import relen (re.findall (r'1999',s2))>0
替换
$s = "do re mi mi mi"; $s2 = preg_replace ('/mi/', "ma", $s);
var s="do re mi mi mi";var s2=s.replace (/mi/g,"ma"); _val.replace (/([^<]+)<\/font>/g,"$1"); image2.replace (/\.([^.]+$)/,function ($0) { return '_360x'+$0 })
s="do re mi mi mi" re.sub (r'mi',r'ma',s)
sed '1,2s/mark/mk/ file # 替换1,2 行的第一个mark sed '1,2s/mark/mk/g file # 替换1,2行每行上的所有mark sed -in-place 's/mark/mk/g file # 替换所有行上的所有mark, 并保存原文件
捕获
$regex = '/^type=(9)&code=([0-9]{6})$/'; $matches = array ();if (preg_match ($regex, $str, $matches)){ echo 'type='.$matches[1]; echo 'code='.$matches[2];
if(/page=([0-9]+)/.test ('&page=52&')){alert (RegExp .$1*1+1);} // 53
A rray/Dict/Set/Map
literal
$arr = array (1, 2, 3, 4); $d = array ("t" => 1, "key" => 0);
var arr=[1, 2, 3, 4]; arr = new Array(10).fill (true)var obj={t:1,key:0}var set1=new Set();var set2 = new Set([1,2,3]);const map1=new Map(); map1.set ('k', 'v') map1.get ('k')const map2=new Map([["k1","v1"],["k2","v2"]]);const map3=new Map([["k1"],["k2"]]); // 这样可作为Set 用 wmap = new WeakMap() // WeakMap的key 必须为引用值,且不能forEach,Map啥类型都行,可以forEach
l=[1, 4] # list t=('东','西','南','北') # tuple 不可修改指向,不变组数 d = {"state": 4, "action": "VO"} # dict s=set(['python','java'])# set 无序不重复 arr=[arr1,arr2] 或 arr=[[1,2,3],['x','y']] # nest
arr=(1 2 3 4 5) declare -A tel tel=([Mark]=6015 [Emy]=0755)
-
📐 length/size
count ($arr)count ($d)
arr.length set1.size map1.size
len (squares) len(tel)
echo ${#arr[*]}
arr | size
Get value by index☝
$arr[0]
arr[0] Array.from(set1)[0] // 先转化成数组
squares[0] arr[1][1]
echo ${arr[0]}
product .tags[0]product .tags | first product .tags | last if product .tags.first == 'sale'
Get value by key🔑
$d["t"]
_obj.t 或 _obj["t"]
tel['jack']
echo ${tel[Mark]}
product .title 或 product ['title']
Set value by index☝
$arr[0] = "lorem";
arr[0]="lorem"; map2.set ("k3","v3"); set1.add (1)
squares[0]="lorem" squares[2:4]=[99,100]
arr[0]="hi" tel[Mark]=6016
Fill 填充
['vscode', 'notepad++', 'xmind', 'postman'].fill ('terminal')
value是否存在于数组/index of array element
if (in_array (2, $a)){} $i = array_search ("y", $arr);
['mark','emy'].indexOf ('mark')>=0; ['mark','emy'].includes ('mark') // ES5+ set1.has (3) map1.has (1)
if a.count (2)>0: print ('exsit') list1=["x","y","z","w"] list1.index ("y")
Get all keys🔑🔑 & Get all values 转成数组
array_keys ($d)array_values ($d)
Object.keys (obj) // Output ['t', 'key'] Object.values (obj) // Output [1, 0] Object.entries (obj) // Output [['t', 1], ['key', 0]]
list (tel.keys ())list (tel.values ())
合并覆盖Object
?
Object.assign(default_opt, _opt)
key🔑是否存在
if (array_key_exists ("key", $d))
Object.keys (obj).includes ('t')
if 'jack' in tel:#还可以使用not in
✂ 数组slice切片选择第3,4个元素
array_slice ($arr, 2, 2)
arr.slice (2,4) squares[2:4]
✂ slice to end 数组切到底选择从第2个到尾部
array_slice ($arr, 1)
arr.slice (1)
squares[1:]
🐯 数组首部操作,追加与删除元素
?
var arr = [6, 7, 8] arr.unshift (5) arr.shift ()
?
🐍 数组尾部操作,追加与删除元素
$arr = array (6, 7, 8);array_push ($arr, 9); $arr[] = 9; # 与array_push相同 array_pop ($arr);删除尾部元素
var arr = [6, 7, 8] arr.push (9) arr.pop ()
list1.append (9) list1.pop ()
Insert an item into an array at a specific index☝
arr.splice (_index, 0, item)
删除元素by index☝
unset ($arr[_index]);//数组将不会重建索引,若要重建索引用array_values($arr)
arr.splice (_index,1)//前面一个参数数是删除的index,后面一参数表示删除后面的几个元素 delete arr[_index];//仍会占据一个长度,值为undefined
del a[0] squares[2:4]=[]#删除2个 squares[:]=[]全部删除或 squares.clear ()
删除元素by key🔑
unset ($d["t"]);
delete _obj.t;
del tel['sape']
删除元素by value
arr.splice(arr.indexOf('v'), 1)
合并
$arr = array (1, 2, 3); $arr2 = array (1, 2, 3);print_r (array_merge ($arr,$arr2));
var arr = [1,2,3], arr2=[4,5,6]; console.log (arr.concat (arr2));
squares+[2,4,'mark']扩展数组 list1.extend (list2) mergedTel=dict(tel,**tel2)
assign things = fruits | concat : vegetables | concat : animals
遍历
foreach ($arr as $s) { echo "$s\n"; }foreach ($arr as $i => $s) { echo "$s at index $i"; }foreach ($d as $k => $v) {echo $k.'=>'.$v;}
for (var key in arr){ console.log (a[key]+' at index '+key); }for (var key in _obj){ _obj[key]}for (let [key,value] of map2) { arr.forEach ((item, index) => {})
for v in squares:
将范围转成数组
$a = range (1, 10);
-
l.list (range (1,10))
数组逆转
$a = array (1, 2, 3); $a = array_reverse ($a);print_r ($a);
var a = [1, 2, 3]; a.reverse (); console.log (a);
l=[1,2,3] l.reverse ()print (l)
arr | reverse
排序
$a = array ("b","A","a","B");sort ($a);print_r ($a);
[15,6,5].sort() // [15, 5, 5] 按字符串排序, 15会排在5前,因为起始字符是1 [15,5,6].sort((a,b)=>a-b) // 数字排序
var arr = [{key:4,v:'mark'},{key:8,v:'emy'},{key:2,v:'mrx'}] arr.sort ((a,b)=>a.key-b.key) // 对象数组数字排序 arr.sort ((a,b)=>(a.v>b.v)?1:-1) // 对象数组字母排序
a=["b","A","a","B"] a.sort ()
assign collection .products | sort : 'price'assign collection .products | sort : 'title'
重复数据删除(去重)
$a = array (1, 2, 2, 3); $a=array_unique ($a);
Array .from (new Set ([1,2,3,3]))
-
arr | uniq
🃏🎲 洗牌和取样♠️♦️♣️♥️
$a = array (1, 2, 3, 4);shuffle ($a);//洗牌 $sa=array_rand ($a, 2);//随机截取
-
-
过滤
arr.filter (item=>item>10) arr.some (item=>item>10) // 返回bool 是否存在至少一个item满足过滤条件, 等同于arr.filter(...).length>0 arr.every (item=>item>10) // 返回bool, 是否所有item都满足过滤条件
collections .products | where : "type", "kitchen"collections .products | where : "available"
按条件查找
[88, 93, 95].find (item=>item>90) // 93 [88, 93, 95].findIndex (item=>item>90) // 1
Reducer
arr.reduce ((total, item)=>total+item) // 累加
Map
collections.map ((item)=> item.title)// ['title1', 'title2', 'title3']
assign collection_titles = collections | map : 'title'// title1title2title3
扁平化Flat
[1,2,[3,4],[[5,6]],[[[7,8]]],[[[[9,10]]]]].flat (2) // [1, 2, 3, 4, 5, 6, Array(2), Array(1)] [1,2,[3,4],[[5,6]],[[[7,8]]],[[[[9,10]]]]].flat (Infinity) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ['May the force', 'be with you'].flatMap (item=>item.split (' ')) // ['May', 'the', 'force', 'be', 'with', 'you']
O bject and Class
定义类
?
class Book { private $isbn; public $title; public $author; public function __construct ($isbn, $title = null, $author = null) { // 构造函数 $this->isbn = $isbn; $this->title = $title; $this->author = $author; } public function display () { echo $this->isbn; } }
var Book = function (isbn, title, author) { if (isbn == undefined) throw new Error('Book constructor requires an isbn.'); this .isbn = isbn; this .title = title || 'No title specified'; this .author = author || 'No author specified'; } Book.prototype .display = function () { ... };//或者 Book.prototype = { checkIsbn : function (isbn) { ... }, display : function () { ... } };ES6 的类是基于原型语法的语法糖 class Book { constructor (isbn, title, author) { if (isbn == undefined) throw new Error ('Book constructor requires an isbn.'); this .isbn = isbn; this .title = title || 'No title specified'; this .author = author || 'No author specified'; } display () { console.log (this .isbn) } }
class Car: def __init__ (self,brandname,horsepower): self.brand = brandname self.power = horsepower def drive (self): print ('Run with %s horsepower' % self.power)也可以这样玩 class Car: pass LamborghiniAventador = Car() LamborghiniAventador.brand = 'Lamborghini' LamborghiniAventador.power = 700
-
-
Creating an Object
?
$theHobbit = new Book ('isbn', 'The Hobbit', 'mark')
var theHobbit = new Book ('isbn', 'The Hobbit', 'mark')
?
method
?
$theHobbit->display ();
theHobbit.display ();
Ferrari458.drive()
Get value by key🔑
?
$theHobbit->isbn; // Cannot access private property Book::$isbn $obj->$param; // 动态 $obj->{$param . 's'}
theHobbit.isbn;
Ferrari458.brand
访问修饰符实现封装,减少耦合
?
public :全开protect :仅同一个类或子类能访问private :仅同一个类能访问 class默认访问修饰符为 public
js
self.__privateattr 要让内部属性不被外部访问,可把属性名称前加__ 但某些版本的解析器中你依然可以用obj._classname__privateattr来访问,也就是Python本身没有任何机制阻止你干坏事,一切靠自觉养成好习惯
继承
?
class ITBook extends Book { public $technology; public function __construct ($isbn, $title, $author, $technology) { parent:: __construct ($isbn, $title, $author); $this->technology = $technology; } }
A对象应用B对象的方法 B.apply (A, arguments); B.call (A, args1,args2);ES6 class ITBook extends Book { constructor (isbn, title, author, technology) { super (isbn, title, author); this .technology = technology; } }
class Dog(Animal):狗继承动物类,获得动物类的全部功能财产
重载(Overloading) & 重写(Overriding)
?
php
-
子类的run()覆盖了父类的run(),在代码运行的时候,总是会调用子类的run() 静态语言需要重载解决调用灵活性,而Python这种灵活的语言可以用其它方式实现
多态同一个接口,使用不同的实例而执行不同操作 实现多态的必要条件继承+重写+父类引用指向子类对象
?
php
js ?
抽象类
?
php js
?
接口生活的接口最具代表性的就是插座,各种三接头插头都能接在三孔插座中,获得电力,每个国家都有各自规定的接口规则。还有打印机接口,显示器接口等。软件中的接口类似于硬件接口,就是一些功能方法的集合 当类实现接口的时,要实现接口中所有方法。否则,类必须声明为抽象类
?
php
js ?
enum通常用来表示如color, type, status等数目有限、形式离散、表达明确的量
?
php js py
F unction
定义函数
?
function add3 ($x1, $x2, $x3) { return $x1 + $x2 + $x3; }
function add3 (x1, x2, x3) { return x1 + x2 + x3; }ES6 function add3 ([x1,x2,x3]) { return x1+x2+x3;}function myfun ({x,y,z}) { return x*y-z;}
def add3(x1,x2,x3): return x1+x2+x3 function add3 (){echo $[$1+$2+$3] }
-
Arrow Function有点像pipeline,箭头前的输出是箭头后的输入
?
-
(param1, param2, …, paramN) => { statements }
(param1, param2, …, paramN) => expression// 等价于: => { return expression; } 如果只有一个参数,圆括号是可选的: (singleParam) => { statements } 或 singleParam => { statements }无参数或者多参数的箭头函数需要使用圆括号或者下划线 () => { statements } 或 _ => { statements }只返回一个对象字面量,没有其他语句时, 应当用圆括号将其包起来, 否则当statements处理而不是当expression处理 params => ({foo: bar})
调用函数
?
add3 (1, 2, 3);
add3 (1, 2, 3)ES6 add3 ([1,2,3]);myfun ({x:3,z:2,y:1});// output: 1
add3 (1, 2, 3)add3 2 3 8 #打印结果13
数组作参数
?
$arr = array (1, 2, 3);function foo ($ar){}echo foo ($arr);
ES6
l=[1,2,3]def foo (arr): for v in arr: print (v)foo (l)
rest参数
?
-
ES6 function add (...values) {for let v of values){ ...add (2,5,7)
函数作参数
?
function add ($x,$y,$f){   return $f($x)+$f($y); }function add (x,y,f ){ return f (x)+f (y) }def add (x, y, f ): return f (x) + f (y)
默认参数
?
function my_log ($x, $base=10) { return log ($x) / log ($base); }my_log (42);my_log (42, M_E);
function my_log (x, base) { base = arguments[1] ? arguments[1]:10; return Math.log (x) / Math.log (base); }my_log (42);my_log (42, Math.E);ES6 支持 function my_log (x, base=10)
import mathdef my_log (x, base=10): return math.log (x) / math.log (base)my_log (42);my_log (42, math.e)
可变数目的参数
?
function first_and_last () { $arg_cnt = func_num_args (); if ($arg_cnt >= 1) { $first = func_get_arg (0); echo "first: " . $first . "\n"; } if ($arg_cnt >= 2) { $arr = func_get_args (); $last = $arr[$arg_cnt-1]; echo "last: " . $last . "\n"; } }function howManyArgs (){ console.log (arguments.length); console.log ("Hello"+arguments[0]+","+arguments[1]); }howManyArgs ("string", 45);Python可变参数
传地址参数
?
function foo (&$x, &$y) { $x += 1; $y .= "ly"; } $n = 7; $s = "hard";foo ($n, $s);
-
-
匿名(Anonymous)函数
?
$sqr = function ($x) { return $x * $x; };echo $sqr(3);
IIFE立即调用函数表达式 (function () { // ... all vars and functions are in this scope only // still maintains access to all globals })();
lambda用于创建匿名函数
闭包(closure)闭包就是能够读取其他函数内部变量的函数
?
function make_counter () { $i = 0; return function () use (&$i) { return ++$i; }; } $nays = make_counter();echo $nays();
var make_counter = function () { var i=0; return function () { return ++i; }; };var nays = make_counter(); //typeof(nays)为funciton console.log (nays()); // output: 1 console.log (nays()); // output: 2
def make_counter (): i=0 def foo (): return i+1 return foo 或 return lambda :i+1 nays = make_counter ()nays ()#输出1
回调模式 Callback
?
call_user_func ('myfun')call_user_func ('myfun','myparam')function call_func (function_param) { function_param (); }function myfunc () { // ... }call_func (myfunc); 例:requestAnimationFramedef callback (fun): fun ()def myfun (): print('hi,mark')callback (myfun)
Op erators
逻辑运算符与或非
&& || !
&& || ! and or xor(逻辑异或,有且只有一个为true则返回true) “与”和“或”有两种不同形式运算符的原因是它们运算的优先级不同
and or not
if [ $env != "dev" ] && [ $env != "test" ];then exit fi
and or
位运算符按位与(对应位全1则为1), 按位或,按位异或(不同为1或0则为1)按位取反,左移,有符号有移,无符号右移
a&b a|b a^b ~a a<<b a>>b a>>>bvar CHUNK_SIZE = 5 << 20 // 5M
-
比较运算符等于,不等于,大于,小于,不小于,不大于
== != > < >= <=<>也可以表示不等于
== != > < >= <====全等(值和类型)
== != > < >= <=
对于数字 -eq -ne -gt -lt -ge -le 对于字符串 = 和 !=
== != > < >= <=
算术运算符加,减,乘,除,余数
+ - * / %/能整型便整型 + - * / %/能整型便整型 + - * / %/至少保留一位小数位 echo $[m+n] + - * / %/整型
product .price | plus : 15 减minus 乘times 除divided_by 余数modulo
整除
(int ) (13 / 5) parseInt (13/5)13//5 floor整除
divmod (整除值,余数)
$d=(int )(10/3); $m=10%3; d=(parseInt )(10/3); m=10%3; divmod (10,3)#输出(3,1)
乘方
pow (2, 4)Math.pow (2,4) 2**4 #输出16 或pow (2, 4)
开方
sqrt (4)Math.sqrt (4) import math math.sqrt (4)
赋值运算符x=y,x=x+y,x=x-y,x=x*y,x=x/y,x="x""y",x=x%y
= += -= *= /= .= %= = += -= *= /= += %= 同Javascipt
浮点转换ceil(天花板)入 floor(地板)舍 round四舍五入
(int )$xround ($x)ceil ($x)floor ($x) parseInt (x) Math.round (x) Math.ceil (x) Math.floor (x)int (x)round (x)import math math.ceil (x) math.floor (x)
4.612 | round : 2 4.6 | ceil 4.6 | floor
最小值最大值
min (1, 2, 3);max (1, 2, 3); $a = array (1, 2, 3);min ($a);max ($a);Math.min (1,2,3); Math.max (1,2,3);不支持数组 min (1, 2, 3);max (1, 2, 3); arr = [1, 2, 3];min (arr);max (arr);
最小值 12 | at_most : 15 最大值 12 | at_least : 15
绝对值
abs ($x)Math.abs (x) abs (x)
-
-17 | abs
随机数
rand (0,99)Math.random () 0.0 ~ 1.0 之间的一个伪随机数 import random random.random ()echo $((RANDOM % 100 +1))
递增递减
++x; x++; --x; x--;
++x; x++; --x; x--;
无,用+=1
$[--i] $[i--] let i++
条件表达式
$x > 0 ? $x : -$x
x=(x > 0 ? x :-x)
-
-
sync and async
//异步1: Callback
getData (a => {
getMoreData (a, b => {
getMoreData (b, c => {
})
})
})
//异步2: Promise
function getData () {
return new Promise((resolve, reject) => {
fs.readFile (fileName, (error, data) => {
if (error) { reject (error) }
else { resolve (data) }
})
})
}
getData ()
.then (a => getMoreData (a))
.then (b => getMoreData (b))
.then (c => {})
.catch (err => {})
//异步3: async/await, async 是Generator 的语法糖
const foo = async function () {
try {
let a = await this .apis.getData ()
let b = await this .apis.getMoreData (a)
} catch (err) {}
}
C ontrol Flow
if
if (条件1 ){ 执行1 } else if (条件2 ){ 执行2 } else { 执行3 }
第一种方式同java 第二种方式将else if替换成elseif, 据说这个更有效率 第三种方式用:代替大括号,不能用else if if (条件1 ): 执行1 elseif (条件2 ): 执行2 else : 执行3 endif ;
if (条件1 ){ 执行1 } else if (条件2 ){ 执行2 } else { 执行3 }
if 条件:elif 条件2:else:
if [ $1 -gt 30 ];then echo 'Not kid any more'elif [ $1 -gt 20 ];then echo 'Become mature'else echo 'you are young'fi 注意[ ]两边都得要有空格 if [ ! -n "$1" ]; then # 变量是否存在 if [ ! -d "web-cms" ]; then # 目录是否存在 if [ ! -f "spm.zip" ]; then # 文件是否存在
{% if user.name == 'mark' %} Hey mark {% elsif user.name == 'ryan' %} Hey ryan {% else %} {% endif %} {% unless name != 'mark' %} Hey mark {% endunless %}
switch
switch (n) {case 0: System.out.println ("no hits"); break ;case 1: System.out.println ("one hit"); break ;default : System.out.println (n+" hits"); }
switch (n) {case 0: echo "no hits"; break ;case 1: echo "one hit"; break ;default : echo n." hits"; }
switch (n) {case 0: console.log ("no hits"); break ;case 1: console.log ("one hit"); break ;default : console.log (n+" hits"); }
为什么Python没有switch? 少量用if elif 大量用字典 func = functions[value]func ()
case $1 in 男|M)echo "小少爷" ;; 女|F)echo "小公主" ;; *)echo "有没有搞错" ;;esac
-
whilea period of time.
while ( i < 100 ) { i++; }
while ( i < 100 ) { i++; }
while ( i < 100 ) { i++; }
while i < 100 : i+=1;
while [ $i -lt 10 ]; do let i=i+1done while true; do # 无限循环用Exit跳出
do while和while循环十分类似, 不过至少执行一次代码
do { sum=sum+i; i++; }while (i<=100);
do { sum=sum+i; i++; }while (i<=100);
do { sum=sum+i; i++; }while (i<=100);
无,用break跳出
for循环
for (int i=1;i<=5;i++) { sum=sum+i; }增强 for 循环 int [] arr = {1, 2, 3, 4};for (int x : arr){ System.out.println ( x ); }
for (i=1;i<=5;i++) { sum=sum+i; }
1 for (i=1;i<=5;i++) { sum=sum+i; } 2 for (var key in obj){obj[key]}
for i in range (1,6): # range为遍历辅助 sum+=ipython中只有for in(相当于foreach)遍历
for i in `seq 1 3`;do echo $i;done for i in 1 2 3;do echo $i;done
{% for block in section.blocks %} {{ forloop .index }} {% endfor %}
break, continue跳出最近一层的for,for-in,while,do-while循环, 或跳出switch continue只结束本次循环,直接进入下一轮 break则是结束整个循环
break , continue
break , continue
break , continue
break , continue
break , continue
-
goto
-
if(1) goto end; ... end: ...不能跨函数使用.This is not a full unrestricted goto,The target label must be within the same file and context
-
-
-
exit
System.exit (0);
exit (); // 别名die, 输出一个消息并且退出当前脚本
-
os._exit () # 终止程序 sys.exit () # 会引发一个异常,如果异常没被捕获,python解析器将会退出,如果被捕获,还可以执行后面的代码,用于做清理工作 一般情况下使用sys.exit ()即可,一般在fork出来的子进程中使用os._exit ()
exit
异常的抛出与捕获无论异常是否发生,在程序结束前,finally中的语句都会被执行
try { }catch (异常类型1 异常的变量名1){ }catch (异常类型2 异常的变量名2){ }finally { }
//主动抛出异常 function checkNum ($number){ if ($number>1){ throw new Exception("不能使用大于1的值"); } return true; }try { checkNum (2); } catch (Exception $e){ echo '错误描述: ' .$e->getMessage (); } finally { echo "always excute"; }
//被动抛出异常 try { adddlert ("Welcome!"); } catch (err){ alert (err);//ReferenceError: adddlert is not defined }//主动抛出异常 try { throw "抛出错误" } catch (err){ alert (err);//抛出错误 }finally {console.log ("always excute")}
# 被动抛出 import systry : f = open ('myfile.txt') s = f.readline () i = int (s.strip ())except OSError as err: print ("OS error: {0}".format (err))except ValueError: print ("Could not convert data to an integer.")except : print ("Unexpected error:", sys.exc_info ()[0])finally : print ("always excute")# 主动抛出 try : raise ValueErrorexcept : print ("value error")
npm run build || (echo '编译失败';exit 1) npm run buildif [ $? -ne 0 ];then echo '编译失败'exit 1else echo '编译成功'fi
T ime
Get Time
import java.util.Date;import java.text.SimpleDateFormat; Date dNow = new Date (); SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss"); System.out.println (ft.format(dNow)); // output: 2020-02-12 12:30:36 System.out.println (dNow.getTime ()); // output: 1620801449261
echo date ("Y-m-d H:i:s");//Output: 2018-05-03 01:06:54 //Other format date (m);date (H);echo time ();//Output: 1525309469
var d=new Date(); console.log (d.toLocaleString ())//Output: 5/2/2018, 2:18:36 PM Date.parse (new Date())/1000//Output: 1525242393 parseInt (new Date().getTime ()/1000) //Output: 1525242680parseInt ((new Date()).valueOf ()/1000)//Output 1525243045
import time;print (time.strftime ("%Y-%m-%d %H:%M:%S", time.localtime ()))#Output: 2018-05-03 10:17:01 print (int (time.time ()))#Output: 1525310230
echo `date`#Output: Wed May 2 13:54:46 2018 echo "[`date +%Y-%m-%d\|%T\|%A`]"#Output: [2018-05-02|14:14:20|Wednesday] echo `date +%s`#Output: 1525242150
-
Datetime to Timestamp
import java.text.SimpleDateFormat;import java.text.ParsePosition;long time = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse ("2021-05-12 02:37:29", new ParsePosition(0)).getTime (); System.out.println (time); // output: 1620758249000
echo strtotime ("2018-10-06 02:00:00");//Output: 1538791200
Date.parse (new Date("2018-10-06 02:00:00"))/1000
import datetimeimport timeprint (time.mktime (datetime.datetime.strptime ("2021-05-12 02:37:29", "%Y-%m-%d %H:%M:%S").timetuple ()))// output: 1620758249.0
-
Timestamp to Datetime
double ts = 1620801449261d; System.out.println (new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format (ts)); // output: 2021-05-12 02:37:29 long ts = 1620801449; System.out.println (new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format (new Date(ts * 1000))); // output: 2021-05-12 02:37:29
echo date ("Y-m-d H:i:s",'1538791200000'/1000); // output:2018-10-06 02:00:00
function formatdate (_timestamp) {var date = new Date (+_timestamp), y = date.getFullYear (), m = date.getMonth () + 1, d = date.getDate (), h = date.getHours (), i = date.getMinutes (), s = date.getSeconds (); m = m > 9? m : "0"+m; d = d > 9? d: "0"+d; h = h > 9? h: "0"+h; i = i > 9? i: "0"+i; s = s > 9? s: "0"+s;return y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s; }formatdate (1538791200*1000)
from datetime import datetimeprint (datetime.fromtimestamp (1620758249))// output: 2021-05-12 02:37:29
-
CL I
命令行
javac HelloWorld.java // 编译 java HelloWorld // 在JVM 中运行
php -f index.phpphp -r "echo 'mark';"
-
python cmd.py ip 116.6.65.67
Method 1: bash backupdb.sh arg1 arg2 Method 2: ./backupdb.sh #need shebang and X permission
-
参数
public static void main (String[] args) { System.out.printf ("第一个参数: %s%n", args[0]); }
$argv 数组 $argv[0]是脚本名 $argv[1]是第一个参数
-
import sys#需要模块:sys sys.argv[0]#脚本名:cmd.py sys.argv[1]#参数1:ip
脚本中$1的值便是arg1,$2便是arg2
输入
import java.util.Scanner System.out.print ("M>>"); // println会换行 Scanner s = new Scanner (System.in); System.out.println (s.nextLine ());
fwrite (STDOUT, "M>>");echo fgets (STDIN);
-
command = input ("M>>")print (command)
read -p "M>> " taskidecho $taskid
D isk I/O
判断目录/文件是否存在
import java.io.File; if (new File("dirNameOrFileName").exists ()) { System.out.println ("文件或目录存在"); }
if (file_exists ('dirNameOrFileName')) { echo "文件或目录存在"; }
-
import osif not os.path.exists (aim_dir+'\\'+name):# 如果文件/目录不存在 os.path.isdir (ori_dir+'\\'+name):# 是否是目录 os.path.isfile (ori_dir+'\\'+name):# 是否是文件
if [ ! -d "web-cms" ]; then # 目录是否存在 if [ ! -f "spm.zip" ]; then # 文件是否存在
-
Create directory/file, write file
File file=new File("dirName");if (!file.exists ()) { file.mkdir (); }
import java.io.File;import java.io.FileWriter;import java.io.IOException; File file = new File("f.txt");try {
if (!file.exists ()) {
file.createNewFile ();
}
FileWriter fw = new FileWriter(file);
fw.write ("content");
fw.flush ();
fw.close ();
} catch (IOException e) {
e.printStackTrace ();
}
mkdir ("/path/to/my/dir", 0700); $f = fopen ("f.txt", "w");fwrite ($f, 'content');fclose ($f);
-
import os os.makedirs (aim_dir) f=open ('f.txt','w') f.write ('content')
mkdir wwwtouch fileecho 'content' >> file
N etwork I/O
HTTP Data
?
$_POST $_GET $_COOKIE $_REQUEST(包含$_POST+$_GET+$_COOKIE 数据的数组) $_SERVER['HTTP_IF_NONE_MATCH'] 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] 判断协议,别忘记考虑负载均衡
window.location, document.cookieconst urlParams = new URLSearchParams (location.hash.replace (/^.*\?/, '')) urlParams.get ('param')
?
-
-
HTTP Server
?
header ()setcookie ()
-
python3 -m http.server [port] // 适合HTML页面 Simple Server Demo
HTTP Client
?
cURL
jquery.ajax Fetch fetch (url).then (res => { return res.json () }).then (myjson => {}).catch (err=>{})
-
curl wget
E ncryption
base64
window.btoa ("test"); //"dGVzdA==" window.atob ("dGVzdA=="); //"test"
echo test|base64 // dGVzdAo= echo dGVzdAo=|base64 -d // test
str | base64_encode str | base64_decode
Hash: md5, sha1, sha256, sha512
md5 ("value")
md5 calculate(.trans.) var hash = md5 ("value");
import hashlib hashlib.md5 (bytes ('value',encoding='utf-8')).hexdigest ()import os hashlib.sha1 (open ('C:\\Users\\m\\Desktop\\file.exe','rb').read ()).hexdigest () hashlib.sha256 (... hashlib.sha512 (...
echo -n "value"|md5sum echo -n "value"|openssl md5 cat file|md5sum cat file|openssl sha1 cat file|openssl sha256 cat file|openssl sha512
str | md5 sha1 sha256 hmac_sha1 hmac_sha256
对称加密: aes
$iv=''; $str = openssl_decrypt (hex2bin ($key), "aes-128-ecb", $aeskey, OPENSSL_RAW_DATA, $iv);openssl_encrypt
?
?
-
非对称加密: RSA
ssh-keygen -t rsa -f ~/.ssh/markli_rsa -N ""read -p "输入目标IP>> " ipscp ~/.ssh/markli_rsa.pub root@$ip:~/markli_rsa.pubssh root@$ip "cat ~/markli_rsa.pub >> ~/.ssh/authorized_keys"ssh root@$ip "rm ~/markli_rsa.pub"