iT邦幫忙

2021 iThome 鐵人賽

DAY 7
0
Software Development

用 PHP 打造專屬於自己的 Telegram 聊天機器人吧!系列 第 7

【PHP Telegram Bot】Day07 - 安裝 VS Code 並撰寫第一支程式

俗話說得好:「工欲善其事,必先利其器」。

沒有一個好用的文字編輯器,要如何輕鬆愉悅的寫程式呢?

安裝 VS Code

這應該不用我教了吧?就是那種最熟悉的安裝方式,

官網下載安裝檔,然後執行

閉著眼睛按下我同意
https://ithelp.ithome.com.tw/upload/images/20210916/20132916KwExY2VHrZ.png

這個建議打勾,之後的 PHP 程式就會預設用 VS Code 開啟
https://ithelp.ithome.com.tw/upload/images/20210916/20132916IcTxB0TnB9.png

下一步
https://ithelp.ithome.com.tw/upload/images/20210916/20132916BBRtl0jWvx.png

這樣就安裝完成了
https://ithelp.ithome.com.tw/upload/images/20210916/20132916qa7vdhje9a.png


安裝擴充功能

https://ithelp.ithome.com.tw/upload/images/20210916/20132916q4IWWkW8xk.png
https://ithelp.ithome.com.tw/upload/images/20210916/20132916100KHcdMON.png


使用者設定

檔案 > 喜好設定 > 設定 或是按 Ctrl + ,

來講講我的設定,先按右上角這個 icon,開啟 json 設定檔
https://ithelp.ithome.com.tw/upload/images/20210916/201329167iOnj1YeCa.png

挑你喜歡的貼進去,記得每行結尾要有逗號

{
    // 文字大小
    "editor.fontSize": 18,
    // 自動格式化
    "editor.formatOnPaste": true,
    "editor.formatOnSave": true,
    "editor.formatOnType": true,
    // 顯示那些你看不見的空白符號
    "editor.renderWhitespace": "all",
    // 平滑捲動
    "editor.smoothScrolling": true,
    // 用 tab 也能自動補全
    "editor.tabCompletion": "on",
    // Code Runner 在終端機運行
    "code-runner.runInTerminal": true,
    // 自動儲存
    "files.autoSave": "afterDelay",
    // 使用 Unix 風格的換行
    "files.eol": "\n",
    // 大括號 { 會在行尾,而不是下一行
    "intelephense.format.braces": "k&r",
    // 設定 php 可執行檔位置
    "php.validate.executablePath": "C:\\php-7.4.23\\php.exe",
    // 設定 php 的格式化工具
    "[php]": {
        "editor.defaultFormatter": "bmewburn.vscode-intelephense-client",
    },
}

撰寫第一支程式

找一個你喜歡的地方(檔案路徑)(不要有中文或是奇怪的符號,有機會讓程式出錯)

建立一個和機器人 username 一樣名稱的資料夾

然後對它按右鍵,以 Code 開啟
https://ithelp.ithome.com.tw/upload/images/20210916/20132916SqqtoCBP7s.png

新增一個檔案,檔名可以隨意,但是結尾要有 .php,這樣 Code Runner 才知道要用 php 執行
https://ithelp.ithome.com.tw/upload/images/20210916/20132916OmjoO3MYke.png

輸入這些文字,之後按下右上角的執行
https://ithelp.ithome.com.tw/upload/images/20210916/201329161lS7fnqavJ.png

如果有出現 Hello, World! 就表示你有輸入正確,做得好!
https://ithelp.ithome.com.tw/upload/images/20210916/2013291631P5qr18u2.png

出現 syntax error 代表你有地方打錯,請檢查看看

一起來找看看我是哪邊打錯了?
https://ithelp.ithome.com.tw/upload/images/20210916/2013291643d1Ub6hGg.png


用指令的方式讓程式運行

其實原本是要在底下自己打指令讓程式運行的啦,但是有了 Code Runner 誰還在跟你打指令呢?

Ctrl + ~ 可以開啟或關閉下方終端機

在終端機輸入 php {檔名}
https://ithelp.ithome.com.tw/upload/images/20210916/20132916fIc85rhaCF.png

按下 Enter 後,程式就會執行
https://ithelp.ithome.com.tw/upload/images/20210916/20132916wkTg2GzxF9.png


結尾

想當年 我自己摸了很久才讓程式跑起來,當時真的非常的開心,原來我也可以寫程式

不知道你第一次讓程式跑起來的感受是如何呢?

或是你有任何問題?都歡迎在下方留言讓我知道


上一篇
【PHP Telegram Bot】Day06 - 安裝 PHP 與設定環境變數
下一篇
【PHP Telegram Bot】Day08 - 官方範例程式
系列文
用 PHP 打造專屬於自己的 Telegram 聊天機器人吧!30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言