What is the difference between PHP and python? A simple comparison between Python and PHP

Source: Internet
Author: User
Tags zend
which is good for PHP and python? What is the difference between PHP and python? A lot of friends will come to ask such a question, then today's PHP Chinese network for everyone to say about the difference between PHP and Python,I believe you read this article should have some judgment.

First, let's take a look at the differences between PHP and Python applications:

PHP is a common open source scripting language. The grammar absorbs the C language, Java and Perl features, is conducive to learning, widely used, mainly for the field of web development.

Python is mainly to do system operations, monitoring, crawler, queue tasks and so on, so, out of the web development field Python more useful.

Second, let's take a look at the differences between PHP and Python syntax:

Note: Python is a strongly typed language, PHP is a weakly typed language

1. The difference between PHP and Python's output syntax:

php: echo can output multiple variables, print () output only one variable, and the successful return value is 1, the failure is 0;print_r () output array, die () first output, after shutdown; printf () format the output string Var_dump () can determine the type and length of a variable and output the number of variables.

Python: Print default line wrapping, no wrapping to add commas.

2. The difference between PHP and Python variables:

PHP starts with a "$" identifier, for example: $a = 0.

Python is defined directly; for example: A=0.

3. The difference between PHP and Python arrays:

The PHP array is relatively simple and uniform, that is, an array, and includes both normal and associative arrays.

Define $arr = Array (' Michael ', ' Bob ', ' Tracy ');//Call echo $arr [0]//michael//  array append array_push ($arr, "Adam");//Array (' Michael ', ' Bob ', ' Tracy ', ' Adam ');

Python arrays can be divided into lists [], tuples (), dictionaries {}, Collections ([]), formats changeable, and the usage is more flexible.

list (linked list): Ordered items, searched by index, using square brackets "[]";

Test_list = [1, 2, 3, 4, ' Oh ']

tuple (tuple): Tuples combine various objects together, cannot be modified, are searched by index, use parentheses "()";

Test_tuple = (1, 2, ' Hello ', (4, 5))

dict (dictionary): A dictionary is a set of keys (key) and a combination of values (value) that are searched by key (key), without order, using curly braces "{}";

Test_dict = {' Wang ': 1, ' Hu ': 2, ' Liu ': 4}

Set (SET): unordered, Element appears only once, automatic de-weight, use "set ([])"

Test_set = Set ([' Wang ', ' Hu ', ' Liu ', 4, ' Wang '])

4. The difference between a conditional judgment statement and a circular statement between PHP and Python:

The difference between a conditional judgment statement between PHP and Python:

Example: PHP's judgment statement is:

if ($age = ' 5 ') {    echo "child";} else if ($age < $age >) {    echo "teen";} else{    echo "Empty";}

The Python judgment statement is:

sex = ' if sex = = ' man ':    print (' male ') elif sex = = ' Women ':    print (' female ') Else:    print (' empty ')

The difference between PHP and Python for looping statements:

Example: PHP's looping statement is:

$arr = Array (' a ' = = ' Apple ', ' b ' = ' banana ', ' c ' = ' pear ', ' d ' = ' fruit '); foreach ($arr as $key = = $value) {    echo "Array key:" $key. " <br> ";        echo "key corresponds to Value:". $value. " <br> ";}

The Python loop statement is:

arr = {' A ': ' Apple ', ' B ': ' Bananas ', ' C ': ' Pears ', ' d ': ' Fruit '}# first type for (Key,value) in Arr.items ():    print ("This is key:" + key)    print ("This is the value of key:" + value) # The second type for key in arr:    print ("This is key:" + key)    print ("This is the value of key:" + Arr[key])

5. The difference between PHP and Python function syntax:

PHP function Syntax:

Function Calc ($number 1, $number 2 = ten) {   return $number 1 + $number 2;} Print (Calc (7));

Python function Syntax:

Def calc (number1, number2 = ten):    sum = number1 + number2        return sum    print (Calc (7))

Finally, let's take a look at the comparison between PHP and Python performance:

Test for PHP:

<?phpfunction Fibonacci ($i) {    if ($i <2) return $i;    Return Fibonacci ($i-2) + Fibonacci ($i-1);} Echo  Fibonacci (34);

The php5.6 test is as follows:

qiangjian@localhost:/works/learncpp$ php-v && time php  fib.php php 5.6.28 (CLI) (Built:dec  6 2016 12:38: Copyright (c) 1997-2016 the PHP groupzend Engine v2.6.0, Copyright (c) 1998-2016 Zend technologies5702887real 0m2    . 307suser    0m2.278ssys     0m0.017s

The PHP7 test is as follows :

qiangjian@localhost:/works/learncpp$ php-v && time php  fib.phpphp 7.1.2 (CLI) (Built:feb 17 2017 10:52:17) ( NTS) Copyright (c) 1997-2017 the PHP groupzend Engine v3.1.0, Copyright (c) 1998-2017 Zend technologies5702887real    0 M0.815suser    0m0.780ssys     0m0.015s

Test for Python:

def Fibonacci (i):    if i<2:        return I    return Fibonacci (i-2) + Fibonacci (i-1) print (Fibonacci (34))

The python2.7 test is as follows:

qiangjian@localhost:/works/learncpp$ python2-v && time python2./fib.py Python 2.7.135702887real 0m2.651suser 0m2.594ssys 0m0.027s

The python3.5 test is as follows:

qiangjian@localhost:/works/learncpp$ python3-v && time python3./fib.py Python 3.5.15702887real    0m3.110suser    0m2.982ssys     0m0.026s

From the above test can be seen that the more the Python upgrade slower, and a lot of syntax is not compatible, usually write algorithms and small programs can be very good, PHP php7 and 5 of the speed difference is very large, it does not feel like a program, and improved greatly.

Of course, all of the above is just a simple comparison between PHP and Python, and a lot of it is not written out, but you can see that PHP and Python each have their own advantages and disadvantages. The above is the whole content of this article, thank you for your attention to the PHP Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.