search

Home  >  Q&A  >  body text

Is there a way to write Python in PHP?

Is there a PHP implementation of Python?

过去多啦不再A梦过去多啦不再A梦2867 days ago478

reply all(3)I'll reply

  • 天蓬老师

    天蓬老师2017-05-16 13:02:21

    You can call shell commands in phppythonExecute python files

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-16 13:02:21

    The respondent’s question is not clear enough.

    Do you want:

    1. Run Python files through PHP? That's no different than applying ordinary shell commands.

    2. Requires interaction between PHP and Python, such as applying a certain function or class of Python in PHP.

    3. Requires a Python parser written in PHP.

    reply
    0
  • PHPz

    PHPz2017-05-16 13:02:21

    some.py


    !/usr/bin/env python

    -- coding:utf-8 --

    import sys

    def do_some(a):
    return "dosome:%s" % a

    if name == '__main__':
    a = sys.argv[1]
    if a:
    T = do_some(a)
    print T

    //t.php

    <?php
    $k = $_REQUEST['k'];
    if (!empty($k))
    {
    $k = trim($k);
    // $a = array();
    // exec('python ./some.py '.$k, $a);
    // echo $a[0];
    passthru('python ./some.py '.$k);
    }

    In my example above, py and php are placed in the same directory

    Then enter http://localhost/t.php?k=test in the browser
    The browser will return dosome:test

    But you must have a python environment

    reply
    0
  • Cancelreply