php8.5
Home/ Manual/ whatwg / url/ Uri\WhatWg\Url::withPort

Uri\WhatWg\Url::withPort

PHP function Edit on GitHub ✎

(PHP 8 >= 8.5.0)

Modify the port component

Description

Uri\WhatWg\Url::withPort(int|null $port): static

Creates a new URL and modifies its port component.

Parameters

port

New port component.

Return Values

The modified Uri\WhatWg\Url instance.

Errors/Exceptions InvalidUrlException

Examples

Uri\WhatWg\Url::withPort basic example

php
<?php
$url = new \Uri\WhatWg\Url("https://example.com:8080");

// 443 is the default port for https, so it is not stored.
$url = $url->withPort(443);
var_dump($url->getPort());

$url = $url->withPort(8443);
var_dump($url->getPort());
?>

The above example will output:

output
NULL
int(8443)

See Also

  • Uri\WhatWg\Url::getPort
  • Uri\Rfc3986\Uri::withPort

Source: reference/uri/uri/whatwg/url/withport.xml · from the official PHP manual (php/doc-en)