Skip to content

Instantly share code, notes, and snippets.

View bxt's full-sized avatar

Bernhard Häussner bxt

View GitHub Profile
@bxt
bxt / Avoid hundrets isset() calls.php
Created February 26, 2011 14:42
Prepare array with false-Values for undefinded keys with array_fill_keys (in PHP >= 5.3.0)
<?php
$in_array=array("foo"=>"fooval","baz"=>"bazval");
$in_array+=array_fill_keys(array("foo","bar","baz"),false);
// array_fill_keys is since PHP 5.2.0
// useage:
echo $in_array["foo"]?:"foodef";
// short ternary operator is since PHP 5.3.0
echo "\n";
@bxt
bxt / bxt_fileList.php
Created February 26, 2011 14:57
List all files into an array, no dirs, optianally filtered by extension (using scandir)
<?php
function bxt_fileList($d,$x=false){
foreach(scandir($d) as $f)if(is_file($d.'/'.$f)&&(!$x||preg_match('/'.$x.'$/i',$f)))$l[]=$f;
return $l;
}
// silly "ninja" comment code from
// http://de3.php.net/manual/en/function.scandir.php#90628
function file_list($d,$x){
foreach(array_diff(scandir($d),array('.','..')) as $f)if(is_file($d.'/'.$f)&&(($x)?ereg($x.'$',$f):1))$l[]=$f;
@bxt
bxt / Fail-array.access.php
Created February 26, 2011 15:01
PHP is failing to immidately acces an array returned by a function
<?php
echo foo()[0]; // PHP Parse error: syntax error, unexpected '[', expecting ',' or ';'
function foo() {
return array("cant thouch this");
}
// use this instead:
list($tmp)= foo();
echo $tmp;
@bxt
bxt / one-line_object_creation.php
Created February 26, 2011 15:14
How to (and not to) use & create a PHP instance in the same line.
<?php
// echo (new Foo())->$bar; // fails
// PHP Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';'
echo Foo::getI()->bar; //works
class Foo {
public function __construct() {
$this->bar="hello world\n";
}
static function getI() {
@bxt
bxt / SVN-KDE-snip.sh
Created February 26, 2011 15:17
Opens current changes to working copy in kompare
svn diff | kompare -o -
#and of course:
git diff | kompare -o -
@bxt
bxt / production-debug-log.php
Created February 26, 2011 15:19
Debugging PHP in production you can use this to save your logs form growing big
<?php if(rand(1,1000)==42) {trigger_error($debugout);}
@bxt
bxt / ternary-varname.php
Created February 26, 2011 15:22
Accessing a var by ternary operator condition in PHP
<?php $var="foo"; $foo=array(); ${true?$var:"bar"}[]=$var; var_dump($foo);
/* Output:
array(1) {
[0]=>
string(3) "foo"
}
*/
@bxt
bxt / whats-happening.php
Created February 26, 2011 15:26
Some really cryptic php, helps to test your operator precedence knowledge
<?php echo 0 || 1337 ? 4 : 0 | -4 ? 5 : 0 ? 7 ^ 5: 0 ; // 2
@bxt
bxt / javascript-multitester.js
Created February 26, 2011 15:29
Interesting method to do multi-value-testing in JS
({abc:1,def:1,jkl:1}[xyz])
// nearly the same as:
( xyz=="abc" || xyz=="def" || xyz=="jkl" )
// and....
(function(){switch(pn) {
case "prv":return "previous";
case "nxt":return "next";
}})()
@bxt
bxt / really-annoying.sh
Created February 26, 2011 15:40
if you really want to annoy someone (or the whole office) run this on their terminal:
(while true; do echo -n $'\a';sleep 1; done;) &