I was having a conversation on IRC and I wanted to demo some code generation I did this afternoon.
I used this PHP to generate this BASH code.
And I used this PHP to generate this PHP code.
I think PHP is a good choice for code generation.
I was having a conversation on IRC and I wanted to demo some code generation I did this afternoon.
I used this PHP to generate this BASH code.
And I used this PHP to generate this PHP code.
I think PHP is a good choice for code generation.
Here is another PHP thing I need to think clearly about from time to time:
var_dump( [ 'a' => 1 ] + [ 'a' => 2, 'b' => 2 ] );
Returns:
array(2) { 'a' => int(1) 'b' => int(2) }
I was in bed trying to get to sleep but my brain wanted to know the answer to this question. So I was forced out of bed to write this experiment:
function main( $argv ) { register_shutdown_function( 'shutdown_1' ); register_shutdown_function( 'shutdown_2' ); exit( 0 ); } function shutdown_1() { exit( 1 ); } function shutdown_2() { exit( 2 ); } main( $argv );
With this PHP code, what do you expect is the resultant error level?
The answer is ‘1’. After main() calls exit( 0 ) the shutdown function shutdown_1() is invoked. When shutdown_1() calls exit( 1 ) the process exists and shutdown_2() is never called.
I’m glad we cleared that up. Back to bed.
I asked ChatGPT to format php datetime as “Fri 24 Mar 2023” and it “understood” what I meant and gave me the answer that I wanted! If that’s not intelligence I don’t know what is…
So this happened. Basically the first regex causes preg_match to fail when it tries to process 128KiB ASCII zeros… and 128KiB isn’t really that many zeros. Fortunately the second regex performs much better, PHP runs out of memory before preg_match chokes on that one.
------------------- Sat Apr 01 13:17:17 [bash:5.1.16 jobs:0 error:0 time:243] jj5@charm:/home/jj5/desktop/experiment $ cat base64-regex.php <?php test( '/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{4})$/' ); test( '/^[a-zA-Z0-9\/+]{2,}={0,2}$/' ); function test( $regex ) { echo "testing: $regex\n"; $n = 0; for ( ;; ) { $n++; echo "n: $n\n"; $string = str_repeat( '0', pow( 2, $n ) ); $base64 = base64_encode( $string ); if ( preg_match( $regex, $base64 ) ) { continue; } echo "error at N = $n.\n"; return; } } ------------------- Sat Apr 01 13:17:21 [bash:5.1.16 jobs:0 error:0 time:247] jj5@charm:/home/jj5/desktop/experiment $ php base64-regex.php testing: /^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{4})$/ n: 1 n: 2 n: 3 n: 4 n: 5 n: 6 n: 7 n: 8 n: 9 n: 10 n: 11 n: 12 n: 13 n: 14 n: 15 n: 16 n: 17 error at N = 17. testing: /^[a-zA-Z0-9\/+]{2,}={0,2}$/ n: 1 n: 2 n: 3 n: 4 n: 5 n: 6 n: 7 n: 8 n: 9 n: 10 n: 11 n: 12 n: 13 n: 14 n: 15 n: 16 n: 17 n: 18 n: 19 n: 20 n: 21 n: 22 n: 23 n: 24 n: 25 n: 26 n: 27 n: 28 n: 29 n: 30 n: 31 n: 32 n: 33 n: 34 n: 35 Killed ------------------- Sat Apr 01 13:18:21 [bash:5.1.16 jobs:0 error:137 time:307]
I’m working on a PHP encryption library called Kickass Crypto.
Wow. I just spent a few hours debugging a problem with my PHP file upload. Turns out the fix was to include an ‘id’ attribute on the file input element. Have no idea why that is required or works, but it does.
There’s some good info about common config options over here: Where Is php.ini, the PHP Configuration File?
The phpinfo()
function will tell you which php.ini file applies.
I stumbled upon phpSysInfo, a PHP library for system information. Haven’t actually used it, yet.