Skip to content

Instantly share code, notes, and snippets.

@Trainmaster
Trainmaster / add.php
Created Jan 11, 2018
Behaviour of weak type check (string/float)
View add.php
<?php
function add(float $a, float $b): float {
return $a + $b;
}
View recursive-array-modification.php
<?php
$data = array('foo', '', 'bar');
array_walk_recursive($data, function (&$value, $key) {
if ($value === '') {
$value = null;
}
});
var_dump($data);
@Trainmaster
Trainmaster / ImageMagickCommandLineProcessor.php
Last active Jan 2, 2016
ImageMagickCommandLineProcessor for processing uploaded image files
View ImageMagickCommandLineProcessor.php
<?php
/**
* ImagickProcessor
*
* @author Frank Liepert <contact@frank-liepert.de>
*/
class ImageMagickCommandLineProcessor
{
/** @type string $error */
protected $error;
View pdflib_pcos_colorspace.php
<?php
try {
$pdflib = new PDFlib;
$colorspace = new Colorspace($pdflib);
print_r ($colorspace->analyze('test.pdf'));
} catch (PDFlibException $e) {
print_r ($e);
}
class Colorspace
View signatures.php
<?php
class Parents
{
public function doSomething($x)
{
print $x;
}
}
class ChildA extends Parents
View reset.php
<?php
class Foo
{
protected $bar;
public function __construct()
{
$this->bar = 'bar';
}
View config.php
<?php
$container->addDefinition('AnotherService', new Definition('Application\Service\AnotherService'))
->setter('setFoo', 'Foo');
$container->addDefinition('ExampleService', new Definition('Application\Service\ExampleService'))
->constructor(array('@AnotherService'));
$container->addDefinition('Session', new Definition('lib\Session\Session'));
$container->addDefinition('SessionAwareInterface', new Definition('lib\Session\SessionAwareInterface'))
@Trainmaster
Trainmaster / htmlspecialchars-types.php
Last active Dec 17, 2015
Behaviour of htmlspecialchars() (or any other string function?) when passing arguments with different types
View htmlspecialchars-types.php
<?php
$string = 'foo';
var_dump(htmlspecialchars($string));
// string(3) "foo"
$integer = 1;
var_dump(htmlspecialchars($integer));
// string(1) "1"
$float = 1.2;
@Trainmaster
Trainmaster / gist:5330082
Last active Dec 15, 2015
Extract array values by html array notation.
View gist:5330082
<?php
function getInputValueByName($name, $data) {
if (strpos($name, '[]') !== false) {
$name = str_replace('[]', '', $name);
}
$parts = explode('[', $name);
$value = $data;
foreach ($parts as $part) {
$part = rtrim($part, ']');