Skip to content

Instantly share code, notes, and snippets.

View nikic's full-sized avatar

Nikita Popov nikic

View GitHub Profile
<?php
var_dump(hex2bin("1af8b47f6d8"));
var_dump(hex2bin("01af8b47f6d8")); // left pad
var_dump(hex2bin("1af8b47f6d80")); // right pad
/* OUTPUT
string(5) "→°┤⌂m"
string(6) "☺»ïG÷Ï"
string(6) "→°┤⌂mÇ"
diff --git a/ext/standard/formatted_print.c b/ext/standard/formatted_print.c
index f26e5d8..e52fb3f 100644
--- a/ext/standard/formatted_print.c
+++ b/ext/standard/formatted_print.c
@@ -468,21 +468,26 @@ php_formatted_print(int ht, int *len, int use_array, int format_offset TSRMLS_DC
PRINTF_DEBUG(("sprintf: looking for modifiers\n"
"sprintf: now looking at '%c', inpos=%d\n",
format[inpos], inpos));
- for (;; inpos++) {
- if (format[inpos] == ' ' || format[inpos] == '0') {
diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
index c3c35eb..b1feb7a 100644
--- a/Zend/zend_compile.c
+++ b/Zend/zend_compile.c
@@ -6089,7 +6089,19 @@ void zend_do_isset_or_isempty(int type, znode *result, znode *variable TSRMLS_DC
zend_do_end_variable_parse(variable, BP_VAR_IS, 0 TSRMLS_CC);
- zend_check_writable_variable(variable);
+ if (type == ZEND_ISEMPTY) {
<?php
register_tick_function(function() {
static $i = 0;
if (++$i >= 100000) {
throw new Exception('Too many ticks!');
}
});
declare(ticks=1) {
@nikic
nikic / microbench.php
Created June 22, 2012 23:42
Microbenchmark of generator implementation
<?php
error_reporting(E_ALL);
function xrange($start, $end, $step = 1) {
for ($i = $start; $i < $end; $i += $step) {
yield $i;
}
}
Fix some lengths in crypt()
Use salt_len_in instead of strlen(salt) or PHP_MAX_SALT_LEN, otherwise too
much memory will be allocated.
sha512 has a 86 character checksum, not 43. That probably was a copy&paste
from the sha256 code which indeed has 43.
The allocation also were using sizeof(char *) instead of sizeof(char), thus
allocating 4 or 8 times as much memory as necessary.
@nikic
nikic / µ.php
Created June 30, 2012 23:58
Something I found in my codebase...
<?php
class Registry implements ArrayAccess {
public $this = null;
protected $impls = array();
protected $curImpl = 0;
public function &__get($key) {
if (isset($this->__onUndefinedProperty)) {
@nikic
nikic / coroutine.php
Created July 14, 2012 13:25
A coroutine example: Streaming XML parsing using xml_parser
<?php
error_reporting(E_ALL);
/* Data can be send to coroutines using `$coroutine->send($data)`. The sent data will then
* be the result of the `yield` expression. Thus it can be received using a code like
* `$data = yield;`.
*/
/* What we're building in this script is a coroutine-based streaming XML parser. The PHP
@nikic
nikic / guestbook.markdown
Created July 29, 2012 14:21
Quick doesn't have to mean dirty: Also applies to PHP!

Quick doesn't have to mean dirty: Also applies to PHP!

This is just a quick response to http://me.veekun.com/blog/2012/07/28/quick-doesnt-mean-dirty/. I won't bother to write a proper blog post for this, so a Gist will have to do ;)

When I read that article, one thing really striked me: If you want to quickly create a web app in PHP, you do exactly the same. I mean, exactly.

I never used the Silex microframework before, so I took this as a chance to see how it works. I'll just do the same as eevee did, only with a bit less commentary (this is a Gist after all!)

I hope that this will show you that PHP and Python are really similar to work with. Also this should show that just because you're using PHP, doesn't mean that you write dirty code. The similarity in the process and code is really incredible :)

string {
string capitalize()
string titleize()
string toLower()
string toUpper()
int length()
string slice(int $offset, int $length = magic)
string replaceSlice(string $replacement, int $offset, int $length = magic)