Use bash variable in bash call to php function?
Here is the situation:
I have a php script called testAddTwoNums-sol.php with a function called Calculator: Code:
<?php Code:
X=$[RANDOM%10+1] How can I pass X and Y from bash using the php cli (as I'm attempting to do above)? |
Welcome to LQ!
To reference the value of variables in bash, called variable expansion, you must precede the variable name with a $. So to pass the values of X and Y in your php command you need to refer to them as $X and $Y. If the name would be ambiguous such as being surrounded by other characters then put the names inside curly braces, ${X} and ${Y}. So you command might become (not tested in actual use, but you get the idea): Code:
result=$(php -r "require 'testAddTwoNums-sol.php'; Calculator(${X},${Y},'+');") |
|
You are welcome!
|
I assume this is a simplified example of a slightly different problem, because:
1) PHP can generate random numbers without Bash. 2) Bash can do basic integer arithmetic without PHP. |
All times are GMT -5. The time now is 08:24 PM. |