How do I run a php function onclick of a button

I’m trying to make PHP respond onClick of a button the game I’m making has tons of buttons and the way I found it online is too long how do I do it in less than 3 lines?

My code so far

 $coins = 123423142134234;
 function foo() {
   $coins = 4321;
    echo $coins;
<button onclick="<?php foo(); ?>">yo</button>

Php runs on the server side and cannot be run on the client side.

To achieve your use case, you have to call a JavaScript method. Because JavaScript is a client-side scripting language.