Skip to content

Instantly share code, notes, and snippets.

@gavinblair
Created September 2, 2010 14:13
Show Gist options
  • Save gavinblair/562335 to your computer and use it in GitHub Desktop.
Save gavinblair/562335 to your computer and use it in GitHub Desktop.
Reversing the order of a condition to avoid hard-to-find assignment problems
<?php
//This is what I normally do:
if($something == "string") { ... }
//The problem is, if I accidentally miss an = then it would be an assignment!
if($something = "string") { /* uh oh! */ }
//To prevent this, get in the habit of reversing the order:
if("string" == $something) { ... }
//That way, if you miss an = and it becomes an assignment, it will throw an error!
if ("string" = $something) { /* you can't assign a value to a constant! */ }
@SeanJA
Copy link

SeanJA commented Sep 10, 2010

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment