เปรียบเทียบ ภาษา php และ python
เปรียบเทียบ ภาษา php และ python
ข้อมูลทั่วไปของทั้ง2ภาษา
พีเอชพี (PHP) PHP Hypertext Preprocessor เป็นภาษาคอมพิวเตอร์ในลักษณะเซิร์ฟเวอร์-ไซด์ สคริปต์ เริ่มสร้างเมื่อปี 1995 โดย Rasmus Lerdorf
Python ไพทอนเป็นภาษาสคริปต์ ทำให้ใช้เวลาในการเขียนและคอมไพล์ไม่มาก ภาษาPython นั้นสร้างโดย Guido van Rossum และถูกเผยแพร่ครั้งแรกในปี 1991 Python นั้นเป็นภาษาแบบ interprete ที่ถูกออกแบบโดยมีปรัญชาที่จะทำให้โค้ดอ่านได้ง่ายขึ้น การเรียกใช้งานตัวแปรพื้นฐานของทั้ง 2 ภาษา
1.Primitive Data Type
1.1 php
1.2 python
วิธีการเรียกตัวแปร และการแสดงผลลัพธ์
2.Reference Data Type
2.1 php
2.2 python
ข้อแตกต่างของทั้ง2ภาษาคือ ในการเรียกใช้arrayนั้น Python มี method ในการจัดการกับ array ทำให้ใช้งานง่ายและสะดวก ตัวอย่าง method ดังรูปที่แนบ ส่วน Reserved word ของทั้งสองภาษานั้น php มีจำนวนมากกว่า python ดังตาราง
![]() |
| method การใช้งานกับ array |
|
PHP
|
||||
|
__halt_compiler()
|
abstract
|
and
|
array()
|
as
|
|
break
|
callable (as
of PHP 5.4)
|
case
|
catch
|
class
|
|
clone
|
const
|
continue
|
declare
|
default
|
|
die()
|
do
|
echo
|
else
|
elseif
|
|
empty()
|
enddeclare
|
endfor
|
endforeach
|
endif
|
|
endswitch
|
endwhile
|
eval()
|
exit()
|
extends
|
|
final
|
finally (as
of PHP 5.5)
|
for
|
foreach
|
function
|
|
global
|
goto (as
of PHP 5.3)
|
if
|
implements
|
include
|
|
include_once
|
instanceof
|
insteadof (as
of PHP 5.4)
|
interface
|
isset()
|
|
list()
|
namespace (as
of PHP 5.3)
|
new
|
or
|
print
|
|
private
|
protected
|
public
|
require
|
require_once
|
|
return
|
static
|
switch
|
throw
|
trait (as
of PHP 5.4)
|
|
try
|
unset()
|
use
|
var
|
while
|
|
xor
|
yield (as
of PHP 5.5)
|
|
|
|
|
Python
|
||||
|
FALSE
|
class
|
finally
|
is
|
return
|
|
None
|
continue
|
for
|
lambda
|
try
|
|
TRUE
|
def
|
from
|
nonlocal
|
while
|
|
and
|
del
|
global
|
not
|
with
|
|
as
|
elif
|
if
|
or
|
yield
|
|
assert
|
else
|
import
|
pass
|
|
|
break
|
except
|
in
|
raise
|
|
ตารางเปรียบเทียบ Reserved word
operators ของทั้ง 2 ภาษา
1.1 Arithmetic Operators
|
PHP Arithmetic Operators
|
|||
|
Operator
|
Name
|
Example
|
Result
|
|
+
|
Addition
|
$x + $y
|
Sum of $x and $y
|
|
-
|
Subtraction
|
$x - $y
|
Difference of $x and $y
|
|
*
|
Multiplication
|
$x * $y
|
Product of $x and $y
|
|
/
|
Division
|
$x / $y
|
Quotient of $x and $y
|
|
%
|
Modulus
|
$x % $y
|
Remainder of $x divided by $y
|
|
**
|
Exponentiation
|
$x ** $y
|
Result of raising $x to the $y'th power (Introduced in PHP 5.6)
|
|
Python Arithmetic Operators
|
|||
|
Operator
|
Name
|
Example
|
|
|
+
|
Addition
|
x + y
|
|
|
-
|
Subtraction
|
x - y
|
|
|
*
|
Multiplication
|
x * y
|
|
|
/
|
Division
|
x / y
|
|
|
%
|
Modulus
|
x % y
|
|
|
**
|
Exponentiation
|
x ** y
|
|
|
//
|
Floor division
|
x // y
|
|
1.2 Assignment Operators
|
PHP Assignment Operators
|
||
|
Assignment
|
Same as...
|
Description
|
|
x = y
|
x = y
|
The left operand gets set to the value of the expression on the
right
|
|
x += y
|
x = x + y
|
Addition
|
|
x -= y
|
x = x - y
|
Subtraction
|
|
x *= y
|
x = x * y
|
Multiplication
|
|
x /= y
|
x = x / y
|
Division
|
|
x %= y
|
x = x % y
|
Modulus
|
|
Python Assignment Operators
|
||
|
Assignment
|
Same as...
|
Description
|
|
=
|
x = 5
|
x = 5
|
|
+=
|
x += 3
|
x = x + 3
|
|
-=
|
x -= 3
|
x = x - 3
|
|
*=
|
x *= 3
|
x = x * 3
|
|
/=
|
x /= 3
|
x = x / 3
|
|
%=
|
x %= 3
|
x = x % 3
|
|
//=
|
x //= 3
|
x = x // 3
|
|
**=
|
x **= 3
|
x = x ** 3
|
|
&=
|
x &= 3
|
x = x & 3
|
|
|=
|
x |= 3
|
x = x | 3
|
|
^=
|
x ^= 3
|
x = x ^ 3
|
|
>>=
|
x >>= 3
|
x = x >> 3
|
|
<<=
|
x <<= 3
|
x = x << 3
|
1.3 Comparison Operators
|
PHP Comparison Operators
|
|||
|
Operator
|
Name
|
Example
|
Result
|
|
==
|
Equal
|
$x == $y
|
Returns true if $x is
equal to $y
|
|
===
|
Identical
|
$x === $y
|
Returns true if $x is
equal to $y, and they are of the same type
|
|
!=
|
Not equal
|
$x != $y
|
Returns true if $x is
not equal to $y
|
|
<>
|
Not equal
|
$x <> $y
|
Returns true if $x is
not equal to $y
|
|
!==
|
Not identical
|
$x !== $y
|
Returns true if $x is
not equal to $y, or they are not of the same type
|
|
>
|
Greater than
|
$x > $y
|
Returns true if $x is
greater than $y
|
|
<
|
Less than
|
$x < $y
|
Returns true if $x is
less than $y
|
|
>=
|
Greater than or equal
to
|
$x >= $y
|
Returns true if $x is
greater than or equal to $y
|
|
<=
|
Less than or equal to
|
$x <= $y
|
Returns true if $x is
less than or equal to $y
|
|
Python Comparison Operators
|
|||
|
Operator
|
Name
|
Example
|
|
|
==
|
Equal
|
x == y
|
|
|
!=
|
Not equal
|
x != y
|
|
|
>
|
Greater than
|
x > y
|
|
|
<
|
Less than
|
x < y
|
|
|
>=
|
Greater than or equal
to
|
x >= y
|
|
|
<=
|
Less than or equal to
|
x <= y
|
|
เปรียบเที่ยบ
Object-oriented programming ของ php และ Python
1. การสร้าง class และ constructor
![]() |
| ภาษา PHP |
![]() |
| ภาษา python |
1.2 Access modifiers
ทั้ง2ภาษา สามารถ Access modifiers ทั้งตัวแปรและคลาสได้ แต่ในภาษา php มี Access modifiers ที่หลากหลายกว่า ดังตาราง Access modifiers ของภาษา php
| access modifier | classes | functions | variable |
| public | Not Applicable | Applicable | Applicable |
| private | Not Applicable | Applicable | Applicable |
| protected | Not Applicable | Applicable | Applicable |
| abstract | Applicable | Applicable | Not Applicable |
| final | Applicable | Applicable | Not Applicable |
1.3 Binding
การผูกค่า (binding) มี 2 แบบคือ
ในภาษาphpนั้นจะเป็น Static Binding ส่วนใน ภาษา python จะเป็น Dynamic Binding
- Static Binding: ผูกค่าตอน compile
- Dynamic Binding: ผูกค่าตอน runtime
![]() |
| ภาษา php |
![]() |
| ภาษา python |
1.4 Polymorphism
ภาษาpython นั้นสามารถทำ Polymorphism ได้ ส่วนภาษา php นั้นไม่สามารถทำได้
1.5 Overloading
![]() |
| ภาษา python |
ภาษาpython นั้นสามารถทำOverloadingได้ ส่วนภาษา php นั้นไม่สามารถทำได้
1.6 Overriding
![]() |
| ภาษา php |
![]() |
| ภาษา python |
ทั้ง 2 ภาษา สามารถทำOverridingได้เหมือนกัน
นายพรรษชนม์ กุตัน

















ความคิดเห็น
แสดงความคิดเห็น