Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Tibor Ruzinyi
Tibor Ruzinyi
17,968 Points

PHP exec function passing a value to command line

Hi there,

i need some help from an advanced users in php.

I need to call an exe file and send a value to the command line with the exec function(i think is the right for this situation).

I have found this code to run an exe file :

```exec( 'abc.exe');

but how to send a value to command line? Any idea ?

4 Answers

Andrew Shook
Andrew Shook
31,709 Points

You need to use the $argv variable built in to PHP. Exec() method emulate the command line, meaning exec("example.php") works the same as php -f example.php on the command line. This means you can pass arguments to by putting a space between the file name and all the arguements (exactly like you would do in the command line). So, exec("example.php $var1 $var2") is the same as php -f example.php $var1 $var2 on the command line. Now the $argv variable come into pay in the executed script. Inside of example.php if you var_dump $argv you will see an array, where the zero index is the name of the script being executed and all the other indexes are the variables passed to the script. Check out the PHP Manual for more information about $argv.

An important thing to remember is the $argv breaks up arguments by spaces. So if you pass a string into the exec() function, and it has spaces in it, $argv will break the string into separate arguments. For example:

$string = "this is a string";
exec("example.php $string");

//inside example.php
var_dump($argv);

this will output an array that looks like this:

array[4]{
    [0]=>"this",
    [1] =>"is",
    [2]=>"a",
    [3]=>"string".
}

If you want to pass in a string as an argument you need to escape all the spaces in the string.

Tibor Ruzinyi
Tibor Ruzinyi
17,968 Points

Hello Andrew,

thank you for your tip. I have one more question. Does it work with exe files or just php files? Iam trying to pass a value to a command line of an exec file like this :

<?php 

exec('abc.exe -value');

exit();
?>
Andrew Shook
Andrew Shook
31,709 Points

It can work with exe files, as far as I know, but only if they are in the same directory as the calling script or included in your windows path directory. Why are you using an exe file? Are you planning to host on a windows server?

Tibor Ruzinyi
Tibor Ruzinyi
17,968 Points

well i have a website on windows server, and i need to send email adresses to exe files command line . But it doesnt work dunno why. I tried to run windows calculator with this script to check if its working, and it doesnt work as well.

Andrew Shook
Andrew Shook
31,709 Points

Tibor, I don't really mess around with windows server. In fact I try to avoid them like the plague. I don't think I will be much help with trying to figure out how to set everything up so that it can run on a windows server. The only thing I can tell you is that it should be possible.

Tibor Ruzinyi
Tibor Ruzinyi
17,968 Points

Ok no problem , thank you anyway for your time.