Ravindu Rashmika Ravindu Rashmika - 1 year ago 60
PHP Question

execute a python code from php and take the return value back to the php code example

I have a python script and a PHP script what I need is execute the python script through PHP and should get the return value of python code back to PHP.Here is my codes

$result1= passthru("python E:\naive-bayes-classifier-master\naiveBayesClassifier\return.py");

echo "return value is: $result1" . "\n";


I execute this through another php script using a submit button

and here is the python code

def add(a, b):
# print "ADDING %d + %d" % (a, b)
return a + b

def subtract(a, b):
#print "SUBTRACTING %d - %d" % (a, b)
return a - b

def multiply(a, b):
#print "MULTIPLYING %d * %d" % (a, b)
return a * b

def divide(a, b):
# print "DIVIDING %d / %d" % (a, b)
return a / b

if __name__ == '__main__':

the result I am getting is

return value is:

there no any value.Please help.Thank you in advance. :)

Answer Source

this would be appropriate for scenario

instead of pass passthru


$command = escapeshellcmd('E:\naive-bayes-classifier-master\naiveBayesClassifier\return.py');
$output = shell_exec($command);
echo $output;


refer this : Running a Python script from PHP

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download