title: Same code on browser/server
body: <p>I'm writing a web app using PHP, and running into several situations where I need similar code on the server and browser. For example:</p>
  
  <ul>
  <li>I want to validate user input, and generate the same error messages on both sides.</li>
  <li>I want to format data using certain rules (e.g. if a given field is less than 1, show it with two decimal places, otherwise none), and have it appear the same regardless of which side renders it.</li>
  </ul>
  
  <p>It seems like this should be a common problem, as people are moving more logic from the server to the browser. But are there any common patterns or libraries for dealing with it (particularly for PHP)?</p>