Thay đổi phiên bản php mặc định mac

Bây giờ, giả sử bạn đã cài đặt PHP 7. 4 và PHP8. 1 với HomeBrew. Nếu bạn muốn chuyển từ PHP 7. 4 to PHP 8. 1, bạn cần chạy các lệnh sau

brew unlink [email protected]

brew link [email protected] --force --overwrite

Làm thế nào về điều này thay vào đó?

8.1

Tập lệnh shell sau (được thêm vào

# determine versions of PHP installed with HomeBrew
installedPhpVersions=($(brew ls --versions | ggrep -E 'php(@.*)?\s' | ggrep -oP '(?<=\s)\d\.\d' | uniq | sort))

# create alias for every version of PHP installed with HomeBrew
for phpVersion in ${installedPhpVersions[*]}; do
    value="{"

    for otherPhpVersion in ${installedPhpVersions[*]}; do
        if [ "${otherPhpVersion}" = "${phpVersion}" ]; then
            continue
        fi

        # unlink other PHP version
        value="${value} brew unlink php@${otherPhpVersion};"
    done

    # link desired PHP version
    value="${value} brew link php@${phpVersion} --force --overwrite; } &> /dev/null && php -v"

    alias "${phpVersion}"="${value}"
done
2 hoặc
# determine versions of PHP installed with HomeBrew
installedPhpVersions=($(brew ls --versions | ggrep -E 'php(@.*)?\s' | ggrep -oP '(?<=\s)\d\.\d' | uniq | sort))

# create alias for every version of PHP installed with HomeBrew
for phpVersion in ${installedPhpVersions[*]}; do
    value="{"

    for otherPhpVersion in ${installedPhpVersions[*]}; do
        if [ "${otherPhpVersion}" = "${phpVersion}" ]; then
            continue
        fi

        # unlink other PHP version
        value="${value} brew unlink php@${otherPhpVersion};"
    done

    # link desired PHP version
    value="${value} brew link php@${phpVersion} --force --overwrite; } &> /dev/null && php -v"

    alias "${phpVersion}"="${value}"
done
3, tùy thuộc vào shell nào bạn thích) tạo bí danh cho mọi phiên bản PHP mà bạn đã cài đặt với Homebrew

# determine versions of PHP installed with HomeBrew
installedPhpVersions=($(brew ls --versions | ggrep -E 'php(@.*)?\s' | ggrep -oP '(?<=\s)\d\.\d' | uniq | sort))

# create alias for every version of PHP installed with HomeBrew
for phpVersion in ${installedPhpVersions[*]}; do
    value="{"

    for otherPhpVersion in ${installedPhpVersions[*]}; do
        if [ "${otherPhpVersion}" = "${phpVersion}" ]; then
            continue
        fi

        # unlink other PHP version
        value="${value} brew unlink php@${otherPhpVersion};"
    done

    # link desired PHP version
    value="${value} brew link php@${phpVersion} --force --overwrite; } &> /dev/null && php -v"

    alias "${phpVersion}"="${value}"
done

💡 macOS đi kèm với

brew link [email protected] --force --overwrite
0, nhưng phiên bản đó không hỗ trợ các biểu thức chính quy (PCRE) tương thích với Perl như được sử dụng ở trên

Chạy

brew install grep

để cài đặt phiên bản mới nhất của

brew link [email protected] --force --overwrite
0. Homebrew cài đặt ________ 10 với tiền tố ________ 13, do đó ________ 14

Điểm khó khăn này là lý do tại sao tôi xây dựng dự án phụ mới nhất của mình,

# determine versions of PHP installed with HomeBrew
installedPhpVersions=($(brew ls --versions | ggrep -E 'php(@.*)?\s' | ggrep -oP '(?<=\s)\d\.\d' | uniq | sort))

# create alias for every version of PHP installed with HomeBrew
for phpVersion in ${installedPhpVersions[*]}; do
    value="{"

    for otherPhpVersion in ${installedPhpVersions[*]}; do
        if [ "${otherPhpVersion}" = "${phpVersion}" ]; then
            continue
        fi

        # unlink other PHP version
        value="${value} brew unlink php@${otherPhpVersion};"
    done

    # link desired PHP version
    value="${value} brew link php@${phpVersion} --force --overwrite; } &> /dev/null && php -v"

    alias "${phpVersion}"="${value}"
done
4;

Hiện tại có một số phương pháp cài đặt

  1. Bạn có thể cài đặt với NPM.
    # determine versions of PHP installed with HomeBrew
    installedPhpVersions=($(brew ls --versions | ggrep -E 'php(@.*)?\s' | ggrep -oP '(?<=\s)\d\.\d' | uniq | sort))
    
    # create alias for every version of PHP installed with HomeBrew
    for phpVersion in ${installedPhpVersions[*]}; do
        value="{"
    
        for otherPhpVersion in ${installedPhpVersions[*]}; do
            if [ "${otherPhpVersion}" = "${phpVersion}" ]; then
                continue
            fi
    
            # unlink other PHP version
            value="${value} brew unlink php@${otherPhpVersion};"
        done
    
        # link desired PHP version
        value="${value} brew link php@${phpVersion} --force --overwrite; } &> /dev/null && php -v"
    
        alias "${phpVersion}"="${value}"
    done
    
    5
  2. Bạn có thể cài đặt với Yarn.
    # determine versions of PHP installed with HomeBrew
    installedPhpVersions=($(brew ls --versions | ggrep -E 'php(@.*)?\s' | ggrep -oP '(?<=\s)\d\.\d' | uniq | sort))
    
    # create alias for every version of PHP installed with HomeBrew
    for phpVersion in ${installedPhpVersions[*]}; do
        value="{"
    
        for otherPhpVersion in ${installedPhpVersions[*]}; do
            if [ "${otherPhpVersion}" = "${phpVersion}" ]; then
                continue
            fi
    
            # unlink other PHP version
            value="${value} brew unlink php@${otherPhpVersion};"
        done
    
        # link desired PHP version
        value="${value} brew link php@${phpVersion} --force --overwrite; } &> /dev/null && php -v"
    
        alias "${phpVersion}"="${value}"
    done
    
    6
  3. Bạn có thể tải xuống tệp
    # determine versions of PHP installed with HomeBrew
    installedPhpVersions=($(brew ls --versions | ggrep -E 'php(@.*)?\s' | ggrep -oP '(?<=\s)\d\.\d' | uniq | sort))
    
    # create alias for every version of PHP installed with HomeBrew
    for phpVersion in ${installedPhpVersions[*]}; do
        value="{"
    
        for otherPhpVersion in ${installedPhpVersions[*]}; do
            if [ "${otherPhpVersion}" = "${phpVersion}" ]; then
                continue
            fi
    
            # unlink other PHP version
            value="${value} brew unlink php@${otherPhpVersion};"
        done
    
        # link desired PHP version
        value="${value} brew link php@${phpVersion} --force --overwrite; } &> /dev/null && php -v"
    
        alias "${phpVersion}"="${value}"
    done
    
    7 theo cách thủ công từ kho lưu trữ
    # determine versions of PHP installed with HomeBrew
    installedPhpVersions=($(brew ls --versions | ggrep -E 'php(@.*)?\s' | ggrep -oP '(?<=\s)\d\.\d' | uniq | sort))
    
    # create alias for every version of PHP installed with HomeBrew
    for phpVersion in ${installedPhpVersions[*]}; do
        value="{"
    
        for otherPhpVersion in ${installedPhpVersions[*]}; do
            if [ "${otherPhpVersion}" = "${phpVersion}" ]; then
                continue
            fi
    
            # unlink other PHP version
            value="${value} brew unlink php@${otherPhpVersion};"
        done
    
        # link desired PHP version
        value="${value} brew link php@${phpVersion} --force --overwrite; } &> /dev/null && php -v"
    
        alias "${phpVersion}"="${value}"
    done
    
    4 và chuyển tệp đó vào thư mục chính của bạn. Sau đó, bạn sẽ cần thêm dòng này vào
    # determine versions of PHP installed with HomeBrew
    installedPhpVersions=($(brew ls --versions | ggrep -E 'php(@.*)?\s' | ggrep -oP '(?<=\s)\d\.\d' | uniq | sort))
    
    # create alias for every version of PHP installed with HomeBrew
    for phpVersion in ${installedPhpVersions[*]}; do
        value="{"
    
        for otherPhpVersion in ${installedPhpVersions[*]}; do
            if [ "${otherPhpVersion}" = "${phpVersion}" ]; then
                continue
            fi
    
            # unlink other PHP version
            value="${value} brew unlink php@${otherPhpVersion};"
        done
    
        # link desired PHP version
        value="${value} brew link php@${phpVersion} --force --overwrite; } &> /dev/null && php -v"
    
        alias "${phpVersion}"="${value}"
    done
    
    9/
    brew link [email protected] --force --overwrite
    
    0/etc của mình.
    brew link [email protected] --force --overwrite
    
    1

Khi bạn cài đặt nó thông qua một trong các phương pháp trên, bạn sẽ có thể truy cập

# determine versions of PHP installed with HomeBrew
installedPhpVersions=($(brew ls --versions | ggrep -E 'php(@.*)?\s' | ggrep -oP '(?<=\s)\d\.\d' | uniq | sort))

# create alias for every version of PHP installed with HomeBrew
for phpVersion in ${installedPhpVersions[*]}; do
    value="{"

    for otherPhpVersion in ${installedPhpVersions[*]}; do
        if [ "${otherPhpVersion}" = "${phpVersion}" ]; then
            continue
        fi

        # unlink other PHP version
        value="${value} brew unlink php@${otherPhpVersion};"
    done

    # link desired PHP version
    value="${value} brew link php@${phpVersion} --force --overwrite; } &> /dev/null && php -v"

    alias "${phpVersion}"="${value}"
done
4 trên toàn cầu

Đây là một ví dụ tối thiểu về cách sử dụng nó

brew link [email protected] --force --overwrite
0

Đây là những gì chúng tôi đang làm

  1. # determine versions of PHP installed with HomeBrew
    installedPhpVersions=($(brew ls --versions | ggrep -E 'php(@.*)?\s' | ggrep -oP '(?<=\s)\d\.\d' | uniq | sort))
    
    # create alias for every version of PHP installed with HomeBrew
    for phpVersion in ${installedPhpVersions[*]}; do
        value="{"
    
        for otherPhpVersion in ${installedPhpVersions[*]}; do
            if [ "${otherPhpVersion}" = "${phpVersion}" ]; then
                continue
            fi
    
            # unlink other PHP version
            value="${value} brew unlink php@${otherPhpVersion};"
        done
    
        # link desired PHP version
        value="${value} brew link php@${phpVersion} --force --overwrite; } &> /dev/null && php -v"
    
        alias "${phpVersion}"="${value}"
    done
    
    4→ Lệnh chính. (Yêu cầu)
  2. brew link [email protected] --force --overwrite
    
    12 → Chỉ định phiên bản PHP mà bạn cũng muốn chuyển đổi, trong trường hợp này, chúng tôi yêu cầu
    brew link [email protected] --force --overwrite
    
    13. (Yêu cầu)

Tuy nhiên, bạn cũng có thể chuyển một số tham số tùy chọn cung cấp một số chức năng bổ sung. Ví dụ

brew link [email protected] --force --overwrite
1
  • brew link [email protected] --force --overwrite
    
    14 → Yêu cầu đầu ra chi tiết (Tùy chọn)
  • brew link [email protected] --force --overwrite
    
    15 → Yêu cầu cài đặt bộ nhớ PHP tùy chỉnh. Nếu bạn không chuyển đối số bổ sung, bộ nhớ sẽ được đặt lại về giá trị mặc định của Valet. (Không bắt buộc)

Vui lòng tải xuống

# determine versions of PHP installed with HomeBrew
installedPhpVersions=($(brew ls --versions | ggrep -E 'php(@.*)?\s' | ggrep -oP '(?<=\s)\d\.\d' | uniq | sort))

# create alias for every version of PHP installed with HomeBrew
for phpVersion in ${installedPhpVersions[*]}; do
    value="{"

    for otherPhpVersion in ${installedPhpVersions[*]}; do
        if [ "${otherPhpVersion}" = "${phpVersion}" ]; then
            continue
        fi

        # unlink other PHP version
        value="${value} brew unlink php@${otherPhpVersion};"
    done

    # link desired PHP version
    value="${value} brew link php@${phpVersion} --force --overwrite; } &> /dev/null && php -v"

    alias "${phpVersion}"="${value}"
done
4, dùng thử và cho tôi biết suy nghĩ của bạn. Mọi lỗi hoặc yêu cầu tính năng có thể được thêm vào repo Github. Nếu bạn thấy
# determine versions of PHP installed with HomeBrew
installedPhpVersions=($(brew ls --versions | ggrep -E 'php(@.*)?\s' | ggrep -oP '(?<=\s)\d\.\d' | uniq | sort))

# create alias for every version of PHP installed with HomeBrew
for phpVersion in ${installedPhpVersions[*]}; do
    value="{"

    for otherPhpVersion in ${installedPhpVersions[*]}; do
        if [ "${otherPhpVersion}" = "${phpVersion}" ]; then
            continue
        fi

        # unlink other PHP version
        value="${value} brew unlink php@${otherPhpVersion};"
    done

    # link desired PHP version
    value="${value} brew link php@${phpVersion} --force --overwrite; } &> /dev/null && php -v"

    alias "${phpVersion}"="${value}"
done
4 hữu ích trong quy trình làm việc của mình, vui lòng chia sẻ nó với bạn bè của bạn và giúp họ thưởng thức Brewing PHP một lần nữa. Cảm ơn

Như mọi khi, nếu bạn thấy bài đăng này hữu ích hoặc thích đọc, vui lòng theo dõi tôi tại đây hoặc trên Twitter để xem các bài đăng trong tương lai

Các phiên bản macOS mới nhất của Big Sur và Catalina hiện được bán kèm với PHP 7. 3 và có thông báo không dùng nữa rằng họ có ý định ngừng hỗ trợ PHP hoàn toàn trong bản nâng cấp trong tương lai

Bạn có thể thấy điều này bằng cách chạy hàm

brew link [email protected] --force --overwrite
67 trong tệp được lưu trữ trên web hoặc chạy
brew link [email protected] --force --overwrite
68 trên dòng lệnh

brew link [email protected] --force --overwrite
8

Vì vậy, giải pháp tốt nhất là sử dụng Homebrew để cài đặt và sau đó chọn bất kỳ phiên bản PHP nào bạn muốn, bao gồm cả 7. 4 và phiên bản 8

Vì vậy, khi bạn đã cài đặt Homebrew trên macOS của mình, một công thức PHP Homebrew cụ thể sẽ cần được thêm từ trình bảo trì này, điều tuyệt vời ở đây là tất cả các phiên bản PHP đều khả dụng trong khi chỉ một số phiên bản khả dụng trên các công thức Homebrew mặc định

Sau khi cài đặt Homebrew, hãy làm theo bên dưới

Thêm các công thức PHP

brew link [email protected] --force --overwrite
6

Chọn phiên bản PHP – ví dụ sử dụng 7. 4

8.1
0

các tùy chọn khác là

  • [email được bảo vệ]
  • [email được bảo vệ]
  • [email được bảo vệ]
  • [email được bảo vệ]
  • [email được bảo vệ]
  • [email được bảo vệ]
  • [email được bảo vệ] hay còn gọi là php
  • [email được bảo vệ]

Liên kết phiên bản PHP

8.1
1

Thay đổi phiên bản của bạn để phù hợp

Khởi động lại thiết bị đầu cuối

Chạy…

8.1
2

Bây giờ bạn sẽ thấy phiên bản mới, để thay đổi sang phiên bản khác, chỉ cần lặp lại quy trình từ

brew link [email protected] --force --overwrite
69, sau đó hủy liên kết và liên kết trong phiên bản PHP mới bằng cách đưa ra lệnh như bên dưới nhưng với phiên bản chính xác của bạn

8.1
4

Sử dụng PHP mới trong macOS Đã vận chuyển Apache

Nếu bạn đang sử dụng macOS trong Apache được vận chuyển sẵn, bạn có thể sử dụng phiên bản PHP mới bằng cách chỉnh sửa tệp Apache

8.1
5

Tìm mô-đun PHP và nhận xét nó và thêm vào đường dẫn phiên bản PHP mới

8.1
6

Khởi động lại Apache

8.1
7

Bây giờ phiên bản PHP mới sẽ được sử dụng

Bạn có thể tải xuống tất cả các phiên bản và chỉ cần bỏ ghi chú phiên bản bạn muốn nếu bạn có ý định trao đổi xung quanh

8.1
8

PHP 8 và macOS Apache

Cần thêm một bước nữa cho Apache đi kèm PHP 8 và macOS

8.1
5

Thêm PHP 8 mới và nhận xét cái cũ

brew link [email protected] --force --overwrite
61

Đi đến cuối tập tin và thêm

brew link [email protected] --force --overwrite
62

Khởi động lại Apache

Xóa phiên bản Homebrew PHP cũ hơn (tùy chọn)

Nếu bạn có các công thức PHP cũ hơn từ bản cài đặt Homebrew cũ hơn, bạn có thể muốn xóa các công thức này

PHP có được cài đặt trên máy Mac theo mặc định không?

Kể từ macOS Monterey, PHP không còn được bao gồm trong cài đặt mặc định của hệ thống . Nếu bạn muốn sử dụng PHP trên macOS Monterey, trước tiên bạn phải cài đặt PHP. Các hướng dẫn sau đây hiển thị các lệnh Terminal để cài đặt PHP.

Tệp cấu hình PHP trong Mac ở đâu?

Đoán xem nó ở đâu