Skip to content

Instantly share code, notes, and snippets.

Avatar

Michihito Shigemura shigemk2

View GitHub Profile
@shigemk2
shigemk2 / array_keys_vs_array_search.php
Created Feb 23, 2013
array_searchとarray_keysの挙動の違いについて
View array_keys_vs_array_search.php
<?php
// 指定した要素に該当する配列のキーを配列で返す
$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));
// 指定した要素に該当する配列の最初のキーを返す(数字で)
$array = array("blue", "red", "green", "blue", "blue");
print_r(array_search("red", $array) . PHP_EOL);
var_dump(array_search("red", $array));
@shigemk2
shigemk2 / public_vs_private.php
Created Feb 23, 2013
publicとprivateの違い
View public_vs_private.php
<?php
class Hoge {
public $fuga = 'fuga';
private $bar = 'bar';
function getBar() {
return $this->bar;
}
}
@shigemk2
shigemk2 / wrong_here_document.php
Created Feb 23, 2013
間違ったヒアドキュメンの使いかた
View wrong_here_document.php
<?php
$hoge = <<<EOT
HOGE
EOT; // ここにスペースとかあるとうまく作動しない
echo $hoge;
@shigemk2
shigemk2 / here_document_on_class.php
Created Feb 23, 2013
ヒアドキュメントでクラスのメソッドを使おう
View here_document_on_class.php
<?php
class Hoge {
function fuga() {
return 'FUGA';
}
}
$hoge = new Hoge();
$str = <<<EOT
{$hoge->fuga()}
@shigemk2
shigemk2 / datetime_format_a.php
Created Feb 23, 2013
datetime型 format %a は経過日数を調べる
View datetime_format_a.php
<?php
$date1 = new Datetime('2012-02-01');
$date2 = new Datetime();
$array = $date1->diff($date2);
var_dump($array->format('%a') > 40);
@shigemk2
shigemk2 / parse_url.php
Created Feb 23, 2013
parse_urlを使おう
View parse_url.php
<?php
$url = 'http://d.hatena.ne.jp/shigemk2/20121121/1353496855';
print_r(parse_url($url));
echo parse_url($url);
/*
* Array
* (
@shigemk2
shigemk2 / before_after.html
Created Feb 23, 2013
疑似要素をz-indexしてれぅ
View before_after.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>HOGE</title>
<style type="text/css">
body {
background-color: rgba(255,0,0,.5);
}
div.hoge {
@shigemk2
shigemk2 / date_interval.php
Created Feb 23, 2013
DateIntervalを使ってみよう
View date_interval.php
<?php
$date = new DateTime('2000-01-20');
$date->sub(new DateInterval('P12M')); // 12ヶ月前
echo $date->format('Y-m-d') . "\n"; // 1999-01-20
@shigemk2
shigemk2 / datetime.php
Created Feb 23, 2013
DateTimeを使ってみよう
View datetime.php
<?php
$t = new DateTime();
print_r($t);
/*
DateTime Object
(
[date] => 2013-01-13 14:54:37
[timezone_type] => 3
[timezone] => Asia/Tokyo
)
View slice.js
console.log('ABCDEFGHIJKLMNOP'.slice(-5)); // LMNOP
console.log('ABCDEFGHIJKLMNOP'.slice(-10)); // GHIJKLMNOP