How can I re-use PHP code across different pages?

You’re looking for something similar to PHPs require or include function.

In WordPress we can use get_template_part( 'example-folder/template' ); to get template.php from the example-folder.

You should also look into the get_template_part function to see the capabilities. If you’re wanting to include outside variables into these, you can use PHP globals.