Skip to content

Instantly share code, notes, and snippets.

@hdknr
Last active April 19, 2018 14:14
Show Gist options
  • Save hdknr/3c6dbfd9e696af0f6f60 to your computer and use it in GitHub Desktop.
Save hdknr/3c6dbfd9e696af0f6f60 to your computer and use it in GitHub Desktop.
PHP

もろもろ

文字数とか

全角チェック

<?php
    /**
     * @assert ('文字列です。') === true
     * @assert ('abcd1234+:;!') === false
     * @assert ('全角abcd') === false
     * @assert ('半角カナ') === false
     */
    function is_zen_preg_match ($str) {
        if (!preg_match("/(?:\xEF\xBD[\xA1-\xBF]|\xEF\xBE[\x80-\x9F])|[\x20-\x7E]/", $str)) {
            return true;
        } else {
            return false;
        }
    }

名前でメソッドを呼ぶ

<?php

class Checker {
    function check_name($value) {
        return false;
    }
}

$profile = array(
    'name' => 'my name',
    'phone' => '333-3333-3333'
);

$checker = new Checker();

function check($checker, $key, $value ){
    $method = "check_$key";
    if(method_exists($checker, $method)){
        return $checker->$method($value);
    }
    return true;
}

foreach($profile as $key => $value ){
    echo $key, ":", check($checker, $key, $value ), "\n";
}

map関数

<?php

$array = array('a' => 1, 'b' => 2, 'c' => 3);

$array2 = array_map(function($val){ return $val * 2 ; }, $array);

foreach($array as  $k => $v ){
    echo "$k .... $v\n";
}

foreach($array2 as  $k => $v ){
    echo "$k .... $v\n";
}
$ php tests_map.php
a .... 1
b .... 2
c .... 3
a .... 2
b .... 4
c .... 6

インスタンスクラスのstatic メンバーへのアクセス

$model_class = get_class($instance);
$value = $model_class::$static_value;

配列要素の削除

unset($array[$i]);

mysql

-接続確認

# /vagrant/php/vendor/bin/psysh
Psy Shell v0.5.2 (PHP 5.5.27 ― cli) by Justin Hileman
>>> (new mysqli('localhost', 'root', 'password'))->query('show databases')->fetch_all();

error

  • php.ini
display_errors = on
error_reporting = E_ALL

templates

TOP 5 PHP TEMPLATE ENGINES

  • Mustache
  • Blade Haml / Jadeインスパイア (Laravel)
  • Smarty
  • Twig Jinjaインスパイア (Symfony)
  • Volt Jinjaインスパイア (Phalcon)

PHP Warning: Unknown: It is not safe to rely on the system's timezone settings.

PHP Warning:  Unknown: It is not safe to rely on the system's timezone settings. 
You are *required* to use the date.timezone setting or the date_default_timezone_set() function.

In case you used any of those methods and you are still getting this warning, 
you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, 
but please set date.timezone to select your timezone. in Unknown on line 0
 # grep timezone $(phpenv prefix)/etc/php.ini
 ; Defines the default timezone used by the date functions
 ; http://php.net/date.timezone
 ;date.timezone =
# vim $(phpenv prefix)/etc/php.ini
# grep timezone $(phpenv prefix)/etc/php.ini
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
;date.timezone =
date.timezone = Asia/Tokyo

xdebug

ソースインストール

# cd $(phpenv root)
$ mkdir src && cd src
# git clone https://github.com/derickr/xdebug.git
Initialized empty Git repository in /root/.anyenv/envs/phpenv/src/xdebug/.git/
remote: Counting objects: 14485, done.
remote: Total 14485 (delta 0), reused 0 (delta 0), pack-reused 14485
Receiving objects: 100% (14485/14485), 6.36 MiB | 732 KiB/s, done.
Resolving deltas: 100% (8790/8790), done.


# cd xdebug/

# phpize
Configuring for:
PHP Api Version:         20121113
Zend Module Api No:      20121212
Zend Extension Api No:   220121212

# which php-config
/root/.anyenv/envs/phpenv/shims/php-config

# ./configure --enable-xdebug

# make

# find . -name "*.so" -print
./.libs/xdebug.so
./modules/xdebug.so

phpenv install でインストールされてた

# cd $(phpenv root)/versions/5.6.11
# more etc/conf.d/xdebug.ini
zend_extension="/root/.anyenv/envs/phpenv/versions/5.6.11/lib/php/extensions/no-debug-non-zts-20131226/xdebug.so"

# ls -al /root/.anyenv/envs/phpenv/versions/5.6.11/lib/php/extensions/no-debug-non-zts-20131226
合計 2648
drwxr-xr-x 2 root root    4096  8月  6 15:36 2015 .
drwxr-xr-x 3 root root    4096  8月  6 15:36 2015 ..
-rwxr-xr-x 1 root root 1104832  8月  6 15:36 2015 opcache.a
-rwxr-xr-x 1 root root  586944  8月  6 15:36 2015 opcache.so
@hdknr
Copy link
Author

hdknr commented Jul 17, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment