在PHP中将arg从函数传递给bash脚本

I have a menu system on a web page which allows a user to select from several options. When an option is selected, I want to pass it as an arg through a function. I want to evaluate to see what was selected, and then execute my script supplying the arg. I'm new to PHP, but happy to be learning. Below is my best attempt after trying to digest related explanations.

public function getData($args=array()) {

        if(isset($args["what"])){
            exec(
                '/home/foo/myscript.((string)$args["what"]');
        }
        else{
            exec(
                '/home/foo/myscript()');
        }
    }

I suspect I don't have the space accounted for correctly between the shell script and the argument, but the seemingly reasonable variations I have tried all fail. Am I on the right track or should I try it another way all together?

exec('/home/foo/myscript.((string)$args["what"]');

The problem with this is that you are still within your string when you start trying to concatenate PHP. Close the string first.

exec('/home/foo/myscript ' . (string)$args["what"] );

Next, you're going to want to be much more secure about passing arguments to scripts so that they don't get misinterpreted. Check out escapeshellarg(). http://php.net/manual/en/function.escapeshellarg.php

exec('/home/foo/myscript ' . escapeshellarg((string)$args["what"]) );

Also, you probably don't need (string) since escapeshellarg() only uses strings anyway. Whatever is in that parameter will be cast to a string.

exec('/home/foo/myscript ' . escapeshellarg($args['what']) );