register javascript function in php

register javascript function in php

hi i want to register javascript function in php file but this doesnt work, please guide me through
this is my php code
<?php
echo "<script language='javascript'>";
echo "function test() { ";
echo "alert('hello world')";
echo "} ";
echo "</script>";
?>

i m calling this php file through ajax