0
0

More than 1 year has passed since last update.

過去の自分のコードを修正してみる - PHP カレンダー編①

Last updated at Posted at 2022-08-23

これは何?

PHP処女作のコードの一部を、ある程度PHPに触れた今の私がツッコミ入れていく記事です。
PHPで4か月分×3段の年間カレンダーを作った時のコードの抜粋です
何回かに分けて投稿します…私が自分のコード読むの疲れるので

①条件に合わせてテーブルにあてるスタイルを変えたかった


<?php
// $t…その段のなかでいくつ目の月なのか的な変数だと思う[後付けコメント]
if ($t == 0) {
  print "<table border='0' width='100%' align='center' class='calender_gyou'>\n<tr>\n<td>\n<div style='float:left;margin-right:20px;margin-bottom:8px;'>";
  $t++;
}

は?

①-1 絶対わざわざprintしなくていい


<?php
if ($t == 0) {
?>
  <table border='0' width='100%' align='center' class='calender_gyou'>\n<tr>\n<td>\n<div style='float:left;margin-right:20px;margin-bottom:8px;'>
<?php
  $t++;
}

当時はこんな書き方できるって知りませんでした…
リファレンスのめっちゃ最初に書いてあるのにね

①-2 \n要らなくないか


<?php
if ($t == 0) {
?>
  <table border='0' width='100%' align='center' class='calender_gyou'>
    <tr>
      <td>
        <div style='float:left;margin-right:20px;margin-bottom:8px;'>
<?php
  $t++;
}

なんで\n要ると思ってたんだこれ
さすがにこの時の思考回路は覚えてない

border要素とかstyle要素使うくらいならCSSファイルに書こうぜ


<?php
if ($t == 0) {
?>
  <table class="calender_gyou first-table">
    <tr>
      <td>
        <div class="div-of-first-table">
<?php
  $t++;
}


.first-table {
  border-style: none;
  width:        100%;
  text-align:   center; 
}
.div-of-first-table {
   float:       left;
   margin-right:20px;
   margin-bottom:8px;
}

このコード書いていた時点でborder要素はそもそも廃止されています
VSCodeでも赤文字だったのになんでそのまま通したんだろうね

なんでこんなことになったのか

  • 当時参考にしたサイトの各種バージョンが古かったのを全く確認していなかった
  • リファレンスを読むという発想がなかった
  • とりあえず動けばいいや精神だった

いやとりあえず動くものを作るのは大事だけどもこれは…
自分で見ててめまいがしました。
まあでもほぼ独学でPHPでカレンダー書いたら割とこうなるのかもしれない…ならないか?

突っ込み入れれるようになっただけ成長したと取ることはできるわね
気力があれば続きます

0
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
0
0