Home Backend Development PHP Tutorial What is the difference between php and Python? Simple comparison between Python and php

What is the difference between php and Python? Simple comparison between Python and php

Aug 25, 2018 pm 03:16 PM
php python

Which one is better, php or Python? What are the differences between php and Python? Many friends will ask such a question, so today the php Chinese website will tell you about the difference between php and Python. I believe you will make some judgments after reading this article.

First of all, let’s take a look at the difference between php and Python applications:

php is a general open source scripting language. The syntax absorbs the characteristics of C language, Java and Perl, which is easy to learn and widely used. It is mainly suitable for the field of Web development.

Python is mainly used for system operation and maintenance, monitoring, crawlers, queue tasks, etc. Therefore, Python is easier to use outside the field of web development.

Secondly, let’s take a look at the difference between php and Python syntax:

Note: python is a strongly typed language, and php is a weakly typed language

1. The difference between the output syntax of php and Python:

php: echo can output multiple variables; print () only outputs one variable, and the return value is 1 on success and 0 on failure; print_r() only outputs an array; die() outputs first, then shuts down; printf() Formatted output string; var_dump() can determine the type and length of a variable and output the number of variables.

Python: print defaults to line wrapping. If not, add a comma.

2. The difference between php and Python variables:

php starts with the "$" identifier; for example: $a = 0 is defined.

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

3. The difference between PHP and Python arrays:

PHP arrays are relatively simple and unified, that is, array; they include ordinary arrays and associative arrays.

1

2

3

4

5

6

7

8

// 定义

$arr = array('Michael', 'Bob', 'Tracy');

// 调用方式

echo $arr[0]

// Michael

//  数组追加

array_push($arr, "Adam");

// array('Michael', 'Bob', 'Tracy','Adam');

Copy after login

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

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

1

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

Copy after login

tuple (tuple) : Tuples bring together various objects and cannot be modified. Search by index, use brackets "()";

1

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

Copy after login

dict (dictionary) : A dictionary is a The combination of group key (key) and value (value), search by key (key), no order, use curly brackets "{}";

1

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

Copy after login

set (set): Unordered, elements only appear once, automatic deduplication, use "set([])"

1

test_set = set(['Wang', 'Hu', 'Liu', 4, 'Wang'])

Copy after login

4. The difference between conditional judgment statements and loop statements between PHP and Python:

The difference between conditional judgment statements between php and Python:

Example: The judgment statement of php is:

1

2

3

4

5

6

7

if($age = '5'){

    echo "小孩";

}else if($age < 25 and $age > 14){

    echo "青少年";

}else{

    echo "空";

}

Copy after login

The judgment statement of Python is:

1

2

3

4

5

6

7

sex = &#39;&#39;

if sex == &#39;man&#39;:

    print(&#39;男&#39;)

elif sex == &#39;women&#39;:

    print(&#39;女&#39;)

else:

    print(&#39;空&#39;)

Copy after login

The difference between loop statements between php and Python:

Example: The loop statement of php is:

1

2

3

4

5

$arr = array(&#39;a&#39; => &#39;苹果&#39;, &#39;b&#39; =>&#39;香蕉&#39;, &#39;c&#39; => &#39;梨子&#39;, &#39;d&#39; => &#39;水果&#39;);

foreach ($arr as $key => $value){

    echo "数组key:".$key."<br>";   

    echo "key对应的value:".$value."<br>";

}

Copy after login

The loop statement of Python is:

1

2

3

4

5

6

7

8

9

arr = {&#39;a&#39;: &#39;苹果&#39;, &#39;b&#39;: &#39;香蕉&#39;, &#39;c&#39;: &#39;梨子&#39;, &#39;d&#39;: &#39;水果&#39;}

# 第一种

for (key,value) in arr.items():

    print("这是key:" + key)

    print("这是key的value:" + value)

# 第二种

for key in arr:

    print("这是key:" + key)

    print("这是key的value:" + arr[key])

Copy after login

5. The difference between php and Python function syntax:

php function syntax:

1

2

3

4

5

function calc($number1, $number2 = 10)

{

   return $number1 + $number2;

}

print(calc(7));

Copy after login

Python function syntax:

1

2

3

4

def calc(number1, number2 = 10):

    sum = number1 + number2

        return sum   

print(calc(7))

Copy after login

Finally we come Take a look at the performance comparison between php and Python:

php test:

1

2

3

4

5

6

<?php

function fibonacci($i){

    if($i<2) return $i;

    return fibonacci($i-2) + fibonacci($i-1);

}

echo  fibonacci(34);

Copy after login

php5.6 test is as follows:

1

2

3

4

5

6

7

8

qiangjian@localhost:/works/learnCPP$ php -v && time php  fib.php

PHP 5.6.28 (cli) (built: Dec  6 2016 12:38:54)

Copyright (c) 1997-2016 The PHP Group

Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

5702887

real    0m2.307s

user    0m2.278s

sys     0m0.017s

Copy after login

php7 test is as follows:

1

2

3

4

5

6

7

8

qiangjian@localhost:/works/learnCPP$ php -v && time php  fib.php

PHP 7.1.2 (cli) (built: Feb 17 2017 10:52:17) ( NTS )

Copyright (c) 1997-2017 The PHP Group

Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

5702887

real    0m0.815s

user    0m0.780s

sys     0m0.015s

Copy after login

Python test:

1

2

3

4

5

6

def fibonacci(i):

    if i<2:

        return i

    return fibonacci(i-2) + fibonacci(i-1)

  

print(fibonacci(34))

Copy after login

python2.7 test is as follows:

1

2

3

4

5

6

qiangjian@localhost:/works/learnCPP$ python2 -V && time python2 ./fib.py

Python 2.7.13

5702887

real 0m2.651s

user 0m2.594s

sys 0m0.027s

Copy after login

python3.5 test is as follows:

1

2

3

4

5

6

qiangjian@localhost:/works/learnCPP$ python3 -V && time python3 ./fib.py

Python 3.5.1

5702887

real    0m3.110s

user    0m2.982s

sys     0m0.026s

Copy after login

It can be seen from the above test that python becomes slower as it is upgraded, and many syntaxes are incompatible. It is good to write algorithms and small programs in daily life; the speed of php7 and 5 in php is very different, and it feels not It's like a program, and it's making great progress.

Of course, all the above content is just a simple comparison between php and python. There are many things that have not been written out, but it can be seen that php and python each have their own advantages and disadvantages. The above is the entire content of this article. Thank you for paying attention to php Chinese website!

Related recommendations:

Should hackers learn PHP or python? Rational analysis of ten differences between PHP and python

PHP programmers use crawler technology to reveal the real data behind the increase in rent

The above is the detailed content of What is the difference between php and Python? Simple comparison between Python and php. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP: An Introduction to the Server-Side Scripting Language PHP: An Introduction to the Server-Side Scripting Language Apr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

PHP and the Web: Exploring its Long-Term Impact PHP and the Web: Exploring its Long-Term Impact Apr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

See all articles