sample program to pass variable : php to python

sample program to pass variable : php to python

An example of how you can pass a variable from PHP to Python:

PHP code (test.php):

<?php
$variable = "Hello from PHP!";
exec("python test.py \"$variable\"", $output);
echo implode("\n", $output);
?>

Python code (test.py):

import sys

# Get the variable passed from PHP
variable_from_php = sys.argv[1]
print("Received from PHP:", variable_from_php)

# Do something with the variable
# For example, convert it to uppercase
variable_from_php_upper = variable_from_php.upper()
print("Uppercase:", variable_from_php_upper)

In this example, test.php passes the variable "Hello from PHP!" to test.py using the exec() function. The Python script test.py receives this variable as a command-line argument (sys.argv[1]), then converts it to uppercase and prints it.

When you run test.php, it will execute test.py with the given variable and print the output.

Leave a Reply

Your email address will not be published. Required fields are marked *