LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-23-2020, 10:42 PM   #1
buddy1234567
LQ Newbie
 
Registered: Sep 2020
Posts: 2

Rep: Reputation: Disabled
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
//Add two numbers together
function Calculator($x, $y, $op) {
    if($op == '+') {
        $result = $x + $y;
    }
}
I'm attempting to pass values generated in bash to Calculator() like so:

Code:
X=$[RANDOM%10+1]
Y=$[RANDOM%20+10]

# Addition
result=$(php -r "require 'testAddTwoNums-sol.php'; Calculator("X","Y",'+');")
but it doesn't work. I get an error saying 'Warning: Use of undefined constant X - assumed 'X'

How can I pass X and Y from bash using the php cli (as I'm attempting to do above)?
 
Old 09-23-2020, 10:55 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
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},'+');")
Give that a try, and good luck!

Last edited by astrogeek; 09-23-2020 at 11:03 PM. Reason: tpoy
 
2 members found this post helpful.
Old 09-23-2020, 11:38 PM   #3
buddy1234567
LQ Newbie
 
Registered: Sep 2020
Posts: 2

Original Poster
Rep: Reputation: Disabled
THanks so much!

That worked perfectly! I gave you a shoutout on StackOverFlow .

Last edited by buddy1234567; 09-23-2020 at 11:40 PM.
 
Old 09-24-2020, 02:15 AM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196Reputation: 4196
You are welcome!
 
Old 09-24-2020, 07:34 AM   #5
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,616

Rep: Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554

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.

 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Call a function within a function in Bash Script bluethundr Linux - Newbie 3 08-23-2018 03:08 PM
global variable in bash function call mvairavan Programming 3 07-18-2012 09:44 PM
[SOLVED] Threaded function cannot call a function with extern "C" but nonthreaded function can morty346 Programming 16 01-12-2010 05:00 PM
How to call another function from a function? geminigal Programming 4 04-21-2005 10:41 PM
Passing one php function result as a parameter to another php function davee Programming 13 09-12-2004 12:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration