Skip to content

Instantly share code, notes, and snippets.

@renarsvilnis
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renarsvilnis/bd35082af37d3bf8ab81 to your computer and use it in GitHub Desktop.
Save renarsvilnis/bd35082af37d3bf8ab81 to your computer and use it in GitHub Desktop.
Some tips or useful info about PHP that i found interesting or useful at the moment of writing.

PHP trick-tips

Some tips or useful info that i found interesting or useful at the moment of writing.

UTF-8

USE IT EVERYWHERE. THE END.

Working with Databases

Data validation

Based on stackoverflow article. Sanitize & Filter -> Validate -> Prepare for storage -> Outputting back to HTML

Sanitize & Filter

Validation

Never ever rely on client-side validation, its just for UX.

Prepare for database storage

Outputting back to HTML

Magic qoutes

TODO http://aaroncameron.net/article.html?aID=59 http://php.net/manual/en/function.addslashes.php https://www.google.lv/webhp?sourceid=chrome-instant&ion=1&espv=2&es_th=1&ie=UTF-8#q=what+are+magic+quotes+in+php+w

Closures and lambdas

Creation within class definition

The only small difference is that closures defined in class methods may also access the class and the current object via $this. Since $this is saved “within the closure” the corresponding object will live at least as long as the closure.

Because not all closures defined in class methods need $this, it is possible to declare a lambda function to be static:

 class Example {
   public function doSomething () {
     $x = 4;
     $closure = static function ($y) use ($x) {
       return $x + $y;
     };
     return $closure (6);
   }
 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment