Skip to content

🐘 GetPHPVersion

Gets the PHP version number currently used by the system.

When to use

Use this when you need to obtain the PHP major version string for log output, compatibility checks, or environment probing.

Signature

go
func (c *Composer) GetPHPVersion() (string, error)

Parameters

ParameterTypeDescription
NoneThis method takes no parameters

Return value

  • string: The current PHP version number, e.g. 8.2.14; returns an empty string if it cannot be parsed
  • error: Returns an error when the command execution fails

Example

go
package main

import (
    "fmt"
    "log"

    "github.com/scagogogo/composer-skills/pkg/composer"
)

func main() {
    comp, err := composer.New(composer.DefaultOptions())
    if err != nil {
        log.Fatal(err)
    }

    phpVersion, err := comp.GetPHPVersion()
    if err != nil {
        log.Fatalf("Failed to get PHP version: %v", err)
    }
    fmt.Printf("Current PHP version: %s\n", phpVersion)
}

Advanced

  • This method executes composer run --php-show-version and extracts the version number from the line starting with PHP
  • For Composer's own version information (including major/minor/patch numbers), see GetVersionInfo in result_types
  • To check whether a certain version constraint is satisfied, use IsPlatformAvailable

Released under the MIT License