how can I create an hook when external id change
Create an extension

The first step to create a hook is to create an extension.
I'd recommend installing civix which can generate an empty extension by simply typing the command :

civix generate:module myextension


Go check https://docs.civicrm.org/dev/en/latest/extensions/ for more details about extensions.

All you need to know for now is that civix will create a file called myextension.php where you can put your hooks. There are a few hooks commented as examples in the generated files.

Create the hook

A hook is simply a function that start with the name of your extension and finish with the name of the hook. For example hook_civicrm_post will be named myextension_civicrm_post in the extension we've just created.

In your case, I believe hook_civicrm_post is the one you want to use but you can see the documentation about the hooks in :


https://docs.civicrm.org/dev/en/latest/hooks/
https://docs.civicrm.org/dev/en/latest/hooks/list/