Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,151,908 members, 7,814,069 topics. Date: Wednesday, 01 May 2024 at 05:24 AM

Running Python Codes In Php - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Running Python Codes In Php (8137 Views)

How Can Someone Draw , Convert Videos Or Images To Codes In Java / Online Shopping System In Php Documentation Help / Differentiating Bar Codes In A Batch: Help Needed (2) (3) (4)

(1) (Reply)

Running Python Codes In Php by cdeveloper(m): 5:48pm On Sep 10, 2010
Lately i started playing with mixing python and php and it happens to be that easy, the point is what is the sense in that.Anyway like the say give to PHP what is PHP and to Python what is Python, th XP guys will tell you that the best way to mix language is to use the strength of each and leave out their weaknesses Today i show you how to call a python script from within php

   //The php code that calles the python code

<?php
 
  //Simple case , call a python code to add two numbers and return the result to php
 
$param1=10;

$param2=20;

  $command="Path\/to\/python\/installation  script_add_number.py $param1 $param2";

  //use the passthru command to execute and return the result

  $buffer='';
  ob_start() // prevent outputting till you are done
   passthru($command);
 
  //get the result out
$buffer=ob_get_contents();
  //clean up the buffer
ob_end_clean();
//echo the result
echo $buffer;

?>



The python code to be executed is shown next



#we need the sys module to get access to the passed in parameters
import sys

# the first parameters in sys.argv[0] is the path to the current executing python script

param1=sys.argv[1]

param2=sys.argv[2]

# Python sees this two parameters as string so we need to cast them to integer before we use it

sum=int(param1) + int(param2)

# send the result back to php
print sum


This is how simple it is to mix php and python, what can be done with this lies in the imagination of the programmer
Happy salah to all my muslim coders
Re: Running Python Codes In Php by joefazee(m): 11:30am On Sep 12, 2010
I love your example, but why should we mix two high-level language like Python & PHP, i think is better to embed PHP/Python into C/C++ instead of mixing to slow languages.

Though nice. u`re good

(1) (Reply)

Common Error In Laravel #1:solved File_put_contents Failed To Open Stream / Computer Science Degree Recommended For Programming Career? / Journey To Being A FULLSTACK Web Developer

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 8
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.