# PHP vs Go

[Original page](https://codewithphp.com/compare/php-vs-go/)

> Compare PHP and Go for web backends, command-line tools and deployment. See how each handles invalid input in a small program.

PHP gives you a web-focused ecosystem with CMSs and comprehensive frameworks. Go gives you compiled programs and standard-library tools for networking and concurrency. A small web app and a background network service can use different languages while sharing the same data and HTTP interfaces.

| Decision | PHP | Go |
| --- | --- | --- |
| What gets deployed | Source and dependencies alongside a PHP runtime and its extensions. | A compiled executable built for the target operating system and architecture. Some builds depend on system libraries. |
| Error style | Built-in APIs may throw exceptions, return a failure value or use both conventions. Check the specific function. | Many APIs return a value and an `error` that the caller checks explicitly. |
| Data | Arrays, objects and declared parameter and return types. | Statically typed structs, slices and maps. |
| Serving HTTP | Frameworks cover routing, templates, and many application features. | The standard `net/http` package provides an HTTP server and client. |

## Reject an invalid port

```php
<?php
declare(strict_types=1);

function readPort(string $value): ?int
{
    $port = filter_var($value, FILTER_VALIDATE_INT);
    return $port !== false && $port >= 1 && $port <= 65535 ? $port : null;
}

echo readPort('8000') ?? 'invalid', PHP_EOL;
echo readPort('abc') ?? 'invalid', PHP_EOL;
```

```go
package main

import (
	"fmt"
	"strconv"
)

func showPort(value string) {
	port, err := strconv.Atoi(value)
	if err != nil || port < 1 || port > 65535 {
		fmt.Println("invalid")
		return
	}
	fmt.Println(port)
}

func main() {
	showPort("8000")
	showPort("abc")
}
```

Both print `8000` followed by `invalid`. Run the examples with `php example.php` and `go run example.go`. This PHP function returns `null` when validation fails. The Go parser returns an error for failed conversion, and the caller also checks the allowed range. These samples agree on the shown inputs. For a shared configuration format, define which spellings to accept and add tests for leading zeroes, whitespace and signs.

## What makes the choice practical

Pick the deployment that best fits the application. An existing PHP app with database access and sessions can usually handle another small API without introducing a separate service. A network tool shipped to several environments may benefit from Go’s executable packaging. Compare the standard library or framework you would actually use and measure the workload you plan to run.

- [Effective Go](https://go.dev/doc/effective_go)
- [Go net/http package](https://pkg.go.dev/net/http)
- [PHP filter_var](https://www.php.net/manual/en/function.filter-var.php)
- [PHP vs Rust](/compare/php-vs-rust/) · [All PHP comparisons](/compare/)
