News for hackers. Stuff that matters.
Re:Ask HN: PHP Tricks and Best Practices? by DanHulton on Wednesday October 29, 2008 @02:41PM (#347807)
I've had trouble for a while now with chained ifs, as in:

  [CHUNK A]
  if (chunk a succeeds) {
      [CHUNK B]
      if (chunk b succeeds) {
          [CHUNK C]
          if (chunk c succeeds) {
              echo 'yay!'
          }
          else {
              echo 'boo!';
          }
      }
      else {
          echo 'boo!';
      }
  }
  else {
      echo 'boo!';
  }
This gets messy if there's anything interesting in those chunks. Instead, take advantage of short-circuiting and put those chunks in functions. Then you can rewrite the above as:

  $success = do_chunk_a();
  
  $success = $success && do_chunk_b();
  
  $success = $success && do_chunk_c();
  
  echo $success ? 'yay!' : 'boo!';
Also, use the ternary operator EVERYWHERE YOU CAN - you'll clean up so much code with it.
Re:Ask HN: PHP Tricks and Best Practices? by nuclear_eclipse on Wednesday October 29, 2008 @03:25PM (#347867)
even better is:

    $success =
        do_chunk_a() &&
        do_chunk_b() &&
        do_chunk_c();
    ...