Rasmus Lerdorf
@rasmus
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define ishex(x) (((x) >= '0' && (x) <= '9') || ((x) >= 'a' && \
(x) <= 'f') || ((x) >= 'A' && (x) <= 'F'))
int htoi(char *s) {
int value;
char c;
c = s[0];
if(isupper(c)) c = tolower(c);
value=(c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10) * 16;
c = s[1];
if(isupper(c)) c = tolower(c);
value += c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10;
return(value);
}
void main(int argc, char *argv[]) {
char *params, *data, *dest, *s, *tmp;
char *name, *age;
puts("Content-type: text/html\r\n");
puts("<HTML><HEAD><TITLE>Form Example</TITLE></HEAD>");
puts("<BODY><H1>My Example Form</H1>");
puts("<FORM action=\"form.cgi\" method=\"GET\">");
puts("Name: <INPUT type=\"text\" name=\"name\">");
puts("Age: <INPUT type=\"text\" name=\"age\">");
puts("<BR><INPUT type=\"submit\">");
puts("</FORM>");
data = getenv("QUERY_STRING");
if(data && *data) {
params = data; dest = data;
while(*data) {
if(*data=='+') *dest=' ';
else if(*data == '%' && ishex(*(data+1))&&ishex(*(data+2))) {
*dest = (char) htoi(data + 1);
data+=2;
} else *dest = *data;
data++;
dest++;
}
*dest = '\0';
s = strtok(params,"&");
do {
tmp = strchr(s,'=');
if(tmp) {
*tmp = '\0';
if(!strcmp(s,"name")) name = tmp+1;
else if(!strcmp(s,"age")) age = tmp+1;
}
} while(s=strtok(NULL,"&"));
printf("Hi %s, you are %s years old\n",name,age);
}
puts("</BODY></HTML>");
}
use CGI qw(:standard);
print header;
print start_html('Form Example'),
h1('My Example Form'),
start_form,
"Name: ", textfield('name'),
p,
"Age: ", textfield('age'),
p,
submit,
end_form;
if(param()) {
print "Hi ",em(param('name')),
"You are ",em(param('age')),
" years old";
}
print end_html;
<html><head><title>Form Example</title></head>
<body><h1>My Example Form</h1>
<form action="form.phtml" method="POST">
Name: <input type="text" name="name">
Age: <input type="text" name="age">
<br><input type="submit">
</form>
<?if($name):?>
Hi <?echo $name?>, you are <?echo $age?> years old
<?endif?>
</body></html>
Story Time
SELECT * FROM logins
mSQL 1.0 Patch 9 20 - Oct - 1995
---------------------------------------
o Fixed close of stdin in keyFD handling (new key code)
o Fixed memory leak in the client library
o Fixed force close of UNIX socket when over-ridden by the
environment variable
o Fixed a couple of core dumps introduced by the new key code
o Replace sorting code with a new algorithm (mmap only)
o Fixed bug in msqlUpdate() that reported a duplicate key error
if you updated the key field to the same value.
o Merged in the LIMIT patch from Rasmus Lerdorf <rasmus@io.org>
o Fixed locale problem with LANG make variable
o Inlined a few calls to bcopy and bcmp for performance
o Removed libmisc.a All misc finctions are now in libmsql.a
o Fixed problem with field definition info (eg IS_PRI_KEY) being
destroyed after a join.
o Added a msql_tmpnam() function to get around the NeXT's
problem with it's own tmpnam().
Focus on the Ecosystem
✔ engine improvements
Improve CPU cache usage
$a = [];
for($i=0; $i < 100000;$i++) {
$a[] = ['abc','def','ghi','jkl','mno','pqr'];
}
echo memory_get_usage(true);
// PHP 5.x 109M
// PHP 7.0 42M no opcache
// PHP 7.0 6M with opcache enabled
GCC Feedback-Directed Optimization (FDO)
$ gcc --version
gcc (Debian 6.3.0-14) 6.3.0 20170415
$ make clean
$ make -j8 prof-gen
...
$ sapi/cgi/php-cgi -T 1000 /var/www/wordpress/index.php > /dev/null
$ make prof-clean
$ make -j8 prof-use
At 5% Adoption
At 100% Adoption
void return type
function should_return_nothing(): void {
return 1; // Fatal error: A void function must not return a value
}
Nullable types
function answer(int $a, ?int $b): ?int {
if($a > $b) return $a;
else return null;
}
Iterable pseudo-type
function foo(iterable $iterable) {
foreach ($iterable as $value) {
// ...
}
}
function bar(): iterable {
return [1, 2, 3];
}
Negative string offsets
$str='abcdef';
var_dump($str[-2]); // => string(1) "e"
List keys
list($first, $second, $third) = [1, 2, 3];
[$first, $second, $third] = [1, 2, 3];
Warn about invalid strings in arithmetic
$numberOfApples = "10 apples" + "5 pears";
Notice: A non well formed numeric string encountered in example.php on line 2
Notice: A non well formed numeric string encountered in example.php on line 2
Class constant visibility
class Token {
// Constants default to public
const PUBLIC_CONST = 0;
private const PRIVATE_CONST = 0;
protected const PROTECTED_CONST = 0;
public const PUBLIC_CONST_TWO = 0;
Things that may break your code
It can catch dumb mistakes
$a = [1,2,3];
if(count($a > 1)) {
echo "Test";
}
% phan test.php
test.php:2 PhanTypeComparisonFromArray array to int comparison
Check phpdoc comments
class C {
/** @var int $prop */
public $prop;
/**
* @param string $arg
* @return int
*/
function test($arg) {
$this->prop = $arg;
return $arg;
}
}
% phan test.php
test.php:10 PhanTypeMismatchProperty Assigning string to property but \C::prop is int
test.php:11 PhanTypeMismatchReturn Returning type string but test() is declared to return int
Help with refactoring
class C {
/**
* @deprecated
*/
static function legacy_function() { }
}
C::legacy_function();
% phan test.php
test.php:8 PhanDeprecatedFunction Call to deprecated function \C::legacy_function() defined at test.php:5
Install with composer
$ composer require --dev etsy/phan
Create .phan/config.php
<?php
use \Phan\Issue;
return [
'should_visit_all_nodes' => true,
'minimum_severity' => Issue::SEVERITY_LOW,
'directory_list' => [ 'src', 'vendor' ],
'exclude_analysis_directory_list' => [ 'vendor' ]
];
$ ./vendor/bin/phan
Type Safety
Legacy code
class Data {
function __construct($data) {
$this->haystack = $data;
}
function find($needle) {
return in_array($needle, $this->haystack, true);
}
}
$storage = new Data(['apple','orange','banana']);
$fruit = false;
$storage->find($fruit);
Going straight to strict types risks runtime fatals
<?php declare(strict_types=1);
class Data {
function __construct(array $data) {
$this->haystack = $data;
}
function find(string $needle):bool {
return in_array($needle, $this->haystack, true);
}
}
$storage = new Data(['apple','orange','banana']);
$fruit = false;
$storage->find($fruit);
Fatal error: Uncaught TypeError: Argument 1 passed to Data::find() must be of the type string, boolean given,
called in test.php on line 13 and defined in test.php:6
Stack trace:
#0 test.php(13): Data->find(false)
#1 {main}
thrown in test.php on line 6
Intermediate step
class Data {
/** @var array $haystack */
public $haystack;
/**
* @param array $data
*/
function __construct($data) {
$this->haystack = $data;
}
/**
* @param string $needle
* @return bool
*/
function find($needle) {
return in_array($needle, $this->haystack, true);
}
}
$storage = new Data(['apple','orange','banana']);
$fruit = false;
$storage->find($fruit);
$ phan test.php
test.php:22 PhanTypeMismatchArgument Argument 1 (needle) is bool but \Data::find() takes string defined at test.php:15
Report Bugs
Useful bug reports, please!