[an error occurred while processing this directive]
[an error occurred while processing this directive]$A[0] = "abc"; $A[10] = "def";のとき、
in_array(10, $A)
array_key_exists("def", $A)
は共にtrueになる。
<?php
$a = "abc";
$A = array("abc", "def");
print in_array($a, $A);
?>
その一方で、arrayに値をpushするときは
<?php $a = "abc"; $A = array(); print array_push($A, $a); ?>なぜ順番が逆なんだろう… a in Aの順番にあわせたのかな?
<?php
$pid = pcntl_fork();
if ($pid < 0) {
die(1);
} elseif($pid) {
print "Parent!\n";
pcntl_wait($s);
} else {
print "Child!\n";
}
?>
<?php $s = '縺ゅ≠'; print mb_convert_encoding($s,"shift_jis","utf-8"); ?>としても正しいshift_jisには戻らないから、もうutf-8の「ああ」としての情報は失われてしまっているっぽい。
[データの例] kei,hoge@hoge.com mei,fuga@hoge.com rei,piyo@hoge.com
if (!$stdin) {
die("Error\n");
}
$A=array();
for(;;){
$line = trim(fgets($stdin));
if($line == "") break;
list($id, $email) = split(',', $line, 2);
print $id . "," . $email . "\n";
}
print crypt("hello", "seed");
print md5("hello");
$fp = fopen("hoge.txt", "w");
for($i = 0; $i < 10; $i++){
fputs($fp, "hello");
}
fclose($fp);
list($a, $b) = split('\|', 's|d', 2);
print $a . " " . $b . "\n" ;
Fatal error: Call to undefined function: sqlite_open() in ...てエラー。
extension=sqlite.soを追加して解決。
<?php
$fp = popen("last", "r");
while(!feof($fp)){
$ret = fgets($fp);
print $ret;
}
$ret = pclose($fp);
print "return status : ". $ret;
?>
<?php
for($i = 0; $i < 3; $i++){
$email = "kei@hoge.com";
$subject = getRandomString(10);
$text = getRandomString(100) . "\n";
$text .= getRandomString(100) . "\n";
$text .= getRandomString(100) . "\n";
$text .= getRandomString(100) . "\n";
$text .= getRandomString(100) . "\n";
$text .= getRandomString(100) . "\n";
print $subject . "\n";
sendmail($email, $subject , $text);
sleep(1);
}
function sendmail($to, $subject, $body){
mb_internal_encoding("euc-jp");
mb_language("Japanese");
$s = mb_encode_mimeheader(mb_convert_encoding($subject, 'JIS', 'euc-jp'));
mb_send_mail($to, $subject, $body);
}
function getRandomString($nLengthRequired = 8){
$sCharList = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_ ";
mt_srand();
$sRes = "";
for($i = 0; $i < $nLengthRequired; $i++)
$sRes .= $sCharList{mt_rand(0, strlen($sCharList) - 1)};
return $sRes;
}
?>
if($a == True){
} elseif { /* elsif や else if ではない! */
}
if($flag) print "hoge"; // 一行if分も書ける
$A = array(); $A[] = "hoge"; // あいているところに"hoge"を追加 array_push($A, "hoge"); // これは末尾に追加
mail("hoge@fuga.com","subject","body");
class A{
var $x;
function A(){ // コンストラクタ
$this->x = 10;
}
function print(){
print "value : " . $this->x;
}
}
$a = new A();
if($a != null){
$a->print();
}
php postgresql php-pgsqlpear自体は標準でPHPに入っているんだけど、postgresと接続するためのライブラリは入っていないので、 php-pgsqlを別に入れる。
Fatal error: Failed opening required 'DB.php' (include_path='.:/usr/share/pear') in ...みたいなエラーが出たら、言われた通りph.iniとかでインクルードパスを設定する。
require_once 'DB.php';
$dsn = array(
'phptype' => 'pgsql',
'dbsyntax' => 'pgsql',
'username' => 'ユーザー名',
'protocol' => 'unix',
'database' => 'データベース名'
);
$db = DB::connect($dsn);
if (DB::isError($db)) {
die($db->getMessage());
}
DB Error: cannot find databaseというエラーが出たときは、postgres-phpみたいなパッケージが入ってるか確認。 (これ、データベース名が間違っているとかいうエラーではないみたいです)
DB Error: connect failedというエラーの時は、データベース名とかを確認。
$str1 = "Hello"; $str2 = $str1; // コピー $str3 = &$str1; // 参照の代入
$A = array(1, 2, 3, 4, 5); print_r($A); $B = &$A; $A[] = 6; // 末尾に6を追加。 print_r($B);
$A = array('kei', 'rei', 'may');
foreach ($A as $index => $value) {
echo " $index : $value \n";
}
あ、perlと違って一行ループもいいようですね。
foreach ($A as $index => $value) echo " $index : $value \n";普通っぽく「配列の先頭から最後まで」と書くには、関数countを使う。
for($i = 0; $i < count($A); $i++){
echo $A[$i];
}
$str = 'kei';
if($str == 'kei'){
...
if ($hoge == 'fuga'){
...
}elseif {
}
echo $GET['hoge'];とする。
<input type=checkbox name="name" value="kei" checked>kei <input type=checkbox name="name" value="mei" checked>mei <input type=checkbox name="name" value="rei" checked>reiを送信すると、
name=kei&name=mei&name=reiというデータが送信されるが、$_GET['name']では最後のreiしか取得できない。
<input type=checkbox name="name[]" value="kei" checked>kei <input type=checkbox name="name[]" value="mei" checked>mei <input type=checkbox name="name[]" value="rei" checked>reiと、属性名に[]を付けておくと、$_GET['name']で配列が取得できる。 たとえばこんな風に。
foreach($_GET['name'] as $i => $val) echo "$i : $val \n";
<?php
ini_set("include_path",ini_get("include_path"));
require_once 'DB.php';
$dsn = array(
'phptype' => 'pgsql',
'dbsyntax' => 'pgsql',
'username' => 'apache',
'protocol' => 'unix',
'database' => 'kei_test'
);
$db = DB::connect($dsn);
if (DB::isError($db)) {
die($db->getMessage());
}
?>
これで、あとは
$sql = "SELECT * FROM friends;";
$result = $db->query($sql);
while($row = $result->fetchRow()){
echo "$row[0] $row[1] \n";
}
とかなんとか。
<?php include 'header.php' ?>
header("Content-Type: text/csv; charset=shift_jis");
/* 拡張子をちゃんと付けてあげる */
header("Content-Disposition: attachment;filename=output.txt");
/* これはコードを勝手にサーバーが変えないように。 */
header("Content-Transfer-Encoding: binary");
(from 井谷さん)