Làm cách nào để kiểm tra cú pháp PHP trên dòng lệnh?

PHP là ngôn ngữ kịch bản phía máy chủ, nhưng cũng có thể được sử dụng làm ngôn ngữ lập trình đa năng

Công cụ kiểm tra lỗi PHP cho phép tìm lỗi cú pháp [lint]. Bạn có thể kiểm tra trực tuyến mã PHP của mình trực tiếp trong trình duyệt của mình

Nếu phát hiện lỗi cú pháp, thì dòng bị lỗi sẽ được tô sáng và nó sẽ chuyển đến dòng đó để tiết kiệm thời gian [không cần tìm kiếm dòng]

Có thể hữu ích khi thực hiện kiểm tra trực tuyến để tiết kiệm thời gian [triển khai. ]

Một API có sẵn để kiểm tra cú pháp của tập lệnh php của bạn

ĐĂNG https. //api. lớp mở rộng. com/php-checker/. Phiên bản PHP

Kiểm tra cú pháp PHP. phiên bản PHP. 5. 6. 40, 7. 4. 8, 8. 0. 0

ví dụ JavaScript

const request = new XMLHttpRequest[];
request.open["POST", "//api.extendsclass.com/php-checker/8.0.0", true];
request.onreadystatechange = [] => {
};
request.send[''];

Phản ứng

{
   "code":0,
   "stdout":"No syntax errors detected"
}

{
    "code": 4294967295,
    "stdout": "\nParse error: syntax error, unexpected 'dir' [T_STRING] in file on line 3\nErrors parsing file\n"
}

PHP version is not available

PHP [từ viết tắt đệ quy của PHP. Hypertext Preprocessor] là ngôn ngữ kịch bản phía máy chủ đặc biệt phù hợp để phát triển web. Tập lệnh PHP có thể được nhúng vào HTML. PHP cũng có thể được sử dụng làm ngôn ngữ lập trình đa năng

PHP đã trở nên phổ biến trong giới Web Developer nhờ những ưu điểm của nó. Đơn giản, Miễn phí, Linh hoạt và Có thể mở rộng, CMS, Cộng đồng rộng lớn

Như đã đề cập cách đây vài tháng, các phiên bản gần đây của PHP bao gồm một công cụ dòng lệnh, nghĩa là bạn có thể sử dụng PHP làm ngôn ngữ kịch bản hệ vỏ. Phiên bản PHP đi kèm với Panther bao gồm công cụ dòng lệnh PHP;

Hãy đưa nó vào sử dụng, bằng cách thêm tính năng kiểm tra cú pháp PHP vào BBEdit

Đây là cách nó hoạt động. Nếu bạn chuyển công cụ dòng lệnh php tùy chọn -l [đó là chữ L viết thường], nó sẽ thực hiện kiểm tra cú pháp thô sơ thay vì thực thi mã nguồn. Ví dụ: từ thiết bị đầu cuối, bạn sẽ nhập

php -l /path/to/your/file.php

Tôi nói “thô sơ” bởi vì, theo như tôi có thể nói, trình kiểm tra cú pháp của PHP chỉ phát hiện các lỗi phân tích cú pháp - các lỗi quá nghiêm trọng khiến PHP không thể phân tích cú pháp mã. Ngoài ra, bởi vì PHP ngừng xử lý tệp khi nó gặp phải một lỗi phân tích cú pháp duy nhất, nên nó chỉ trả về một lỗi duy nhất; . Bạn sẽ cần sửa nó và chạy lại trình kiểm tra để tìm cái tiếp theo. Tuy nhiên, vẫn tốt hơn là không có người kiểm tra nào cả

Dựa theo

Tùy chọn này cung cấp một cách thuận tiện để chỉ thực hiện kiểm tra cú pháp trên mã PHP đã cho. Khi thành công, văn bản No syntax errors detected in được ghi vào đầu ra tiêu chuẩn và mã trả về shell là 0. Khi không thành công, văn bản Errors parsing cùng với thông báo lỗi trình phân tích cú pháp nội bộ được ghi vào đầu ra tiêu chuẩn và mã trả về shell được đặt thành 255

OK, vậy bây giờ chúng ta biết cách yêu cầu công cụ dòng lệnh php thực hiện kiểm tra cú pháp và chúng ta biết loại đầu ra mong đợi. Để gọi nó từ BBEdit, chúng ta có thể sử dụng lệnh

-- start script
on run
   -- The run handler is called when the script is invoked normally,
   -- such as from BBEdit's Scripts menu.
   set php_errs to PHPSyntaxCheck[]
   if not php_errs then
      display dialog "No PHP syntax errors detected." buttons {"OK"} ¬
         default button 1 with icon note
   end if
end run


on menuselect[]
   -- The menuselect[] handler gets called when the script is invoked
   -- by BBEdit as a menu script. Save this script, or an alias to it,
   -- as "Check•Document Syntax" in the "Menu Scripts" folder in your
   -- "BBEdit Support" folder.
   set php_errs to PHPSyntaxCheck[]
   if php_errs then
      -- PHP reported an error, so tell BBEdit *not* to
      -- continue with it's HTML syntax check:
      return true
   else
      -- No PHP errors, so tell BBEdit to run its
      -- HTML syntax check 
      return false
   end if
end menuselect


on PHPSyntaxCheck[]
   -- Input: none
   -- Returns: true if PHP reports errors, false otherwise

   tell application "BBEdit"
      try
         set w to text window 1
      on error
         beep
         return
      end try

        set is_dirty to modified of [document of w]
        -- Find out if w is using Mac, Unix, or DOS line endings:
        set linebreaks to [line breaks of document of w]
        set the_filename to name of [document of w]

      if [is_dirty] or [linebreaks is not equal to Unix] then
         -- We need to write a temporary file.
         set parent_folder to [path to temporary items folder]
         set temp_file to [parent_folder as string] & the_filename
         if not [my WriteUnixTextFile[temp_file, text of w]] then
            -- End script, because an error occured writing the temp file
            return
         end if
      else
         -- We can pass the actual file to PHP, no temp file needed:
         tell application "Finder"
            set parent_folder to [container of [file of w as alias]] as alias
         end tell
      end if
   end tell

   -- First, cd to the directory where the script file is, then tell
   -- php to syntax check it. We *could* do this with one command,
   -- by passing to php the full path of the script, but then the
   -- error messages from php will contain the full path as well,
   -- instead of just the file name.
   --
   -- [NOTE: If you're running Jaguar, you'll need to change the path for
   -- the php tool, probably to "/usr/local/php/bin/php" ]
   set the_command to "cd " & quoted form of POSIX path of parent_folder & ¬
      ";  /usr/bin/php -l " & [quoted form of [the_filename]]
   try
      set the_result to do shell script the_command
      if the_result starts with "No syntax errors detected" then
         set errors_found to false -- the return value
      else
         -- I don't think we'll ever get here, because if PHP reports
         -- any actual errors, they're sent to STDERR, not STDOUT, and
         -- will thus trigger the below 'on error' handler.
         display dialog the_result
      end if

   on error err_text
      tell application "BBEdit"
         try
            -- first line of err_text is, as far as I can tell, always blank
            -- second line is the useful error message
            -- third line is "Errors parsing "
            set errors_found to true
            set msg to paragraph 2 of err_text
            set line_num to last word of msg as integer
            select line line_num of text window 1
            display dialog "PHP " & msg with icon stop ¬
               buttons {"OK"} default button 1
         on error
            -- If the error message from PHP comes back in
            -- in an unexpected format, display it in a new
            -- BBEdit window.
            make new text window with properties {text:err_text}
         end try
      end tell
   end try

   return errors_found

end PHPSyntaxCheck



on WriteUnixTextFile[file_name, file_contents]
   -- Write a text file with unix-style line endings.
   -- Input:
   --    file_name - the HFS-style path for the file to write
   --    file_contents - the text to write to a file
   -- Returns: true for success, false for failure
   try
      set file_ref to ¬
         open for access file file_name with write permission
      set eof of file_ref to 0

      -- change the text of file_contents to unix line breaks
      set old_delims to AppleScript's text item delimiters
      set AppleScript's text item delimiters to return
      set text_list to every text item of file_contents as list
      set AppleScript's text item delimiters to [ASCII character 10]
      set file_contents to [text_list as string]
      set AppleScript's text item delimiters to old_delims

      write file_contents to file_ref starting at eof
      close access file_ref
      return true
   on error err_msg
      try
         close access file file_ref
      end try
      display dialog err_msg with icon stop buttons {"OK"} ¬
         default button 1
      return false
   end try
end WriteUnixTextFile
-- end script
0 của AppleScript. Vì vậy, ý chính cơ bản về cách tập lệnh AppleScript có thể hoạt động

  1. Nhận đường dẫn đến tài liệu của cửa sổ văn bản phía trước
  2. Sử dụng
    -- start script
    on run
       -- The run handler is called when the script is invoked normally,
       -- such as from BBEdit's Scripts menu.
       set php_errs to PHPSyntaxCheck[]
       if not php_errs then
          display dialog "No PHP syntax errors detected." buttons {"OK"} ¬
             default button 1 with icon note
       end if
    end run
    
    
    on menuselect[]
       -- The menuselect[] handler gets called when the script is invoked
       -- by BBEdit as a menu script. Save this script, or an alias to it,
       -- as "Check•Document Syntax" in the "Menu Scripts" folder in your
       -- "BBEdit Support" folder.
       set php_errs to PHPSyntaxCheck[]
       if php_errs then
          -- PHP reported an error, so tell BBEdit *not* to
          -- continue with it's HTML syntax check:
          return true
       else
          -- No PHP errors, so tell BBEdit to run its
          -- HTML syntax check 
          return false
       end if
    end menuselect
    
    
    on PHPSyntaxCheck[]
       -- Input: none
       -- Returns: true if PHP reports errors, false otherwise
    
       tell application "BBEdit"
          try
             set w to text window 1
          on error
             beep
             return
          end try
    
            set is_dirty to modified of [document of w]
            -- Find out if w is using Mac, Unix, or DOS line endings:
            set linebreaks to [line breaks of document of w]
            set the_filename to name of [document of w]
    
          if [is_dirty] or [linebreaks is not equal to Unix] then
             -- We need to write a temporary file.
             set parent_folder to [path to temporary items folder]
             set temp_file to [parent_folder as string] & the_filename
             if not [my WriteUnixTextFile[temp_file, text of w]] then
                -- End script, because an error occured writing the temp file
                return
             end if
          else
             -- We can pass the actual file to PHP, no temp file needed:
             tell application "Finder"
                set parent_folder to [container of [file of w as alias]] as alias
             end tell
          end if
       end tell
    
       -- First, cd to the directory where the script file is, then tell
       -- php to syntax check it. We *could* do this with one command,
       -- by passing to php the full path of the script, but then the
       -- error messages from php will contain the full path as well,
       -- instead of just the file name.
       --
       -- [NOTE: If you're running Jaguar, you'll need to change the path for
       -- the php tool, probably to "/usr/local/php/bin/php" ]
       set the_command to "cd " & quoted form of POSIX path of parent_folder & ¬
          ";  /usr/bin/php -l " & [quoted form of [the_filename]]
       try
          set the_result to do shell script the_command
          if the_result starts with "No syntax errors detected" then
             set errors_found to false -- the return value
          else
             -- I don't think we'll ever get here, because if PHP reports
             -- any actual errors, they're sent to STDERR, not STDOUT, and
             -- will thus trigger the below 'on error' handler.
             display dialog the_result
          end if
    
       on error err_text
          tell application "BBEdit"
             try
                -- first line of err_text is, as far as I can tell, always blank
                -- second line is the useful error message
                -- third line is "Errors parsing "
                set errors_found to true
                set msg to paragraph 2 of err_text
                set line_num to last word of msg as integer
                select line line_num of text window 1
                display dialog "PHP " & msg with icon stop ¬
                   buttons {"OK"} default button 1
             on error
                -- If the error message from PHP comes back in
                -- in an unexpected format, display it in a new
                -- BBEdit window.
                make new text window with properties {text:err_text}
             end try
          end tell
       end try
    
       return errors_found
    
    end PHPSyntaxCheck
    
    
    
    on WriteUnixTextFile[file_name, file_contents]
       -- Write a text file with unix-style line endings.
       -- Input:
       --    file_name - the HFS-style path for the file to write
       --    file_contents - the text to write to a file
       -- Returns: true for success, false for failure
       try
          set file_ref to ¬
             open for access file file_name with write permission
          set eof of file_ref to 0
    
          -- change the text of file_contents to unix line breaks
          set old_delims to AppleScript's text item delimiters
          set AppleScript's text item delimiters to return
          set text_list to every text item of file_contents as list
          set AppleScript's text item delimiters to [ASCII character 10]
          set file_contents to [text_list as string]
          set AppleScript's text item delimiters to old_delims
    
          write file_contents to file_ref starting at eof
          close access file_ref
          return true
       on error err_msg
          try
             close access file file_ref
          end try
          display dialog err_msg with icon stop buttons {"OK"} ¬
             default button 1
          return false
       end try
    end WriteUnixTextFile
    -- end script
    
    0 để gọi
    -- start script
    on run
       -- The run handler is called when the script is invoked normally,
       -- such as from BBEdit's Scripts menu.
       set php_errs to PHPSyntaxCheck[]
       if not php_errs then
          display dialog "No PHP syntax errors detected." buttons {"OK"} ¬
             default button 1 with icon note
       end if
    end run
    
    
    on menuselect[]
       -- The menuselect[] handler gets called when the script is invoked
       -- by BBEdit as a menu script. Save this script, or an alias to it,
       -- as "Check•Document Syntax" in the "Menu Scripts" folder in your
       -- "BBEdit Support" folder.
       set php_errs to PHPSyntaxCheck[]
       if php_errs then
          -- PHP reported an error, so tell BBEdit *not* to
          -- continue with it's HTML syntax check:
          return true
       else
          -- No PHP errors, so tell BBEdit to run its
          -- HTML syntax check 
          return false
       end if
    end menuselect
    
    
    on PHPSyntaxCheck[]
       -- Input: none
       -- Returns: true if PHP reports errors, false otherwise
    
       tell application "BBEdit"
          try
             set w to text window 1
          on error
             beep
             return
          end try
    
            set is_dirty to modified of [document of w]
            -- Find out if w is using Mac, Unix, or DOS line endings:
            set linebreaks to [line breaks of document of w]
            set the_filename to name of [document of w]
    
          if [is_dirty] or [linebreaks is not equal to Unix] then
             -- We need to write a temporary file.
             set parent_folder to [path to temporary items folder]
             set temp_file to [parent_folder as string] & the_filename
             if not [my WriteUnixTextFile[temp_file, text of w]] then
                -- End script, because an error occured writing the temp file
                return
             end if
          else
             -- We can pass the actual file to PHP, no temp file needed:
             tell application "Finder"
                set parent_folder to [container of [file of w as alias]] as alias
             end tell
          end if
       end tell
    
       -- First, cd to the directory where the script file is, then tell
       -- php to syntax check it. We *could* do this with one command,
       -- by passing to php the full path of the script, but then the
       -- error messages from php will contain the full path as well,
       -- instead of just the file name.
       --
       -- [NOTE: If you're running Jaguar, you'll need to change the path for
       -- the php tool, probably to "/usr/local/php/bin/php" ]
       set the_command to "cd " & quoted form of POSIX path of parent_folder & ¬
          ";  /usr/bin/php -l " & [quoted form of [the_filename]]
       try
          set the_result to do shell script the_command
          if the_result starts with "No syntax errors detected" then
             set errors_found to false -- the return value
          else
             -- I don't think we'll ever get here, because if PHP reports
             -- any actual errors, they're sent to STDERR, not STDOUT, and
             -- will thus trigger the below 'on error' handler.
             display dialog the_result
          end if
    
       on error err_text
          tell application "BBEdit"
             try
                -- first line of err_text is, as far as I can tell, always blank
                -- second line is the useful error message
                -- third line is "Errors parsing "
                set errors_found to true
                set msg to paragraph 2 of err_text
                set line_num to last word of msg as integer
                select line line_num of text window 1
                display dialog "PHP " & msg with icon stop ¬
                   buttons {"OK"} default button 1
             on error
                -- If the error message from PHP comes back in
                -- in an unexpected format, display it in a new
                -- BBEdit window.
                make new text window with properties {text:err_text}
             end try
          end tell
       end try
    
       return errors_found
    
    end PHPSyntaxCheck
    
    
    
    on WriteUnixTextFile[file_name, file_contents]
       -- Write a text file with unix-style line endings.
       -- Input:
       --    file_name - the HFS-style path for the file to write
       --    file_contents - the text to write to a file
       -- Returns: true for success, false for failure
       try
          set file_ref to ¬
             open for access file file_name with write permission
          set eof of file_ref to 0
    
          -- change the text of file_contents to unix line breaks
          set old_delims to AppleScript's text item delimiters
          set AppleScript's text item delimiters to return
          set text_list to every text item of file_contents as list
          set AppleScript's text item delimiters to [ASCII character 10]
          set file_contents to [text_list as string]
          set AppleScript's text item delimiters to old_delims
    
          write file_contents to file_ref starting at eof
          close access file_ref
          return true
       on error err_msg
          try
             close access file file_ref
          end try
          display dialog err_msg with icon stop buttons {"OK"} ¬
             default button 1
          return false
       end try
    end WriteUnixTextFile
    -- end script
    
    2 theo đường dẫn từ bước 1
  3. Nếu không có lỗi, hãy đưa ra cảnh báo “Không tìm thấy lỗi”;

Tuy nhiên, bước 1, như đã nói ở trên, hơi lười. Nếu cửa sổ BBEdit phía trước có các thay đổi chưa được lưu thì sao? . Nhưng điều đó vẫn còn lười biếng - chúng ta có thể kiểm tra cú pháp một tệp có các thay đổi chưa được lưu. Ngoài ra, nếu cửa sổ BBEdit phía trước là một tài liệu mới không có tiêu đề và chưa bao giờ được lưu thì sao?

Một cách để giải quyết những vấn đề này là sử dụng một tệp tạm thời nếu cửa sổ BBEdit phía trước có các thay đổi chưa được lưu [hoặc nếu nó không sử dụng kết thúc dòng kiểu Unix, vì đó là điều mà công cụ php mong đợi]. Sau đó, chúng tôi sẽ chuyển tệp tạm thời sang trình kiểm tra cú pháp PHP

Khi tìm thấy lỗi, chúng tôi có thể tạo cửa sổ trình duyệt kết quả BBEdit thay vì chỉ hiển thị thông báo lỗi trong cảnh báo. BBEdit thực hiện điều này với trình kiểm tra cú pháp HTML tích hợp sẵn của nó và với sự hỗ trợ của nó đối với các tập lệnh Perl kiểm tra cú pháp. Nhưng vấn đề là gì, vì PHP sẽ chỉ trả về một lỗi duy nhất tại một thời điểm?

Tuy nhiên, điều thú vị sẽ là kiểm tra xem liệu PHP có trả về một số dòng có thông báo lỗi hay không và nếu có, hãy chọn dòng đó trong cửa sổ văn bản. Vì vậy, chúng tôi sẽ làm điều đó

Và, nói về trình kiểm tra HTML tích hợp của BBEdit, sẽ rất tuyệt nếu chúng ta có thể kết hợp nó với trình kiểm tra PHP của mình phải không? . Bằng cách thêm một trình xử lý ngắn

-- start script
on run
   -- The run handler is called when the script is invoked normally,
   -- such as from BBEdit's Scripts menu.
   set php_errs to PHPSyntaxCheck[]
   if not php_errs then
      display dialog "No PHP syntax errors detected." buttons {"OK"} ¬
         default button 1 with icon note
   end if
end run


on menuselect[]
   -- The menuselect[] handler gets called when the script is invoked
   -- by BBEdit as a menu script. Save this script, or an alias to it,
   -- as "Check•Document Syntax" in the "Menu Scripts" folder in your
   -- "BBEdit Support" folder.
   set php_errs to PHPSyntaxCheck[]
   if php_errs then
      -- PHP reported an error, so tell BBEdit *not* to
      -- continue with it's HTML syntax check:
      return true
   else
      -- No PHP errors, so tell BBEdit to run its
      -- HTML syntax check 
      return false
   end if
end menuselect


on PHPSyntaxCheck[]
   -- Input: none
   -- Returns: true if PHP reports errors, false otherwise

   tell application "BBEdit"
      try
         set w to text window 1
      on error
         beep
         return
      end try

        set is_dirty to modified of [document of w]
        -- Find out if w is using Mac, Unix, or DOS line endings:
        set linebreaks to [line breaks of document of w]
        set the_filename to name of [document of w]

      if [is_dirty] or [linebreaks is not equal to Unix] then
         -- We need to write a temporary file.
         set parent_folder to [path to temporary items folder]
         set temp_file to [parent_folder as string] & the_filename
         if not [my WriteUnixTextFile[temp_file, text of w]] then
            -- End script, because an error occured writing the temp file
            return
         end if
      else
         -- We can pass the actual file to PHP, no temp file needed:
         tell application "Finder"
            set parent_folder to [container of [file of w as alias]] as alias
         end tell
      end if
   end tell

   -- First, cd to the directory where the script file is, then tell
   -- php to syntax check it. We *could* do this with one command,
   -- by passing to php the full path of the script, but then the
   -- error messages from php will contain the full path as well,
   -- instead of just the file name.
   --
   -- [NOTE: If you're running Jaguar, you'll need to change the path for
   -- the php tool, probably to "/usr/local/php/bin/php" ]
   set the_command to "cd " & quoted form of POSIX path of parent_folder & ¬
      ";  /usr/bin/php -l " & [quoted form of [the_filename]]
   try
      set the_result to do shell script the_command
      if the_result starts with "No syntax errors detected" then
         set errors_found to false -- the return value
      else
         -- I don't think we'll ever get here, because if PHP reports
         -- any actual errors, they're sent to STDERR, not STDOUT, and
         -- will thus trigger the below 'on error' handler.
         display dialog the_result
      end if

   on error err_text
      tell application "BBEdit"
         try
            -- first line of err_text is, as far as I can tell, always blank
            -- second line is the useful error message
            -- third line is "Errors parsing "
            set errors_found to true
            set msg to paragraph 2 of err_text
            set line_num to last word of msg as integer
            select line line_num of text window 1
            display dialog "PHP " & msg with icon stop ¬
               buttons {"OK"} default button 1
         on error
            -- If the error message from PHP comes back in
            -- in an unexpected format, display it in a new
            -- BBEdit window.
            make new text window with properties {text:err_text}
         end try
      end tell
   end try

   return errors_found

end PHPSyntaxCheck



on WriteUnixTextFile[file_name, file_contents]
   -- Write a text file with unix-style line endings.
   -- Input:
   --    file_name - the HFS-style path for the file to write
   --    file_contents - the text to write to a file
   -- Returns: true for success, false for failure
   try
      set file_ref to ¬
         open for access file file_name with write permission
      set eof of file_ref to 0

      -- change the text of file_contents to unix line breaks
      set old_delims to AppleScript's text item delimiters
      set AppleScript's text item delimiters to return
      set text_list to every text item of file_contents as list
      set AppleScript's text item delimiters to [ASCII character 10]
      set file_contents to [text_list as string]
      set AppleScript's text item delimiters to old_delims

      write file_contents to file_ref starting at eof
      close access file_ref
      return true
   on error err_msg
      try
         close access file file_ref
      end try
      display dialog err_msg with icon stop buttons {"OK"} ¬
         default button 1
      return false
   end try
end WriteUnixTextFile
-- end script
4 vào tập lệnh của chúng tôi, chúng tôi có thể sử dụng nó làm tập lệnh menu BBEdit

Tập lệnh menu được lưu trong thư mục “Tập lệnh Menu” trong thư mục “Hỗ trợ BBEdit”. Tập lệnh menu có thể thay thế hoặc bổ sung bất kỳ lệnh menu tích hợp nào của BBEdit. Các tập lệnh menu được gắn vào các lệnh bằng cách đặt cho chúng các tên tệp đặc biệt, ở dạng Menu•Name. Trong trường hợp này, vì chúng tôi muốn bổ sung trình kiểm tra cú pháp HTML của BBEdit, chúng tôi cần sử dụng tên tệp “Check•Document Syntax”

Nhưng chúng ta cũng có thể sử dụng cùng một tập lệnh như một tập lệnh thông thường, được gọi từ menu Tập lệnh của BBEdit. Để làm như vậy, chúng tôi sẽ thêm một trình xử lý

-- start script
on run
   -- The run handler is called when the script is invoked normally,
   -- such as from BBEdit's Scripts menu.
   set php_errs to PHPSyntaxCheck[]
   if not php_errs then
      display dialog "No PHP syntax errors detected." buttons {"OK"} ¬
         default button 1 with icon note
   end if
end run


on menuselect[]
   -- The menuselect[] handler gets called when the script is invoked
   -- by BBEdit as a menu script. Save this script, or an alias to it,
   -- as "Check•Document Syntax" in the "Menu Scripts" folder in your
   -- "BBEdit Support" folder.
   set php_errs to PHPSyntaxCheck[]
   if php_errs then
      -- PHP reported an error, so tell BBEdit *not* to
      -- continue with it's HTML syntax check:
      return true
   else
      -- No PHP errors, so tell BBEdit to run its
      -- HTML syntax check 
      return false
   end if
end menuselect


on PHPSyntaxCheck[]
   -- Input: none
   -- Returns: true if PHP reports errors, false otherwise

   tell application "BBEdit"
      try
         set w to text window 1
      on error
         beep
         return
      end try

        set is_dirty to modified of [document of w]
        -- Find out if w is using Mac, Unix, or DOS line endings:
        set linebreaks to [line breaks of document of w]
        set the_filename to name of [document of w]

      if [is_dirty] or [linebreaks is not equal to Unix] then
         -- We need to write a temporary file.
         set parent_folder to [path to temporary items folder]
         set temp_file to [parent_folder as string] & the_filename
         if not [my WriteUnixTextFile[temp_file, text of w]] then
            -- End script, because an error occured writing the temp file
            return
         end if
      else
         -- We can pass the actual file to PHP, no temp file needed:
         tell application "Finder"
            set parent_folder to [container of [file of w as alias]] as alias
         end tell
      end if
   end tell

   -- First, cd to the directory where the script file is, then tell
   -- php to syntax check it. We *could* do this with one command,
   -- by passing to php the full path of the script, but then the
   -- error messages from php will contain the full path as well,
   -- instead of just the file name.
   --
   -- [NOTE: If you're running Jaguar, you'll need to change the path for
   -- the php tool, probably to "/usr/local/php/bin/php" ]
   set the_command to "cd " & quoted form of POSIX path of parent_folder & ¬
      ";  /usr/bin/php -l " & [quoted form of [the_filename]]
   try
      set the_result to do shell script the_command
      if the_result starts with "No syntax errors detected" then
         set errors_found to false -- the return value
      else
         -- I don't think we'll ever get here, because if PHP reports
         -- any actual errors, they're sent to STDERR, not STDOUT, and
         -- will thus trigger the below 'on error' handler.
         display dialog the_result
      end if

   on error err_text
      tell application "BBEdit"
         try
            -- first line of err_text is, as far as I can tell, always blank
            -- second line is the useful error message
            -- third line is "Errors parsing "
            set errors_found to true
            set msg to paragraph 2 of err_text
            set line_num to last word of msg as integer
            select line line_num of text window 1
            display dialog "PHP " & msg with icon stop ¬
               buttons {"OK"} default button 1
         on error
            -- If the error message from PHP comes back in
            -- in an unexpected format, display it in a new
            -- BBEdit window.
            make new text window with properties {text:err_text}
         end try
      end tell
   end try

   return errors_found

end PHPSyntaxCheck



on WriteUnixTextFile[file_name, file_contents]
   -- Write a text file with unix-style line endings.
   -- Input:
   --    file_name - the HFS-style path for the file to write
   --    file_contents - the text to write to a file
   -- Returns: true for success, false for failure
   try
      set file_ref to ¬
         open for access file file_name with write permission
      set eof of file_ref to 0

      -- change the text of file_contents to unix line breaks
      set old_delims to AppleScript's text item delimiters
      set AppleScript's text item delimiters to return
      set text_list to every text item of file_contents as list
      set AppleScript's text item delimiters to [ASCII character 10]
      set file_contents to [text_list as string]
      set AppleScript's text item delimiters to old_delims

      write file_contents to file_ref starting at eof
      close access file_ref
      return true
   on error err_msg
      try
         close access file file_ref
      end try
      display dialog err_msg with icon stop buttons {"OK"} ¬
         default button 1
      return false
   end try
end WriteUnixTextFile
-- end script
5 vào tập lệnh

Đây là cách tôi thiết lập nó ở đây

  1. Tôi đã lưu tập lệnh dưới dạng AppleScript đã biên dịch, trong thư mục “Tập lệnh” trong “Thư mục hỗ trợ BBEdit của tôi. Tôi đã đặt tên cho nó là “Kiểm tra cú pháp PHP” và đó là cách nó xuất hiện trong menu Tập lệnh của tôi

  2. Tôi đã tạo bí danh cho tập lệnh “Kiểm tra cú pháp PHP” và tôi đặt tên cho bí danh là “Kiểm tra • Cú pháp tài liệu”. Tôi đã chuyển bí danh vào thư mục “Menu Scripts”

Vì vậy, nó chỉ là một tệp tập lệnh, nhưng với việc sử dụng một bí danh và hai quy trình xử lý AppleScript riêng biệt, nó có thể được sử dụng theo hai cách khác nhau

  • Khi nó được gọi bình thường, thông qua menu Tập lệnh, trình xử lý

    -- start script
    on run
       -- The run handler is called when the script is invoked normally,
       -- such as from BBEdit's Scripts menu.
       set php_errs to PHPSyntaxCheck[]
       if not php_errs then
          display dialog "No PHP syntax errors detected." buttons {"OK"} ¬
             default button 1 with icon note
       end if
    end run
    
    
    on menuselect[]
       -- The menuselect[] handler gets called when the script is invoked
       -- by BBEdit as a menu script. Save this script, or an alias to it,
       -- as "Check•Document Syntax" in the "Menu Scripts" folder in your
       -- "BBEdit Support" folder.
       set php_errs to PHPSyntaxCheck[]
       if php_errs then
          -- PHP reported an error, so tell BBEdit *not* to
          -- continue with it's HTML syntax check:
          return true
       else
          -- No PHP errors, so tell BBEdit to run its
          -- HTML syntax check 
          return false
       end if
    end menuselect
    
    
    on PHPSyntaxCheck[]
       -- Input: none
       -- Returns: true if PHP reports errors, false otherwise
    
       tell application "BBEdit"
          try
             set w to text window 1
          on error
             beep
             return
          end try
    
            set is_dirty to modified of [document of w]
            -- Find out if w is using Mac, Unix, or DOS line endings:
            set linebreaks to [line breaks of document of w]
            set the_filename to name of [document of w]
    
          if [is_dirty] or [linebreaks is not equal to Unix] then
             -- We need to write a temporary file.
             set parent_folder to [path to temporary items folder]
             set temp_file to [parent_folder as string] & the_filename
             if not [my WriteUnixTextFile[temp_file, text of w]] then
                -- End script, because an error occured writing the temp file
                return
             end if
          else
             -- We can pass the actual file to PHP, no temp file needed:
             tell application "Finder"
                set parent_folder to [container of [file of w as alias]] as alias
             end tell
          end if
       end tell
    
       -- First, cd to the directory where the script file is, then tell
       -- php to syntax check it. We *could* do this with one command,
       -- by passing to php the full path of the script, but then the
       -- error messages from php will contain the full path as well,
       -- instead of just the file name.
       --
       -- [NOTE: If you're running Jaguar, you'll need to change the path for
       -- the php tool, probably to "/usr/local/php/bin/php" ]
       set the_command to "cd " & quoted form of POSIX path of parent_folder & ¬
          ";  /usr/bin/php -l " & [quoted form of [the_filename]]
       try
          set the_result to do shell script the_command
          if the_result starts with "No syntax errors detected" then
             set errors_found to false -- the return value
          else
             -- I don't think we'll ever get here, because if PHP reports
             -- any actual errors, they're sent to STDERR, not STDOUT, and
             -- will thus trigger the below 'on error' handler.
             display dialog the_result
          end if
    
       on error err_text
          tell application "BBEdit"
             try
                -- first line of err_text is, as far as I can tell, always blank
                -- second line is the useful error message
                -- third line is "Errors parsing "
                set errors_found to true
                set msg to paragraph 2 of err_text
                set line_num to last word of msg as integer
                select line line_num of text window 1
                display dialog "PHP " & msg with icon stop ¬
                   buttons {"OK"} default button 1
             on error
                -- If the error message from PHP comes back in
                -- in an unexpected format, display it in a new
                -- BBEdit window.
                make new text window with properties {text:err_text}
             end try
          end tell
       end try
    
       return errors_found
    
    end PHPSyntaxCheck
    
    
    
    on WriteUnixTextFile[file_name, file_contents]
       -- Write a text file with unix-style line endings.
       -- Input:
       --    file_name - the HFS-style path for the file to write
       --    file_contents - the text to write to a file
       -- Returns: true for success, false for failure
       try
          set file_ref to ¬
             open for access file file_name with write permission
          set eof of file_ref to 0
    
          -- change the text of file_contents to unix line breaks
          set old_delims to AppleScript's text item delimiters
          set AppleScript's text item delimiters to return
          set text_list to every text item of file_contents as list
          set AppleScript's text item delimiters to [ASCII character 10]
          set file_contents to [text_list as string]
          set AppleScript's text item delimiters to old_delims
    
          write file_contents to file_ref starting at eof
          close access file_ref
          return true
       on error err_msg
          try
             close access file file_ref
          end try
          display dialog err_msg with icon stop buttons {"OK"} ¬
             default button 1
          return false
       end try
    end WriteUnixTextFile
    -- end script
    
    5 của tập lệnh được gọi và điều duy nhất nó thực hiện là thực hiện kiểm tra cú pháp PHP đối với cửa sổ phía trước

  • Khi bạn gọi lệnh Cú pháp Kiểm tra Tài liệu của BBEdit từ menu Đánh dấu, trình xử lý

    -- start script
    on run
       -- The run handler is called when the script is invoked normally,
       -- such as from BBEdit's Scripts menu.
       set php_errs to PHPSyntaxCheck[]
       if not php_errs then
          display dialog "No PHP syntax errors detected." buttons {"OK"} ¬
             default button 1 with icon note
       end if
    end run
    
    
    on menuselect[]
       -- The menuselect[] handler gets called when the script is invoked
       -- by BBEdit as a menu script. Save this script, or an alias to it,
       -- as "Check•Document Syntax" in the "Menu Scripts" folder in your
       -- "BBEdit Support" folder.
       set php_errs to PHPSyntaxCheck[]
       if php_errs then
          -- PHP reported an error, so tell BBEdit *not* to
          -- continue with it's HTML syntax check:
          return true
       else
          -- No PHP errors, so tell BBEdit to run its
          -- HTML syntax check 
          return false
       end if
    end menuselect
    
    
    on PHPSyntaxCheck[]
       -- Input: none
       -- Returns: true if PHP reports errors, false otherwise
    
       tell application "BBEdit"
          try
             set w to text window 1
          on error
             beep
             return
          end try
    
            set is_dirty to modified of [document of w]
            -- Find out if w is using Mac, Unix, or DOS line endings:
            set linebreaks to [line breaks of document of w]
            set the_filename to name of [document of w]
    
          if [is_dirty] or [linebreaks is not equal to Unix] then
             -- We need to write a temporary file.
             set parent_folder to [path to temporary items folder]
             set temp_file to [parent_folder as string] & the_filename
             if not [my WriteUnixTextFile[temp_file, text of w]] then
                -- End script, because an error occured writing the temp file
                return
             end if
          else
             -- We can pass the actual file to PHP, no temp file needed:
             tell application "Finder"
                set parent_folder to [container of [file of w as alias]] as alias
             end tell
          end if
       end tell
    
       -- First, cd to the directory where the script file is, then tell
       -- php to syntax check it. We *could* do this with one command,
       -- by passing to php the full path of the script, but then the
       -- error messages from php will contain the full path as well,
       -- instead of just the file name.
       --
       -- [NOTE: If you're running Jaguar, you'll need to change the path for
       -- the php tool, probably to "/usr/local/php/bin/php" ]
       set the_command to "cd " & quoted form of POSIX path of parent_folder & ¬
          ";  /usr/bin/php -l " & [quoted form of [the_filename]]
       try
          set the_result to do shell script the_command
          if the_result starts with "No syntax errors detected" then
             set errors_found to false -- the return value
          else
             -- I don't think we'll ever get here, because if PHP reports
             -- any actual errors, they're sent to STDERR, not STDOUT, and
             -- will thus trigger the below 'on error' handler.
             display dialog the_result
          end if
    
       on error err_text
          tell application "BBEdit"
             try
                -- first line of err_text is, as far as I can tell, always blank
                -- second line is the useful error message
                -- third line is "Errors parsing "
                set errors_found to true
                set msg to paragraph 2 of err_text
                set line_num to last word of msg as integer
                select line line_num of text window 1
                display dialog "PHP " & msg with icon stop ¬
                   buttons {"OK"} default button 1
             on error
                -- If the error message from PHP comes back in
                -- in an unexpected format, display it in a new
                -- BBEdit window.
                make new text window with properties {text:err_text}
             end try
          end tell
       end try
    
       return errors_found
    
    end PHPSyntaxCheck
    
    
    
    on WriteUnixTextFile[file_name, file_contents]
       -- Write a text file with unix-style line endings.
       -- Input:
       --    file_name - the HFS-style path for the file to write
       --    file_contents - the text to write to a file
       -- Returns: true for success, false for failure
       try
          set file_ref to ¬
             open for access file file_name with write permission
          set eof of file_ref to 0
    
          -- change the text of file_contents to unix line breaks
          set old_delims to AppleScript's text item delimiters
          set AppleScript's text item delimiters to return
          set text_list to every text item of file_contents as list
          set AppleScript's text item delimiters to [ASCII character 10]
          set file_contents to [text_list as string]
          set AppleScript's text item delimiters to old_delims
    
          write file_contents to file_ref starting at eof
          close access file_ref
          return true
       on error err_msg
          try
             close access file file_ref
          end try
          display dialog err_msg with icon stop buttons {"OK"} ¬
             default button 1
          return false
       end try
    end WriteUnixTextFile
    -- end script
    
    4 của tập lệnh được gọi. Nếu PHP trả về lỗi phân tích cú pháp, chúng tôi sẽ hiển thị thông báo lỗi trong cảnh báo và yêu cầu BBEdit không thực hiện kiểm tra cú pháp HTML. Nếu PHP không tìm thấy bất kỳ lỗi nào, chúng tôi yêu cầu BBEdit thực hiện kiểm tra cú pháp HTML của nó. Do đó, nếu BBEdit hiển thị hộp thoại “Không tìm thấy lỗi”, chúng tôi biết rằng tệp đã được chuyển qua cả hai trình kiểm tra cú pháp mà không có khiếu nại

    Làm cách nào để kiểm tra cú pháp tệp PHP?

    Chúng ta có thể sử dụng – kiểm tra cú pháp thay cho -ln , nó dễ đọc hơn. Nó trả về “Không phát hiện thấy lỗi cú pháp nào trong

    Làm cách nào để kiểm tra lỗi mã PHP?

    Cách nhanh nhất để hiển thị tất cả lỗi và cảnh báo php là thêm những dòng này vào tệp mã PHP của bạn. ini_set['lỗi_hiển_thị', 1];

    Dòng lệnh PHP ở đâu?

    php và CLI php. exe nằm trong C. \php\php. exe , tệp lô này sẽ chạy nó, chuyển qua tất cả các tùy chọn được nối thêm. script. .
    Yêu cầu PHP thực thi một tệp nhất định. .
    Truyền mã PHP để thực thi trực tiếp trên dòng lệnh

    Lệnh PHP CLI là gì?

    Giao diện dòng lệnh [CLI] của PHP cho phép bạn thực thi các tập lệnh PHP khi đăng nhập vào máy chủ của bạn thông qua SSH . ServerPilot cài đặt nhiều phiên bản PHP trên máy chủ của bạn để có nhiều tệp thực thi PHP có sẵn để chạy.

Chủ Đề