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

Uri\WhatWg\Url::withUsername

PHP function Edit on GitHub ✎

(PHP 8 >= 8.5.0)

Modify the username component

Description

Uri\WhatWg\Url::withUsername(string|null $username): static

Creates a new URL and modifies its username component.

Parameters

username

New username component.

Return Values

The modified Uri\WhatWg\Url instance.

Errors/Exceptions InvalidUrlException

Examples

Uri\WhatWg\Url::withUsername basic example

php
<?php
$url = new \Uri\WhatWg\Url("https://user:password@example.com");
$url = $url->withUsername("usr");

echo $url->getUsername();
?>

The above example will output:

output
usr

See Also

  • Uri\WhatWg\Url::getUsername
  • Uri\WhatWg\Url::getPassword
  • Uri\Rfc3986\Uri::withUserInfo

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