Reputation: 2316
I want to to exchange "30" and "16" with two variables "hour" and "mins" in a php exec function
$hour = 30;
$mins = 16;
exec('echo "30 16 * * * sudo /home/pi/raspberry-remote/./send 11010 1 1" | crontab -');
and changed it like that:
exec('echo "$hour $mins * * * sudo /home/pi/raspberry-remote/./send 11010 1 1" | crontab -');
How can i get this right? Thank you
Upvotes: 0
Views: 1023
Reputation: 101
You can do this using concat string
exec('echo "'.$hour.' ' .$mins.' * * * sudo /home/pi/raspberry-remote/./send 11010 1 1" | crontab -');exec('echo "$hour $mins * * * sudo /home/pi/raspberry-remote/./send 11010 1 1" | crontab -');
Upvotes: 1
Reputation: 5744
I recommend you sprintf. That does not lose readability.
$format = 'echo "%d %d * * * sudo /home/pi/raspberry-remote/./send 11010 1 1" | crontab -';
exec(sprintf($format, $hour, $mins));
Upvotes: 1
Reputation: 22646
Variables inside single quoted strings are not populated. Try either
exec('echo "'.$hour.' '.$mins.' * * * sudo /home/pi/raspberry-remote/./send 11010 1 1" | crontab -');
or
exec("echo \"$hour $mins * * * sudo /home/pi/raspberry-remote/./send 11010 1 1\" | crontab -");
Upvotes: 3
Reputation: 780714
Variables aren't expanded inside single-quoted strings, only inside double-quoted strings. So switch your quotes around.
exec("echo '$hour $mins * * * sudo /home/pi/raspberry-remote/./send 11010 1 1' | crontab -");
Upvotes: 2