You can't. Javascript is a client side technology, while PHP is a server side technology. You can't call JavaScript from PHP, because the PHP is executed on the server before the page even gets served to the client.
What you can do is use PHP to dynamically generate javascript code before it is sent to the browser - for example, you could have:
<?php
if ($myvariable == '1')
{
print "<script>writetext(\"hello\");</script>";
}
?>