简体   繁体   中英

execute a multi line shell comand in PHP

I'm trying to get user email of current user using command line. In Powershell i paste these lines and it works

>> $searcher = [ADSISearcher] "(sAMAccountName=$env:USERNAME)"
>> $searcher.PropertiesToLoad.AddRange(@("mail"))
>> $searcher.FindOne().Properties["mail"][0]
test@gmail.com

but when i try to do this in php with exec or shell_exec

$cmd=<<<CMD
$searcher = [ADSISearcher] "(sAMAccountName=$env:USERNAME)" \
$searcher.PropertiesToLoad.AddRange(@("mail")) \
$searcher.FindOne().Properties["mail"][0]
CMD;

print_r(shell_exec($cmd));

it shows me errors undefined variable searcher

You want to use nowdoc instead of heredoc . Just enclose CMD in single quotes and PHP will not try to expand your $searcher as a PHP variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM