Str::upper

Converts a string to uppercase with full multibyte support.

Signature
Str::upper(string $value): string
  • Added in Laravel 4.0

Parameters

ParameterTypeDescription
$$valuestringThe string to uppercase. Interpreted as UTF-8.

Return value

stringThe uppercased string. The result may be longer than the input because some characters expand when uppercased.

Str::upper calls mb_strtoupper($value, 'UTF-8'). The reason to prefer it over strtoupper is that PHP's built-in works one byte at a time against the C locale, so strtoupper('café') returns CAFé — the accented character is left alone because it is not a single ASCII byte.

Reach for it when case is part of a format or a comparison: country and currency codes, ticket or coupon references, header names, or normalising both sides before an equality check.

What trips people up

  • The length can change. German ß uppercases to SS, so straße (6 characters) becomes STRASSE (7). A column sized to the input length will overflow, and the transformation is not reversible.
  • It is not locale aware. Turkish dotless ı and dotted i do not follow Turkish rules; the Unicode default mapping is used regardless of the app locale.
  • Uppercasing is the wrong way to compare strings case-insensitively for anything beyond ASCII. Use mb_convert_case with MB_CASE_FOLD, or a database collation, when correctness matters.
  • Uppercase text is harder to read and can be announced letter by letter by screen readers. Prefer text-transform in CSS for purely visual emphasis.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Normalise an ISO country or currency code before looking it up in a table.
  • Format a generated coupon or reference code consistently for display and lookup.
  • Uppercase both operands before an ASCII-only equality check on an enum-like value.

Frequently asked questions

Why use Str::upper instead of strtoupper?

strtoupper operates byte by byte and leaves accented and non-Latin characters untouched, so strtoupper('café') returns 'CAFé'. Str::upper wraps mb_strtoupper with UTF-8 and handles the whole string correctly.

Can Str::upper change the length of my string?

Yes. Characters such as the German ß uppercase to two letters, so 'straße' becomes the seven-character 'STRASSE'. Size database columns for the output, not the input.

Is Str::upper safe for case-insensitive comparison?

Only for ASCII. For anything else use mb_convert_case with MB_CASE_FOLD, or let the database do it with a case-insensitive collation, because uppercasing collapses distinctions such as ß versus ss.

More examples

Normalising codes before a lookup

Prints 1, 0.92 and 32.5 — squishing then uppercasing makes the array lookup work regardless of how the code was typed. The final dump is "ÜRÜN KODU", where strtoupper would have returned "üRüN KODU" and broken any comparison against a stored uppercase value.

Example · php
<?php

use Illuminate\Support\Str;

$rates = ['USD' => 1.0, 'EUR' => 0.92, 'TRY' => 32.5];

foreach (['usd', 'Eur', 'trY'] as $input) {
    $code = Str::upper(Str::squish($input));

    echo $input, ' => ', $rates[$code] ?? 'unknown', PHP_EOL;
}

// Non-ASCII input is handled correctly.
var_dump(Str::upper('ürün kodu'));

When uppercasing is the wrong comparison tool

The first dump is true because ß uppercases to SS, collapsing a real distinction between two different strings. The second is also true here, since Unicode case folding maps ß to ss by design — the point is that folding is the documented primitive for caseless matching, while uppercasing happens to work only for ASCII and has surprising edge cases everywhere else.

Example · php
<?php

use Illuminate\Support\Str;

$a = 'STRASSE';
$b = 'Straße';

// Uppercasing makes these compare equal, which is probably not intended.
var_dump(Str::upper($a) === Str::upper($b));

// Case folding is the correct primitive for case-insensitive matching.
var_dump(
    mb_convert_case($a, MB_CASE_FOLD, 'UTF-8')
    === mb_convert_case($b, MB_CASE_FOLD, 'UTF-8')
);

Discussion

  • Be the first to comment on this lesson.