In a .php file. Make a new folder in the assets/snippets folder, name it whatever you like. Create a file with the php code, put it in the new folder. In the MODx manager, create a new snippet (Resources -> Manage Resources, the Snippets tag. Put
include 'assets/snippets/foldername/filename.php';
in the snippet Wherever you want the php output to appear, either in your template or the content of a page, put [[snippetname]].