1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PHP入門メモ

Last updated at Posted at 2021-06-11

PHPで色々と触ったので使ったもののメモを

基本

php
<?php
//<?php> <?>で囲む
//行末に;
------------------------------------------
//変数は頭に$をつけて宣言する
$num = 15;
$str = "apple";
------------------------------------------
//echoとつけるとhtmlとして出力される
echo "<b>Hello, World!</b>";
------------------------------------------
//POSTされたものを受け取る
$number = @$_POST["number"]; //numberでPOSTされてる時
------------------------------------------
//変数の中身の長さが0or有るかを見る
//上記の応用でフォームを受け取れるかに使う
if (!strlen($number)) {

}
------------------------------------------
//配列の宣言
//辞書みたいにも出来る
$list = [];
$dict = ["country" => "japan", "city" => "tokyo"]; //$dict["country"]で"japan"と出力される
------------------------------------------
//乱数作成
$rand_num = mt_rand(1,99); //1~99までの乱数が作成される
------------------------------------------
//for文
//javascriptと同じ
$n = 10
for($i = 0; $i < n; $i++){

}
------------------------------------------
//配列をシリアライズ、アンシリアライズ
//どこかにpostする時は一旦シリアライズしてから送り先で戻す
$seri_li = serialize($list);
$un_seri_li = unserialize($seri_li);
------------------------------------------
//配列の長さを取得
$count = count($list);
------------------------------------------
//配列にappend
array_push($追加する配列名,何かしらの値);
------------------------------------------
//csvを取得し配列に格納
$csv_file = file_get_contents('txt/test.csv');
//変数を改行毎に配列に変換
$aryHoge = explode("\n", $csv_file);
$aryCsv = [];
foreach($aryHoge as $key => $value){
 if(!$value) continue; //空白を除外する
 $aryCsv[] = explode(",", $value);
    }
------------------------------------------
//pythonみたいなfor文
$li = [1,2,3]
foreach ($li as $i){
   echo($i)
}
//1
//2
//3
------------------------------------------

受け取った配列をcsv出力

重要なのはhtmlタグ前に書く

php
<?php
//csvと言う名のPOSTを受け取っていた時
function export($file_name, $data)
{
    $fp = fopen('php://output', 'w');

    foreach ($data as $row) {
        fputcsv($fp, $row, ',', '"');
    }
    fclose($fp);
    header('Content-Type: application/octet-stream');
    header("Content-Disposition: attachment; filename={$file_name}");
    header('Content-Transfer-Encoding: binary');
    exit;
}
?>
<?php
if (isset($_POST['csv'])) {
    $get_csv = $_POST['csv'];
    $csv_list = unserialize($get_csv);
    //var_dump($csv_list);
    export('test.csv',$csv_list);
}
header("location:javascript://history.go(-1)");
?>

<html>
<!--以下html-->

前のページにリダイレクト

php
header("location:javascript://history.go(-1)");

<?= ?><?php echo(???) ?>を代替する

そのままの意味

<?=
   "hello world"
?>

//hello world
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?