Without exploring the shell_exec() functionality...
You are confusing javascript for PHP code. You are also confusing server-side code with client-side code. In the code below:
print('<a href ="#" onclick = " print1(); return false;">Print</a>');
echo "<br/>";
}
function print1()
{
$fin = shell_exec("lpr -Pprinter1 $str");
}
...you cannot call a php function from a link in the browser directly. Here's why:
Once you have the html anchor's onclick set to listen and fire print1(), it will be looking for a javascript function called print1() in the browsers scope only (client-side.) Your php function print1() cannot possibly be in scope because you "left it behind" in the server script (php runs at the server. server-side.) Javascript cannot possibly call the php function shell_exec() either because it's back on the server (and I might add a completely difference programming language.) In other words, you need to send a post or get version of an http header to request anything from the server.
Now you can do this in two ways. If you don't mind the page redirecting, you can set a real url in the anchor tag that goes off to an additional php script that calls your print1() function (and so on...)
Another method would be to have javascript make the get or post request. This is called ajax. The advantage is: no reloading of the html page.