About significant changes in PHP 7.3

The following changes in PHP 7.3

– Flexible Heredoc syntax and Nowdoc syntax
– End comma can be used in function call JSON_THROW_ON_ERROR
– Can use reference passing with list () is_countable function
– array_key_first(), array_key_last()
– Argon2 Password Hash Enhancement

Flexible description of heredoc/nowdoc statement
You can now indent the end markers and the text you insert.

if(TRUE){
	print<<<EOS
	foo
	EOS;
}
&#91;/php&#93;

The indent of the string is determined relative to the end marker's position(indent). This allows you to easily control the number of spaces inserted etc. Note that mixing blanks and tabs in a string is not permitted, as it is controlled by indenting, and an error will occur.

<b>mbstring case conversion is enhanced</b>
The mbstring extension was originally developed to handle multi-byte strings, but it is actually used by many PHP programmers, including single-byte users, as extensions for string processing. As of PHP 7.3, upper and lower-case conversion mainly used in single-byte range is enhanced.


echo mb_strtoupper("Straße");

Allow commas to be inserted at the end of function arguments
Previously, in PHP 7.2 and earlier versions, although it was possible to write a comma at the end of the last element of the array, it was not possible to insert a comma at the end of function arguments. For example, when creating a function definition automatically by a script, putting a comma at the end may make writing code easier. In such cases, new features are useful.

$a = [1,2];
$b = [3,4];
$c = array_merge($a,$b,[5,6],);

is_countable(), array_key_*() function added
As of PHP 7.3, the is_countable() function has been added to check for countable functions. It can be used for purposes such as checking the functions that can be specified in the count() function.

if(is_countable($foo)){
	//$fooはカウント可
}

$a = ['one'=>'First','two'=>'Second','three'=>'Thred'];
echo array_key_first($a); // one
echo array_key_last($a); // three

Support for Same-site Cookie
A PHP session supports the Same-site Cookie(suggested as RFC 6265) that is said to be effective for cross-site request forgery(CSRF) protection as a security related function imrovement. When the SameSite attribute is specified, corss-site requests are limited and supported by major we browsers such as Google Chrome.

zipからダウンロード

set_time_limit(0);

$zip_name = 'create_zip_'.date('Ymd').'.zip';
$zip_tmp_dir = dirname(__FILE__).'/tmp_zip/'; 

$zip_obj = new ZipArchive();

$result = $zip_obj -> open($zip_tmp_dir.$zip_name, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
if(!$result){
    echo 'error code : '.$result;
    exit();
}

$files[] = [
	'zip_path' => 'building.jpg',
	'file_path' =>  dirname(__FILE__)."/".'building.jpg'
];

foreach ($files as $file)
{
    //ファイルを追加する場合
    $zip_obj -> addFile($file['file_path'], $file['zip_path']);
}

$zip_obj -> addFromString('test.txt', 'test');
$zip_obj -> close();

header('Content-Type: application/force-download;');
header('Content-Length: '.filesize($zip_tmp_dir.$zip_name));
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_tmp_dir.$zip_name);

$files[] = [
	'zip_path' => ['building.jpg', 'monkey.jpg'],
	'file_path' =>  ['building.jpg', 'monkey.jpg']
];

foreach ($files as $file)
{
    //ファイルを追加する場合
    $zip_obj -> addFile( dirname(__FILE__)."/".$file['file_path'], $file['zip_path']);
}

[Sun Apr 7 16:33:50 2019] PHP Notice: Undefined index: /home/vagrant/local/app/test/file_path in /home/vagrant/local/app/test/index.php on line 24
[Sun Apr 7 16:33:50 2019] PHP Warning: ZipArchive::addFile() expects parameter 2 to be string, array given in /home/vagrant/local/app/test/index.php on line 24

うん、まー要するに配列で渡せば、zipで複数ダウンロードできるってわけね。
チェックボックスなら、POSTで対象ファイルを渡せばOK

phpのzip機能

Check if PHP has Zip extension

[vagrant@localhost test]$ php -m
[PHP Modules]
bz2
calendar
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gd
gettext
hash
iconv
intl
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
readline
Reflection
session
SimpleXML
sockets
SPL
sqlite3
standard
tokenizer
wddx
xml
xmlreader
xmlwriter
xsl
zip
zlib

うん、ありますね。

phpでファイルダウンロード

$file_name = "building.jpg";
$file_path = dirname(__FILE__)."/".$file_name;

$download_file_name = "building.jpg";

header('Content-Type: application/force-download;');
header('Content-Length: '.filesize($file_path));
header('Content-Disposition: attachment; filename="'.$file_name.'"');

readfile($download_file_name);

特に問題なし

うーん、複数のファイルをDLしたいです。

メールの型チェック

$var="hoge@gmail.com";
if(filter_var($var, FILTER_VALIDATE_EMAIL)){
	echo $var . "正しいメール<br>";
} else {
	echo $var . "不正のメール<br>";
}

こちらでも、できますねー

$var="hoge@gmail.com";
if(preg_match('/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/iD', $var)){
	echo $var . "正しいメール<br>";
} else {
	echo $var . "不正のメール<br>";
}

URLの型チェック

filter_var: filter data by specified filter

$var="https://www.google.com/";
if(filter_var($var, FILTER_VALIDATE_URL)){
	echo $var . "正しいURL<br>";
} else {
	echo $var . "不正のURL<br>";
}

時間型のチェック

$var="10:53";
if($var === date('H:i', strtotime($var))){
	echo $var . "正しい時間型<br>";
} else {
	echo $var . "不正の時間型<br>";
}

なんか、くだらないなーと思うのは私だけ??

日付の型チェック

$var="2019/2/19";
if(preg_match('/^([1-9][0-9]{3})\/([1-9]{1}|1[0-2]{1}|[0][1-9])\/([1-9]{1}|[1-2]{1}[0-9]{1}|3[0-1]{1}|[0][1-9])$/', $var)){
	echo $var . "正しい日付<br>";
} else {
	echo $var . "不正の日付<br>";
}

ただ、これvarが2018/2/29でも、2019/2/29でも正しいって出てしまうし、30日しかない月でも31日って入力されてもOKになってしまうんだよね。

まーライブラリーであるんだろうけど。ロジックとしてはわかりました。

How to specify envelop-from with PHP

To specify envelop-from in mail function, it is specified in $additional_parameters of the fourth argument.

$to = 'info@master.com';
$subject = 'test mail';
$msg = "hogehoge";
$header = "From: sender@sample.com";
$opt = 'sender@yahoo.co.jp';
$r = mail($to, $subject, $msg, $headers, $opt);
var_dump($r);

Writing Reply-to for mail sending

1.From, 2.Reply-to, 3.CC, 4.BCC, 5.Reply-to

mb_language("Japanese");
mb_internal_encoding("UTF-8");

$email = "xxx@example.com";
$subjet = "test";
$body = "this is test \n";
$to = 'yyy@example.com';
$header = "From: $email\nReplay-To: $email\n";

mb_send_mail($to; $subject, $body, $header);