When the script is only used in a particular page it can be written inside that page, there is no need to externalize it. If that script will be used in more than one place or it's inside a really big php file it may be a good idea to externalize it for better maintenance. So, how to do it? Lets see an example.
The container page
This is everything we need to change to get it working.
If we need to send some php variables to the script it can be achieved like any php GET parameter. Here it's a simple example:
Have any doubts? Write a comment and i'll try to help.