WordPress is written in PHP. Depending on the project/your needs, there are several ways to extend functionality of WordPress.
The Dedicated Plugin Way
This is way to go when you want to “isolate” the features you’ll add.
The Theme way
Themes have a file called functions.php that is always executed.
To avoid your added changes get overwritten, make sure you create a Child Theme and add your changes to the functions.php of the Child Theme.
It’s a quick and easy way to test something. Conceptually it might feel not a 100% to mix feature stuff with Theme stuff.
The “Use a Plugin” Way
Use a plugin e.g. Code Snippets to add php code.
Perfect if you want to quickly add something and/or want to do some testing.