We're growing into something new. Read More
nathancarnes

Nathan Carnes


<?= ($username) ? "Hello, $username!" : "Welcome, Guest!"; ?>

<? if($thing1 == $thing2): ?>
  <!-- do some stuff -->
<? else: ?>
  <!-- do something else -->
<? endif; ?>

<? foreach($widgets as $widget): ?>
  <li><?= $widget[name]; ?></li>
<? endforeach; ?>

Write PHP like a Rubyist

After starting off my career in PHP and then falling deeply in love with Ruby (and of course Rails), I still find myself working on PHP projects from time to time. A lot of devs complain about PHP, and it can be clunky at times, but after stepping away for a while and coming back, I realized a lot of the reason why PHP is often so ugly is how we're writing it. While there are a lot of principals from Ruby/Rails that make PHP a lot more fun to work with (DRY, ORMs, etc), one of the simplest things I did was to start leveraging PHP's alternative control structure syntax to prevent my code from becoming a tangle of curly braces.

STR_TO_DATE(my_time_field, '%h:%i%p') as my_time

Convert time string to datetime in MySQL

Need to sort by a time field that's stored like '09:00AM'? Convert it to a valid DATETIME field in your SELECT statement, and then ORDER_BY works just how you'd expect. Simple, I know, but would have saved me some time yesterday. P.S. never store your times like that.