这里找到一份适用于 PHP 开发人员的 Python 基础知识,暂时没有找到逆向的,所以记录一下自己做的对比
共有部分
- 变量
- 字符串
- 列表,字典
- 打印
- 函数
- for
- if
- while
- 类
PHP有,Python没有:
Python有,PHP没有:
1 变量
语言 |
定义 |
Python |
a = 1 #不需要美元符号,区分大小写,其他都类似 |
PHP |
\$a = 1; #需要美元符号,区分大小写,且需要分号结尾 |
2 字符串
语言 |
定义 |
求长度 |
找第一个子串 |
切片 |
拆分 |
大小写 |
替换 |
开头/结尾匹配 |
衔接 |
Python |
a = ‘abc’ |
len(a) |
a.index(‘bc’) |
a[1:2] |
a.split(‘b’) |
a.lower() a.upper() |
a.replace(‘b’, ‘d’) |
a.startswith(‘a’) a.endswith(‘b’) |
‘’.join(list1) str1 = str1 + str2 |
PHP |
$a = ‘abc’; |
strlen($a) |
strpos(\$a, ‘bc’) |
无 |
str_split(\$a, 2) 按照数量拆分 |
strtolower(\$a) strtoupper(\$a) |
strtr(\$a, ‘b’, ‘d’) |
无 |
implode(\$list1); \$str1 = \$str1 . \$str2; |
3 列表和字典
Python 中列表和字典的定义是分开的,但是PHP中都是通过array实现
语言 |
定义 |
排序 |
Python |
lista = [1,2,3] dicta = {1:’a’, 2:’b’, 3:’c’} |
lista.sort() sorted(lista) sorted(dicta.items(), key=lambda x: x1) |
PHP |
\$lista = array(1,2,3); \$dicta = array(1=>’a’, 2=>’b’, 3=>’c’); |
sort(\$lista); asort(\$dicta) |
4 打印
语言 |
用法 |
Python |
print(‘abc’, ‘def’) |
PHP |
echo ‘abc’, ‘def’; |
5 函数
语言 |
定义 |
Python |
def foo(para1, para2):\n\s\s\s\spass #Python使用格式区分函数 |
PHP |
function foo(\$para1, \$para2){ echo ‘abc’;} #PHP中用花括号 |
6 for
Python
1 2
| for i in range(20): print(i)
|
PHP
1 2 3
| for($i = 0; $i < 20; $i++){ echo $i; }
|
7 if
Python
1 2 3 4 5
| a = 5 if a > 10 : print(a) else: print('error')
|
PHP
1 2 3 4 5 6 7
| $a = 5; if ($a > 10){ echo $a; } else{ echo 'error'; }
|
8 while
Python
1 2 3 4
| i = 1 while i < 10: print('less than 10') i += 1
|
PHP
1 2 3 4 5
| $x = 1; while($x<=5) { echo "这个数字是:$x <br\>"; $x++; }
|
9 类
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| class Employee: empCount = 0 def __init__(self, name, salary): self.name = name self.salary = salary Employee.empCount += 1 def displayCount(self): print("Total Employee %d" % Employee.empCount) def displayEmployee(self): print("Name : ", self.name, ", Salary: ", self.salary) def func0(self): pass @staticmethod def func1(): pass @classmethod def func2(cls): pass def __del__(self): pass class A(B, C): pass
|
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <? class Person { var $name; var $sex; var $age; function __construct($name, $sex, $age) { $this->name = $name; $this->sex = $sex; $this->age = $age; } function say() { echo "我的名子叫:" . $this->name . " 性别:" . $this->sex . " 我的年龄是:" . $this->age; } function __destruct() { echo "再见" . $this->name; } } ?> class A extends B{ var $a; }
|
PHP有,Python没有
10 switch
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| switch ($x) { case 1: echo "Number 1"; break; case 2: echo "Number 2"; break; case 3: echo "Number 3"; break; default: echo "No number between 1 and 3"; }
|
11 do…while
1 2 3 4 5 6
| $x=6; do { echo "这个数字是:$x <br>"; $x++; } while ($x<=5);
|
12 常量
1 2 3 4 5
| define("GREETING", "Welcome to W3School.com.cn!"); echo GREETING; define("GREETING", "Welcome to W3School.com.cn!", true); echo greeting;
|
13 静态变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| function myTest() { static $x=0; echo $x; $x++; } myTest(); echo "<br>"; myTest(); echo "<br>"; myTest(); echo "<br>"; myTest(); echo "<br>"; myTest(); 0 1 2 3 4
|