<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/">
  <channel>
    <title>PHP｜林鼎淵 Dean Lin</title>
    <link>https://deanlin.net/tags/php/</link>
    <image><url>https://deanlin.net/images/author_hu_aa256fc934e9a932.jpg</url><title>PHP｜林鼎淵 Dean Lin</title><link>https://deanlin.net/tags/php/</link><width>144</width><height>144</height></image>
    <description>林鼎淵（Dean Lin）的部落格：生成式 AI 企業內訓講師、外商資安公司軟體專家、7 本書作者。400 多篇文章用白話分享生成式 AI 實戰、Claude Code 與 Vibe Coding 教學、團隊合作與職場成長。</description>
    <generator>Hugo</generator>
    <language>zh-tw</language>
    <copyright>© 2020–2026 林鼎淵 Dean Lin</copyright>
    <lastBuildDate>Wed, 18 Oct 2023 11:25:57 +0800</lastBuildDate>
    <atom:link href="https://deanlin.net/tags/php/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>PHP 陣列操作：那些讓你意外的小陷阱(array_unique、array_filter)</title>
      <link>https://deanlin.net/blog/202310/php-array-pitfalls/</link>
      <pubDate>Wed, 18 Oct 2023 11:25:57 +0800</pubDate>
      <guid>https://deanlin.net/blog/202310/php-array-pitfalls/</guid>
      <dc:creator>林鼎淵</dc:creator>
      <category>技術教學</category>
      <description>我發現許多從其他程式語言轉來 PHP 的開發者，經常會在操作陣列時碰到意料之外的狀況（我就是受害者）。今天我要來談談 PHP 中的 array_unique 和 array_filter 函數，以及它們的行為與你想像中可能存在的差異。</description>
      <media:content url="https://deanlin.net/blog/202310/php-array-pitfalls/images/php-array-pitfalls-og_hu_cf8472b68b605288.jpg" medium="image" type="image/jpeg" width="1200" height="675" />
      <content:encoded><![CDATA[<p>我發現許多從其他程式語言轉來 PHP 的開發者，經常會在操作陣列時碰到意料之外的狀況（我就是受害者）。今天我要來談談 PHP 中的 <code>array_unique</code> 和 <code>array_filter</code> 函數，以及它們的行為與你想像中可能存在的差異。</p>
<p><img src="https://deanlin.net/blog/202310/php-array-pitfalls/images/php-array-pitfalls-01.webp" width="1056" height="590" alt="PHP 陣列操作：那些讓你意外的小陷阱(array_unique、array_filter)（圖 1）" fetchpriority="high" decoding="async"></p>
<h2 class="h-nomark" id="-array_filter-的索引行為">🎭 Array_filter 的索引行為<a class="h-anchor" href="#-array_filter-%e7%9a%84%e7%b4%a2%e5%bc%95%e8%a1%8c%e7%82%ba" aria-label="這個段落的連結"></a></h2>
<p>在 JavaScript 的世界中，我們用 <code>filter</code> 來過濾陣列，結果非常直觀。</p>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#ff7b72">let</span> arr <span style="color:#ff7b72;font-weight:bold">=</span> [<span style="color:#a5d6ff">1</span>, <span style="color:#a5d6ff">2</span>, <span style="color:#a5d6ff">3</span>, <span style="color:#a5d6ff">4</span>];
</span></span><span style="display:flex;"><span><span style="color:#ff7b72">let</span> filteredArr <span style="color:#ff7b72;font-weight:bold">=</span> arr.filter(value =&gt; value <span style="color:#ff7b72;font-weight:bold">&gt;</span> <span style="color:#a5d6ff">2</span>);
</span></span><span style="display:flex;"><span>console.log(filteredArr); <span style="color:#8b949e;font-style:italic">// 看吧！直接返回 [3, 4]
</span></span></span></code></pre></div><p>但換到 PHP，<code>array_filter</code> 卻有點不一樣。使用它之後，你會發現原始的索引還在：</p>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-php" data-lang="php"><span style="display:flex;"><span><span style="color:#79c0ff">$arr</span> <span style="color:#ff7b72;font-weight:bold">=</span> <span style="color:#ff7b72">array</span>(<span style="color:#a5d6ff">1</span>, <span style="color:#a5d6ff">2</span>, <span style="color:#a5d6ff">3</span>, <span style="color:#a5d6ff">4</span>);
</span></span><span style="display:flex;"><span><span style="color:#79c0ff">$filteredArr</span> <span style="color:#ff7b72;font-weight:bold">=</span> array_filter(<span style="color:#79c0ff">$arr</span>, <span style="color:#ff7b72">function</span>(<span style="color:#79c0ff">$value</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#ff7b72">return</span> <span style="color:#79c0ff">$value</span> <span style="color:#ff7b72;font-weight:bold">&gt;</span> <span style="color:#a5d6ff">2</span>;
</span></span><span style="display:flex;"><span>});
</span></span><span style="display:flex;"><span>print_r(<span style="color:#79c0ff">$filteredArr</span>); <span style="color:#8b949e;font-style:italic">// 喔喔！結果是 Array ( [2] =&gt; 3 [3] =&gt; 4 )
</span></span></span></code></pre></div><p><strong>這意味著如果你嘗試在過濾後的陣列上取得 <code>$filteredArr[0]</code>，會得到一個未定義的索引錯誤。</strong></p>
<hr>
<h2 class="h-nomark" id="-array_unique-的特殊之處">🎩 Array_unique 的特殊之處<a class="h-anchor" href="#-array_unique-%e7%9a%84%e7%89%b9%e6%ae%8a%e4%b9%8b%e8%99%95" aria-label="這個段落的連結"></a></h2>
<p><code>array_unique</code> 函數在 PHP 中用於移除陣列中的重複值。</p>
<p>但小心！它也不會重新排列索引：</p>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-php" data-lang="php"><span style="display:flex;"><span><span style="color:#79c0ff">$arr</span> <span style="color:#ff7b72;font-weight:bold">=</span> <span style="color:#ff7b72">array</span>(<span style="color:#a5d6ff">1</span>, <span style="color:#a5d6ff">2</span>, <span style="color:#a5d6ff">2</span>, <span style="color:#a5d6ff">3</span>, <span style="color:#a5d6ff">3</span>, <span style="color:#a5d6ff">4</span>);
</span></span><span style="display:flex;"><span><span style="color:#79c0ff">$uniqueArr</span> <span style="color:#ff7b72;font-weight:bold">=</span> array_unique(<span style="color:#79c0ff">$arr</span>);
</span></span><span style="display:flex;"><span>print_r(<span style="color:#79c0ff">$uniqueArr</span>); <span style="color:#8b949e;font-style:italic">// 哇！這是什麼魔法？Array ( [0] =&gt; 1 [1] =&gt; 2 [3] =&gt; 3 [5] =&gt; 4 )
</span></span></span></code></pre></div><p>同樣的，如果你直接取得 <code>$uniqueArr[2]</code>會讓你再次掉入那恐怖的陷阱中！</p>
<hr>
<h2 class="h-nomark" id="-如何應對">🌈 如何應對？<a class="h-anchor" href="#-%e5%a6%82%e4%bd%95%e6%87%89%e5%b0%8d" aria-label="這個段落的連結"></a></h2>
<p>為了避免這些問題，建議在使用這些函數後，用 <code>array_values</code> 函數重設索引：</p>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-php" data-lang="php"><span style="display:flex;"><span><span style="color:#79c0ff">$arr</span> <span style="color:#ff7b72;font-weight:bold">=</span> <span style="color:#ff7b72">array</span>(<span style="color:#a5d6ff">1</span>, <span style="color:#a5d6ff">2</span>, <span style="color:#a5d6ff">3</span>, <span style="color:#a5d6ff">4</span>);
</span></span><span style="display:flex;"><span><span style="color:#79c0ff">$filteredArr</span> <span style="color:#ff7b72;font-weight:bold">=</span> array_filter(<span style="color:#79c0ff">$arr</span>, <span style="color:#ff7b72">function</span>(<span style="color:#79c0ff">$value</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#ff7b72">return</span> <span style="color:#79c0ff">$value</span> <span style="color:#ff7b72;font-weight:bold">&gt;</span> <span style="color:#a5d6ff">2</span>;
</span></span><span style="display:flex;"><span>});
</span></span><span style="display:flex;"><span><span style="color:#79c0ff">$reIndexedArr</span> <span style="color:#ff7b72;font-weight:bold">=</span> array_values(<span style="color:#79c0ff">$filteredArr</span>);
</span></span><span style="display:flex;"><span>print_r(<span style="color:#79c0ff">$reIndexedArr</span>); <span style="color:#8b949e;font-style:italic">// Array ( [0] =&gt; 3 [1] =&gt; 4 )
</span></span></span></code></pre></div><p>這樣，你就可以放心地存取 <code>$reIndexedArr[0]</code> 而不必擔心會遭遇未定義的索引。</p>
<hr>
<h2 class="h-nomark" id="-總結">🎉 總結<a class="h-anchor" href="#-%e7%b8%bd%e7%b5%90" aria-label="這個段落的連結"></a></h2>
<p>雖然 PHP 的陣列操作函數在某些方面與其他語言有所不同，但只要你了解這些差異並採取適當的預防措施，就可以順利避免可能的錯誤。</p>
<p>希望這篇文章能夠幫助你更深入地了解 PHP 的陣列操作 🚀</p>
]]></content:encoded>
    </item>
    <item>
      <title>[面試][後端]你會的後端框架不只一個，可以說明一下它們之間的差異嗎？</title>
      <link>https://deanlin.net/blog/202109/fullstack-career-notes-day-13/</link>
      <pubDate>Tue, 28 Sep 2021 08:52:51 +0800</pubDate>
      <guid>https://deanlin.net/blog/202109/fullstack-career-notes-day-13/</guid>
      <dc:creator>林鼎淵</dc:creator>
      <category>職涯成長</category>
      <description>工程師真的很難，會得不多會被問為什麼沒學，會得很多還要被質疑專業程度；這種比較類型的問題，除非你有事先做功課，不然就算你都會用也未必能在現場對答如流。</description>
      <media:content url="https://deanlin.net/blog/202109/fullstack-career-notes-day-13/images/cover_hu_fd19aa6d1c00a16c.jpg" medium="image" type="image/jpeg" width="1200" height="675" />
      <content:encoded><![CDATA[<blockquote>
<p>你是十八般武藝樣樣精通，還是樣樣通樣樣鬆？</p>
</blockquote>
<p>工程師真的很難，會得不多會被問為什麼沒學，會得很多還要被質疑專業程度；這種比較類型的問題，除非你有事先做功課，不然就算你都會用也未必能在現場對答如流。</p>
<h2 id="大綱">大綱<a class="h-anchor" href="#%e5%a4%a7%e7%b6%b1" aria-label="這個段落的連結"></a></h2>
<ol>
<li><strong>你會的後端框架不只一個，可以說明一下它們之間的差異嗎？</strong>
<ul>
<li>1.1 面試官為什麼會問？</li>
<li>1.2 面試官想從答案確認什麼？</li>
<li>1.3 筆者提供的簡答</li>
</ul>
</li>
<li><strong>回答問題所需具備的知識</strong>
<ul>
<li>2.1 PHP VS Node.js</li>
<li>2.2 用 supervisor 讓 Node.js 熱更新</li>
<li>2.3 不一定要拼個誰輸誰贏</li>
</ul>
</li>
<li><strong>衍伸問題</strong>
<ul>
<li>3.1 既然你先學了 Node.js 的框架，為何後來改用 PHP 的框架？</li>
<li>3.2 使用框架有什麼好處跟壞處？</li>
</ul>
</li>
</ol>
<hr>
<h2 class="h-nomark" id="1-你會的後端框架不只一個可以說明一下它們之間的差異嗎">1. 你會的後端框架不只一個，可以說明一下它們之間的差異嗎？<a class="h-anchor" href="#1-%e4%bd%a0%e6%9c%83%e7%9a%84%e5%be%8c%e7%ab%af%e6%a1%86%e6%9e%b6%e4%b8%8d%e5%8f%aa%e4%b8%80%e5%80%8b%e5%8f%af%e4%bb%a5%e8%aa%aa%e6%98%8e%e4%b8%80%e4%b8%8b%e5%ae%83%e5%80%91%e4%b9%8b%e9%96%93%e7%9a%84%e5%b7%ae%e7%95%b0%e5%97%8e" aria-label="這個段落的連結"></a></h2>
<h3 class="h-nomark" id="11-面試官為什麼會問">1.1 面試官為什麼會問？<a class="h-anchor" href="#11-%e9%9d%a2%e8%a9%a6%e5%ae%98%e7%82%ba%e4%bb%80%e9%ba%bc%e6%9c%83%e5%95%8f" aria-label="這個段落的連結"></a></h3>
<p>因為<mark>履歷資訊</mark>中顯示求職者會如下的後端（Backend）框架：</p>
<ul>
<li>PHP：Laravel</li>
<li>Node.js：Express.js、AdonisJS</li>
</ul>
<p>當你會的東西多，面試官就會想了解你是每個都學一點，還是真的了解它。</p>
<hr>
<h3 class="h-nomark" id="12-面試官想從答案確認什麼">1.2 面試官想從答案確認什麼？<a class="h-anchor" href="#12-%e9%9d%a2%e8%a9%a6%e5%ae%98%e6%83%b3%e5%be%9e%e7%ad%94%e6%a1%88%e7%a2%ba%e8%aa%8d%e4%bb%80%e9%ba%bc" aria-label="這個段落的連結"></a></h3>
<ul>
<li>能否簡單敘述 PHP 與 Node.js 的差異</li>
<li>舉例過去用這些框架做過哪些專案或是功能</li>
<li>為什麼會學習新的框架</li>
</ul>
<hr>
<h3 class="h-nomark" id="13-筆者提供的簡答">1.3 筆者提供的簡答<a class="h-anchor" href="#13-%e7%ad%86%e8%80%85%e6%8f%90%e4%be%9b%e7%9a%84%e7%b0%a1%e7%ad%94" aria-label="這個段落的連結"></a></h3>
<p>之前使用 <code>PHP 的 Laravel 框架</code>開發過內部管理系統，也曾用 <code>Node.js 的 Express.js 框架</code>完成過餐廳點餐系統。</p>
<p>兩者的<mark>差異主要因為程式語言</mark>而產生的，PHP 是同步語言、多執行緒，Node.js 是一個能執行 JavaScript 的環境，支援非同步，為單執行緒。</p>
<p>如果專案要求<mark>穩定一致的效能</mark>，我會偏向使用 PHP 框架；而如果要<mark>承接高流量</mark>，我會選擇 Node.js 框架。</p>
<hr>
<h2 class="h-nomark" id="2-回答問題所需具備的知識">2. 回答問題所需具備的知識<a class="h-anchor" href="#2-%e5%9b%9e%e7%ad%94%e5%95%8f%e9%a1%8c%e6%89%80%e9%9c%80%e5%85%b7%e5%82%99%e7%9a%84%e7%9f%a5%e8%ad%98" aria-label="這個段落的連結"></a></h2>
<h3 class="h-nomark" id="21-php-vs-nodejs">2.1 PHP VS Node.js<a class="h-anchor" href="#21-php-vs-nodejs" aria-label="這個段落的連結"></a></h3>
<p>如果面對一個問題只有<mark>一種解決方案</mark>時，面試官會<mark>深入詢問</mark>這個技術；但如果面對問題有<mark>兩個以上的解決方案</mark>，他就會問你在<mark>不同情境</mark>下該使用什麼技術，每個技術都有各自的優勢，這邊我們就從不同的角度來做分析吧～</p>
<ul>
<li><strong>同步的 PHP vs 異步的 Node.js</strong>
<ul>
<li>同步的 PHP 會為每個 Request 建立一個 thread，每個 thread 都會消耗記憶體，也就是說如果想要服務更多的客戶就要花心思在<mark>硬體設備</mark>上。</li>
<li>異步的 Node.js 是非阻塞 (non-blocking) I/O 設計，搭配上 Event Loop 可以用一句很形象的話說明：「與其人多但好多人閒著；還不如一個人拼命往死裡做。」</li>
</ul>
</li>
<li><strong>多執行緒的 PHP VS 單執行緒的 Node.js</strong>
<ul>
<li>
<p><strong>多執行緒 (multi-threaded)的 PHP</strong><br>使用阻塞 (blocking) I/O 的設計</p>
<blockquote>
<p>PHP 可以為每個 Request 都開一個執行緒；如果你想要實現非阻塞也能透過 <a href="https://reactphp.org/">reactPHP</a> 來實現。</p>
</blockquote>
</li>
<li>
<p><strong>單執行緒 (single-threaded)的 Node.js</strong><br>使用非阻塞 (non-blocking) I/O 設計 + 提供非同步 (asynchronous) 處理</p>
<blockquote>
<p>Node.js 原則上是單執行緒，但可以透過 <a href="https://pm2.keymetrics.io/">PM2</a> 的 Cluster Mode 達到多執行緒。</p>
</blockquote>
</li>
</ul>
</li>
<li><strong>應用場景</strong><br>從 Client 端與 Server 端的溝通頻率來做分析：
<ul>
<li><strong>溝通頻率不高</strong><br>PHP 是個不錯的選擇（ex：個人部落格，像 WordPress 就是用 PHP 寫的）。</li>
<li><strong>溝通頻率頻繁</strong><br>Node.js 更能激發性能（ex：single-page-application、聊天室）；它適合大吞吐量的系統，較不適合需要大量 CPU 運算的系統。</li>
</ul>
</li>
<li><strong>可使用的資源</strong>
<ul>
<li>
<p><strong>PHP 有 Composer</strong><br>Composer 的命名方式：「所有者/package 名稱」</p>
</li>
<li>
<p><strong>Node.js 有 NPM</strong><br>NPM 的命名方式：「package 名稱」</p>
<blockquote>
<p>因為 NPM 的命名方式太自由，導致上面有很多的資源都是無效資源，<mark>關鍵字第一個找到的未必是最合適的</mark>。</p>
</blockquote>
</li>
</ul>
</li>
<li><strong>人才市場</strong>
<ul>
<li><strong>人才數量</strong><br>PHP 比 Node.js 早了 10 幾年發行，因此市場上使用 PHP 的人比使用 Node.js 的人更多；如果<mark>職位流動性高，用 PHP 開發</mark>會比較合適。</li>
<li><strong>薪資</strong><br>物以稀為貴，同樣都是新人，<code>會 Node.js 的薪水會略高於會 PHP</code> 的求職者。</li>
</ul>
</li>
<li><strong>專案容量</strong><br>用 Laravel 跟 Express.js 來比較：
<ul>
<li><strong>專案初期</strong><br>一開始 Laravel 因為包山包海所以專案體積較大。</li>
<li><strong>專案後期</strong><br>儘管 Express 是 Node.js 輕量級的框架，但因為<mark>實作過程往往要擴充一堆 package</mark>，而且一個 package 往往會有很多的 dependency；這些原因容易導致最終實作出來的是一個笨重的專案。</li>
</ul>
</li>
<li><strong>程式語言轉換</strong><br>除非你的運氣逆天，每次都剛好找到符合你技能樹的公司，而且這間公司專案用的技術還很固定；不然肯定要面對程式語言的轉換，這邊分享幾個轉換時要注意的重點。
<ul>
<li><strong>找類型相似的框架</strong><br>從 PHP 跳到 Node.js 一定會面臨諸多不適應；但如果你選擇的是跟 <mark>Laravel 很類似的 AdonisJS 框架</mark>，這份不適感會下降許多，反之亦然。</li>
<li><strong>語言語法</strong><br>如果你已經熟悉了一種程式語言，我覺得轉換到另外一個語言的學習成本不會太高；但許多<mark>內建函式都要重新學習、生命週期也要重新熟悉、錯誤處理的經驗也要重新累積</mark>。</li>
<li><strong>專案規模</strong><br>如果是只需要 CRUD 邏輯的小專案，那我覺得藉此多瞭解一門程式語言是一個很棒的機會；但如果面臨的是業務邏輯複雜的大專案，我強烈建議一定要選自己熟悉的程式語言，或者團隊中的扛霸子熟悉這門語言；不然出事根本沒人可以救得了。</li>
</ul>
</li>
<li><strong>效能</strong>
<ul>
<li><strong>響應速度</strong><br>Node.js 靠著 V8 引擎加上非同步的特型，擁有較快的響應速度。</li>
</ul>
</li>
<li><strong>面對錯誤的處理</strong>
<ul>
<li>
<p>PHP 遇到某個 Request 錯誤時，它只對這個 Request 產生影響。</p>
</li>
<li>
<p>Node.js 因為所有 Request 都在單執行緒的 Web-server 中，所以某個 Request 導致的未知錯誤就可能影響到整個 Server。</p>
<blockquote>
<p>不過這塊可以透過 PM2 的 Cluster Mode 與自動重啟來補救。</p>
</blockquote>
</li>
</ul>
</li>
<li><strong>熱更新</strong>
<ul>
<li>PHP 修改後，只要刷新頁面就能看到。</li>
<li>Node.js 預設沒有熱更新，需要關閉專案後重啟。</li>
</ul>
</li>
</ul>
<hr>
<h3 class="h-nomark" id="22-用-supervisor-讓-nodejs-熱更新">2.2 用 supervisor 讓 Node.js 熱更新<a class="h-anchor" href="#22-%e7%94%a8-supervisor-%e8%ae%93-nodejs-%e7%86%b1%e6%9b%b4%e6%96%b0" aria-label="這個段落的連結"></a></h3>
<p>在開發階段推薦使用 supervisor 這款套件。</p>
<ul>
<li>
<p><strong>STEP 1</strong>：在全域安裝套件<code>npm install supervisor -g</code></p>
</li>
<li>
<p><strong>STEP 2</strong>：建立一個<code>myapp.js</code>的檔案，並複製貼上下面程式</p>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#ff7b72">var</span> http <span style="color:#ff7b72;font-weight:bold">=</span> require(<span style="color:#a5d6ff">&#34;http&#34;</span>);
</span></span><span style="display:flex;"><span>http
</span></span><span style="display:flex;"><span>  .createServer(<span style="color:#ff7b72">function</span> (req, res) {
</span></span><span style="display:flex;"><span>    res.writeHead(<span style="color:#a5d6ff">200</span>, { <span style="color:#a5d6ff">&#34;Content-Type&#34;</span><span style="color:#ff7b72;font-weight:bold">:</span> <span style="color:#a5d6ff">&#34;text/plain&#34;</span> });
</span></span><span style="display:flex;"><span>    res.end(<span style="color:#a5d6ff">&#34;Hello World!&#34;</span>);
</span></span><span style="display:flex;"><span>  })
</span></span><span style="display:flex;"><span>  .listen(<span style="color:#a5d6ff">3000</span>, <span style="color:#a5d6ff">&#34;127.0.0.1&#34;</span>);
</span></span></code></pre></div></li>
<li>
<p><strong>STEP 3</strong>：用<code>node myapp.js</code>啟動程式後，修改<code>res.end(&quot;Hello World!&quot;);</code>的內容；再刷新網頁後<mark>內容不變</mark>。</p>
</li>
<li>
<p><strong>STEP 4</strong>：改用<code>supervisor myapp.js</code>啟動程式，修改<code>res.end(&quot;Hello World!&quot;);</code>的內容；再刷新網頁後<mark>內容更新</mark>。</p>
</li>
</ul>
<p>supervisor 也可以做專案資料夾的監控，但是要記得排除一些資料夾（ex：儲存 log 檔的）；否則你的專案就無時無刻在重起了。</p>
<blockquote>
<p>如果是正式的網站且流量大，建議使用 PM2 來做處理，它也有監控資料夾的功能。</p>
</blockquote>
<hr>
<h3 class="h-nomark" id="23-不一定要拼個誰輸誰贏">2.3 不一定要拼個誰輸誰贏<a class="h-anchor" href="#23-%e4%b8%8d%e4%b8%80%e5%ae%9a%e8%a6%81%e6%8b%bc%e5%80%8b%e8%aa%b0%e8%bc%b8%e8%aa%b0%e8%b4%8f" aria-label="這個段落的連結"></a></h3>
<blockquote>
<p>存在即合理</p>
</blockquote>
<ul>
<li><strong>小專案不用花太多時間在評估框架及程式語言選擇上</strong><br>有些公司對專案要用什麼框架以及程式語言保持著非常謹慎的態度，謹慎的態度是對的，但也要考量到專案的規模；如果今天的專案就是 CRUD 的簡單應用，<mark>預估使用者數量不多或是主要來自於內部成員，用什麼框架及程式語言其實都差不多</mark>。</li>
<li><strong>看組織成員擅長的語言</strong><br>如果公司高層為了追求潮流而要求開發人員用一個不熟悉的框架去做專案，他最好祈求專案上線後平平安安；不然<mark>出事了公司沒有能夠處理的人才</mark>，沒有人會在意一個不能用的網站效能有多好。</li>
<li><strong>每個程式語言都有自己的擁護者</strong><br>在不同的條件下，每個程式語言都有自己的優勢，而且每年都會誕生新的框架與程式語言；筆者覺得與其不停盲目追逐新的技術，不如先深入研究一個成熟的技術，<mark>當你有辦法靠它解決各種現實難題時，再去學新的框架跟程式語言也不晚</mark>。</li>
</ul>
<hr>
<h2 class="h-nomark" id="3-衍伸問題">3. 衍伸問題<a class="h-anchor" href="#3-%e8%a1%8d%e4%bc%b8%e5%95%8f%e9%a1%8c" aria-label="這個段落的連結"></a></h2>
<h3 class="h-nomark" id="31-既然你先學了-nodejs-的框架為何後來改用-php-的框架">3.1 既然你先學了 Node.js 的框架，為何後來改用 PHP 的框架？<a class="h-anchor" href="#31-%e6%97%a2%e7%84%b6%e4%bd%a0%e5%85%88%e5%ad%b8%e4%ba%86-nodejs-%e7%9a%84%e6%a1%86%e6%9e%b6%e7%82%ba%e4%bd%95%e5%be%8c%e4%be%86%e6%94%b9%e7%94%a8-php-%e7%9a%84%e6%a1%86%e6%9e%b6" aria-label="這個段落的連結"></a></h3>
<p><mark>考點：這題其實是想了解求職者的人格特質</mark></p>
<p>這兩個都是為了解決問題的技術，我一開始用 Node.js 的 Express 框架<mark>獨立完成</mark>了幾份專案；但後來有一個<code>需要 Team work</code> 的大型專案，因為當時的組長比較熟悉 PHP 的 Laravel 框架，所以我便藉由這個機會<mark>學習新的技術</mark>；也因為這次的技術轉換，讓我<mark>了解不同框架在部署以及效能各方面的差異</mark>。</p>
<blockquote>
<p><strong>我想透過上面的回答給面試官幾個訊息：</strong></p>
<ul>
<li>我能獨立完成專案，也可以團隊合作。</li>
<li>我願意學習新技術。</li>
<li>學習的同時會分析不同框架的優劣。</li>
</ul>
</blockquote>
<hr>
<h3 class="h-nomark" id="32-使用框架有什麼好處跟壞處">3.2 使用框架有什麼好處跟壞處？<a class="h-anchor" href="#32-%e4%bd%bf%e7%94%a8%e6%a1%86%e6%9e%b6%e6%9c%89%e4%bb%80%e9%ba%bc%e5%a5%bd%e8%99%95%e8%b7%9f%e5%a3%9e%e8%99%95" aria-label="這個段落的連結"></a></h3>
<p><mark>考點：確認求職者對框架的認知</mark></p>
<ul>
<li><strong>好處</strong><br>框架能夠<mark>讓初學者快速入門並開發出有一定水平</mark>的專案，因為框架提供了許多常用的函式，因此可以<mark>大幅縮短專案時程</mark>；在團隊協作上框架優勢更為明顯，能夠讓成員<mark>有一致的開發方式</mark>。</li>
<li><strong>壞處</strong><br>因為框架已經幫開發人員做了太多事情，這會導致<mark>開發人員並不理解許多功能的原理</mark>，這也造成了<mark>一但框架無法滿足功能，初學者的生產力會斷崖式下降</mark>；如果長期使用，也容易<mark>讓自己的開發思維被限制在框架內</mark>。</li>
</ul>
<hr>
<h3 id="筆者碎碎念">筆者碎碎念<a class="h-anchor" href="#%e7%ad%86%e8%80%85%e7%a2%8e%e7%a2%8e%e5%bf%b5" aria-label="這個段落的連結"></a></h3>
<p>希望讀者不要只把我提出來的問題當成面試會出的考題；當面試官會這樣詢問時，通常是因為實務上也會遇到類似的問題。</p>
<p>感謝大家的閱讀，如果喜歡我的文章可以<code>訂閱</code>接收通知；如果有幫助到你，按<code>Like</code>可以讓我更有寫文的動力，我們明天見～</p>
<h3 id="參考資源">參考資源：<a class="h-anchor" href="#%e5%8f%83%e8%80%83%e8%b3%87%e6%ba%90" aria-label="這個段落的連結"></a></h3>
<ol>
<li><a href="https://thomashunter.name/posts/2012-06-21-php-vs-nodejs">PHP + Apache Stack vs Node.js</a></li>
<li><a href="https://web.archive.org/web/20220124190306/https://www.itread01.com/content/1549876684.html" title="原網頁已失效，連到網際網路檔案館的存檔">服務端 I/O 效能大比拼：Node、PHP、Java、Go</a></li>
<li><a href="https://jishuin.proginn.com/p/763bfbd2cab5">簡析 Node.js 特點與應用場景</a></li>
<li><a href="https://www.jianshu.com/p/6d84e5efe99d">詳細版 | 用 Supervisor 守護你的 Node.js 進程</a></li>
<li><a href="https://iter01.com/403097.html">淺談 Node.js 和 PHP 程式管理</a></li>
</ol>
<hr>
<hr>
<blockquote>
<p><img src="https://deanlin.net/blog/202109/fullstack-career-notes-day-13/images/fullstack-career-notes-day-13-01.webp" width="1024" height="233" alt="參考資源：（圖 1）" fetchpriority="high" decoding="async"></p>
<p>在許多人的幫助下，本系列文章已成功出版，除了添加新的篇章，更完善了每個案例的應對進退；如果對現在的職涯感到迷茫，也許這本書能帶給你不一樣的觀點～</p>
<p><strong>天瓏書局：</strong> <a href="https://www.tenlong.com.tw/products/9786263334571">https://www.tenlong.com.tw/products/9786263334571</a></p>
</blockquote>
]]></content:encoded>
    </item>
    <item>
      <title>Laravel常用指令</title>
      <link>https://deanlin.net/blog/202006/laravel-common-commands/</link>
      <pubDate>Tue, 16 Jun 2020 17:05:47 +0800</pubDate>
      <guid>https://deanlin.net/blog/202006/laravel-common-commands/</guid>
      <dc:creator>林鼎淵</dc:creator>
      <category>技術教學</category>
      <description>laravel有很多常用的指令，但有時候會忘記或是不想自己全部打完，那這篇文章就是你專門用來複製貼上的大補帖</description>
      <media:content url="https://deanlin.net/blog/202006/laravel-common-commands/images/laravel-common-commands-og_hu_9496b00eff300207.jpg" medium="image" type="image/jpeg" width="1200" height="675" />
      <content:encoded><![CDATA[<p><img src="https://deanlin.net/blog/202006/laravel-common-commands/images/laravel-common-commands-01.webp" width="1400" height="583" alt="Laravel常用指令（圖 1）" fetchpriority="high" decoding="async"></p>
<blockquote>
<p>這篇文章會列出我在使用Laraval框架時常用到的指令(部分是套件指令)</p>
</blockquote>
<h2 class="h-nomark" id="1-資料庫相關指令">1. 資料庫相關指令<a class="h-anchor" href="#1-%e8%b3%87%e6%96%99%e5%ba%ab%e7%9b%b8%e9%97%9c%e6%8c%87%e4%bb%a4" aria-label="這個段落的連結"></a></h2>
<p><strong>Migration 資料庫完全重新設定</strong></p>
<pre tabindex="0"><code>php artisan migrate:refresh — seed
</code></pre><p><strong>備份應用程式包括資料庫，<a href="https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/190804/">請確認是否已經安裝plugin</a></strong></p>
<pre tabindex="0"><code>php artisan backup:run
</code></pre><p><strong>備份的資料庫執行</strong></p>
<pre tabindex="0"><code>php artisan backup:run --only-db
</code></pre><p><strong>只需要備份的檔案</strong></p>
<pre tabindex="0"><code>php artisan backup:run --only-files
</code></pre><p><strong>database 還原</strong></p>
<pre tabindex="0"><code>mysql -u root -p database_name &lt; backup.sql
</code></pre><h2 class="h-nomark" id="2-安裝檔遺失相關issue">2. 安裝檔遺失相關issue<a class="h-anchor" href="#2-%e5%ae%89%e8%a3%9d%e6%aa%94%e9%81%ba%e5%a4%b1%e7%9b%b8%e9%97%9cissue" aria-label="這個段落的連結"></a></h2>
<p><strong>composer 全部砍掉重裝</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>rm -rf vendor/
</span></span><span style="display:flex;"><span>composer update -v
</span></span></code></pre></div><h2 class="h-nomark" id="3-laravel-常用指令">3. Laravel 常用指令<a class="h-anchor" href="#3-laravel-%e5%b8%b8%e7%94%a8%e6%8c%87%e4%bb%a4" aria-label="這個段落的連結"></a></h2>
<p><strong>創建model</strong></p>
<pre tabindex="0"><code>php artisan make:model Photo
</code></pre><p><strong>創建controller</strong></p>
<pre tabindex="0"><code>php artisan make:controller UserController
</code></pre><p><strong>創建migration</strong></p>
<pre tabindex="0"><code>php artisan make:migration create_users_table — table=users
</code></pre><p><strong>創建seeder</strong></p>
<pre tabindex="0"><code>php artisan make:seeder UsersTableSeeder
</code></pre><p><strong>創建middleware</strong></p>
<pre tabindex="0"><code>php artisan make:middleware CheckAge
</code></pre><p><strong>創建job</strong></p>
<pre tabindex="0"><code>php artisan make:job SendReminderEmail
</code></pre><p><strong>創建testcase</strong></p>
<pre tabindex="0"><code>php artisan make:test UserTest
</code></pre><h2 class="h-nomark" id="4-php相關指令">4. php相關指令<a class="h-anchor" href="#4-php%e7%9b%b8%e9%97%9c%e6%8c%87%e4%bb%a4" aria-label="這個段落的連結"></a></h2>
<p><strong>尋找php.ini</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>sudo find / -name ‘php.ini’
</span></span></code></pre></div><p><strong>更改php相關設定(如果遇到nginx 502也能用這個重啟嘗試看看)</strong></p>
<pre tabindex="0"><code>vi /etc/php/7.3/fpm/php.ini
service php7.3-fpm restart
</code></pre><h2 class="h-nomark" id="5-單元測試">5. 單元測試<a class="h-anchor" href="#5-%e5%96%ae%e5%85%83%e6%b8%ac%e8%a9%a6" aria-label="這個段落的連結"></a></h2>
<pre tabindex="0"><code>./vendor/bin/phpunit
// 如果使用sudo權限來做測試，部分storage會變成admin權限，這個指令可以讓整個資料夾回歸www-data權限
chown -R www-data:www-data storage/
</code></pre><h2 class="h-nomark" id="6-排程相關">6. 排程相關<a class="h-anchor" href="#6-%e6%8e%92%e7%a8%8b%e7%9b%b8%e9%97%9c" aria-label="這個段落的連結"></a></h2>
<p><strong>排程重啟supervisor</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>// 檔案位置
</span></span><span style="display:flex;"><span>/etc/supervisor/conf.d
</span></span><span style="display:flex;"><span>// 每次修改需要執行這幾個指令重啟<span style="color:#ff7b72;font-weight:bold">(</span>開啟<span style="color:#ff7b72;font-weight:bold">)</span>
</span></span><span style="display:flex;"><span>sudo supervisorctl reread
</span></span><span style="display:flex;"><span>sudo supervisorctl update
</span></span><span style="display:flex;"><span>sudo supervisorctl start laravel-worker:*
</span></span><span style="display:flex;"><span>sudo supervisorctl restart laravel-worker:*
</span></span></code></pre></div><p><strong>本機local端測試排程job</strong></p>
<p>下面幾種job為我個人將每個排程進行分類，讓需要較長時間的排程不會影響到其他短時間能完成的排程</p>
<pre tabindex="0"><code>php artisan queue:work — queue=small_job,default — timeout=600 — delay=3 — sleep=3 — tries=3
php artisan queue:work — queue=big_job,default — timeout=7200 — delay=3 — sleep=3 — tries=3
php artisan queue:work — queue=default — delay=3 — sleep=3 — tries=3
</code></pre><h2 class="h-nomark" id="7-查看錯誤訊息">7. 查看錯誤訊息<a class="h-anchor" href="#7-%e6%9f%a5%e7%9c%8b%e9%8c%af%e8%aa%a4%e8%a8%8a%e6%81%af" aria-label="這個段落的連結"></a></h2>
<p>如果為 php 的錯誤訊息</p>
<pre tabindex="0"><code>vi /var/log/php7.3-fpm.log
</code></pre><p>如果為 Laravel 的錯誤訊息</p>
<pre tabindex="0"><code>vi project_path/storage/logs/laravel-xxx-xx.log
</code></pre><blockquote>
<p>從 log 就能推斷出大部分的問題，並取得查詢的關鍵字</p>
</blockquote>
]]></content:encoded>
    </item>
  </channel>
</rss>
