PHP is mainly used in the server side of the site to do the background development, some functions with PHP to achieve a little effort or can not be achieved, now in the learning of Python, is also a scripting language, feel Python can do things PHP may not be competent. But now most of the Web site is written in PHP, although Python can be used as a web development (flask, this look for a while to feel that since the Web site with PHP development, Python can do bad things php do, it is better, pause), Now there is a question: Is there any way that PHP and Python can be combined, PHP and Python do what they are good at, after all, the work of rapid development operation see the effect or accounted for a large demand ratio.
Remember that there is a function in PHP that can run third-party scripts: exec (), look at the function structure of exec:
string Exec (string $command [, Array & $output [, int & $return _var]])Parameter interpretation (from the PHP Manual) command to execute commands. If output is supplied, this array is populated with the outputs of the command execution, and each row of output fills an element in the array. The data in the array does not contain white space characters at the end of the line, such as \ n characters. Note that if some elements are already included in the array, the EXEC () function appends the contents to the end of the array. If you do not want to append at the end of the array, use the unset () function to reset the arrays before passing in the Exec () function. Return_var if both the output and the Return_var parameters are supplied, the return state after the command execution is written to this variable. Return value: The last line of the command execution result. If you need to get all the unprocessed output data, use the PassThru () function. If you want to get the output of the command, make sure to use an output parameter.
PHP executes an external program extension:
1, system-execute external program, and display outputDescription: String System (String $command [, int & $return _var]) is the same as the C version of the system () function, which executes the command specified by a command parameter and outputs the execution result. If PHP is running in a server module, the system () function will also attempt to automatically refresh the output cache of the Web server after each line has finished outputting. Use the PassThru () function if you want to get the original output of a command without any processing. Parameters: Command to execute the commands. Return_var if the Return_var parameter is supplied, the return state after the execution of the external command is set to this variable. Return value: Success returns the last line of the command output, FALSE if failed
2. passthru-executes external program and displays original outputvoid PassThru (String $command [, int & $return _var]) is similar to the EXEC () function, and the PassThru () function is also used to execute external commands. This function is used instead of the exec () or system () function when the executed Unix command outputs binary data and needs to be delivered directly to the browser. Often used to perform commands such as pbmplus that can output image streams directly. By setting Content-type to Image/gif, and then calling the Pbmplus program output GIF file, you can output the image directly from the PHP script to the browser. Parameters: Command to execute the commands. Return_var if the Return_var parameter is supplied, the return status of the Unix command is logged to this parameter. Return value: no return value. The function used to execute the external program is the EXEC function:
PHP test Code:
Header("Content-type:text/html;charset=utf-8"); //test php to execute Python code $a= 5; $b= 8; $c= ' Davidszhou PHP operates a python script with parameters and returns the result '; $d=UrlEncode($c); unset($out); $c=exec("C:\python35\python plug/index.py {$a} {$b} {$d}",$out,$res); Print_r(UrlDecode($out[0])); Echo"<br>"; Echo' External program is running successfully: '.$res." (0 stands for Success, 1 represents failure) ";
python test code:
# -*-coding:utf-8-*- Import SYS def Ceshi (a,b,c): = Int (a) +int (b) = Int (d) +C return rif__name__ "__main__": = Ceshi (A=sys.argv[1],b=sys.argv[2],c = Sys.argv[3]) Print(res)
Explanation: 1, exec problem: exec ("C:\python35\python plug/index.py {$a} {$b} {$d}", $out, $res): "C:\python35\python" : This is the absolute environment for Python installation in the local environment, ' plug/index.py ': This is the directory of the Python script, followed by {$a} {$b} {$d} is followed by a parameter that takes note of the space between it and the Python script directory and between each parameter, 2, How Python receives PHP's arguments for him python receives PHP parameters mainly through the SYS module: explanation (from the Python manual): sys.argv the command-line argument list passed to the Python script. Argv[0] is the name of the script (whether it is the full path name depending on the operating system). If the command is executed with the-C command-line option of the interpreter, Argv[0] is set to the string '-C ' if no script name is passed to the Python interpreter, Argv[0] is an empty string. To traverse the list of files given on the standard input or command line, see the Fileinput module.
php with parameter value call Python script