ParagonIE_Sodium_Core_Util::strlen( string $str ): int
- Source
wp-includes/sodium_compat/src/Core/Util.php:786
Calculates the byte length of a string, bypassing mbstring overloading so sodium_compat always counts bytes not characters. It is an internal helper of the bundled sodium_compat polyfill used throughout WordPress's AEAD encryption and authentication routines, not a public API most plugins should call directly. Passing anything other than a string throws a native TypeError, and pairing it with ParagonIE_Sodium_Core_Util::substr() is how the library slices ciphertext safely byte by byte.
Compatibility
- WordPress
- core
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$strstring
Return value
int
Code examples
Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Compare byte length vs character length for a UTF-8 string
Run both a plain ASCII string and a string with accented characters through the sodium helper and mb_strlen() side by side.
$ascii = 'Hello world!';
$utf8 = 'Héllo wörld!';
printf(
'ASCII: %s -> %d bytes (safe strlen) vs %d chars (mb_strlen)<br>',
esc_html( $ascii ),
ParagonIE_Sodium_Core_Util::strlen( $ascii ),
mb_strlen( $ascii )
);
printf(
'UTF-8: %s -> %d bytes (safe strlen) vs %d chars (mb_strlen)',
esc_html( $utf8 ),
ParagonIE_Sodium_Core_Util::strlen( $utf8 ),
mb_strlen( $utf8 )
);The accented characters take two bytes each in UTF-8, so the safe byte count is higher than the mb_strlen character count.
See what happens when a non-string value is passed in
Loop over a mix of a real string, an integer and null to trigger the method's own type check.
$values = array( 'a valid string', 42, null );
foreach ( $values as $value ) {
try {
$length = ParagonIE_Sodium_Core_Util::strlen( $value );
printf( 'Value of type %s has length %d bytes<br>', esc_html( gettype( $value ) ), $length );
} catch ( TypeError $e ) {
printf( 'Rejected %s: %s<br>', esc_html( gettype( $value ) ), esc_html( $e->getMessage() ) );
}
}The TypeError comes straight from the method's own is_string() guard, not from PHP's native strict typing.
Common problems and fixes · 3
- Why do I get a TypeError when calling this method?
- Why does my byte count not match mb_strlen()?
- Does server configuration change what this returns?
Why do I get a TypeError when calling this method?
Why does my byte count not match mb_strlen()?
Does server configuration change what this returns?
Alternatives and related functions
strlen- When you just need a byte count in ordinary PHP code and don't need the extra TypeError guard this method adds.
mb_strlen- When you're counting displayable characters in a multibyte string rather than raw bytes, such as for a character-limit UI.
ParagonIE_Sodium_Core_Util::substr- When you already know the byte length and now need to slice that same string safely by byte offset.
Performance profile
How much work a call to ParagonIE_Sodium_Core_Util::strlen() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 7–13
- Plugin surface
- None
- Called by
- 50
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 18.
Nothing here hands control to plugin code.
50 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost ParagonIE_Sodium_Core_Util::strlen() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!is_string($str) | 7 | none |
is_string($str) && !::isMbStringOverride() | 9 | ::isMbStringOverride() |
is_string($str) && ::isMbStringOverride() | 13 | ::isMbStringOverride(), mb_strlen() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 18 | 7–13 | 2 | |
| 8.5 | 18 | 7–13 | 2 | |
| 8.4 | 18 | 7–13 | 2 | |
| 8.3 | 18 | 7–13 | 2 | |
| 8.2 | 18 | 7–13 | 2 | |
| 8.1 | 18 | 7–13 | 2 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 20 | 7–14 | 2 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 3
- mb_strlen()Compat function to mimic mb_strlen().
- TypeError::__construct()
- ParagonIE_Sodium_Core_Util::isMbStringOverride()Returns whether or not mbstring.func_overload is in effect.
Used by · 50
- ParagonIE_Sodium_Compat::add()Add two numbers (little-endian unsigned), storing the value in the first parameter.
- ParagonIE_Sodium_Compat::bin2base64()
- ParagonIE_Sodium_Compat::crypto_aead_aegis128l_decrypt()Authenticated Encryption with Associated Data: Decryption
- ParagonIE_Sodium_Compat::crypto_aead_aegis128l_encrypt()Authenticated Encryption with Associated Data: Encryption
- ParagonIE_Sodium_Compat::crypto_aead_aegis256_decrypt()Authenticated Encryption with Associated Data: Decryption
- ParagonIE_Sodium_Compat::crypto_aead_aegis256_encrypt()Authenticated Encryption with Associated Data: Encryption
- ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_decrypt()Authenticated Encryption with Associated Data: Decryption
- ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_encrypt()Authenticated Encryption with Associated Data: Encryption
- ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_decrypt()Authenticated Encryption with Associated Data: Decryption
- ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_encrypt()Authenticated Encryption with Associated Data
- ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_decrypt()Authenticated Encryption with Associated Data: Decryption
- ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_encrypt()Authenticated Encryption with Associated Data
Show all 50
- ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_ietf_decrypt()Authenticated Encryption with Associated Data: Decryption
- ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_ietf_encrypt()Authenticated Encryption with Associated Data
- ParagonIE_Sodium_Compat::crypto_auth()Authenticate a message. Uses symmetric-key cryptography.
- ParagonIE_Sodium_Compat::crypto_auth_verify()Verify the MAC of a message previously authenticated with crypto_auth.
- ParagonIE_Sodium_Compat::crypto_box()Authenticated asymmetric-key encryption. Both the sender and recipient may decrypt messages.
- ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey()Combine two keys into a keypair for use in library methods that expect a keypair. This doesn't necessarily have to be the same person's keys.
- ParagonIE_Sodium_Compat::crypto_box_open()Decrypt a message previously encrypted with crypto_box().
- ParagonIE_Sodium_Compat::crypto_box_publickey()Extract the public key from a crypto_box keypair.
- ParagonIE_Sodium_Compat::crypto_box_publickey_from_secretkey()Calculate the X25519 public key from a given X25519 secret key.
- ParagonIE_Sodium_Compat::crypto_box_seal()Anonymous public-key encryption. Only the recipient may decrypt messages.
- ParagonIE_Sodium_Compat::crypto_box_seal_open()Opens a message encrypted with crypto_box_seal(). Requires the recipient's keypair (sk || pk) to decrypt successfully.
- ParagonIE_Sodium_Compat::crypto_box_secretkey()Extract the secret key from a crypto_box keypair.
- ParagonIE_Sodium_Compat::crypto_generichash()Calculates a BLAKE2b hash, with an optional key.
- ParagonIE_Sodium_Compat::crypto_generichash_init()Initialize a BLAKE2b hashing context, for use in a streaming interface.
- ParagonIE_Sodium_Compat::crypto_generichash_init_salt_personal()Initialize a BLAKE2b hashing context, for use in a streaming interface.
- ParagonIE_Sodium_Compat::crypto_kdf_derive_from_key()
- ParagonIE_Sodium_Compat::crypto_kx()Perform a key exchange, between a designated client and a server.
- ParagonIE_Sodium_Compat::crypto_kx_client_session_keys()
- ParagonIE_Sodium_Compat::crypto_kx_seed_keypair()
- ParagonIE_Sodium_Compat::crypto_kx_server_session_keys()
- ParagonIE_Sodium_Compat::crypto_scalarmult()Calculate the shared secret between your secret key and your recipient's public key.
- ParagonIE_Sodium_Compat::crypto_scalarmult_base()Calculate an X25519 public key from an X25519 secret key.
- ParagonIE_Sodium_Compat::crypto_secretbox()Authenticated symmetric-key encryption.
- ParagonIE_Sodium_Compat::crypto_secretbox_open()Decrypts a message previously encrypted with crypto_secretbox().
- ParagonIE_Sodium_Compat::crypto_secretbox_xchacha20poly1305()Authenticated symmetric-key encryption.
- ParagonIE_Sodium_Compat::crypto_secretbox_xchacha20poly1305_open()Decrypts a message previously encrypted with crypto_secretbox_xchacha20poly1305().
- ParagonIE_Sodium_Compat::crypto_secretstream_xchacha20poly1305_init_pull()
- ParagonIE_Sodium_Compat::crypto_shorthash()Calculates a SipHash-2-4 hash of a message for a given key.
- ParagonIE_Sodium_Compat::crypto_sign()Returns a signed message. You probably want crypto_sign_detached() instead, which only returns the signature.
- ParagonIE_Sodium_Compat::crypto_sign_detached()Calculate the Ed25519 signature of a message and return ONLY the signature.
- ParagonIE_Sodium_Compat::crypto_sign_ed25519_pk_to_curve25519()Convert an Ed25519 public key to a Curve25519 public key
- ParagonIE_Sodium_Compat::crypto_sign_ed25519_sk_to_curve25519()Convert an Ed25519 secret key to a Curve25519 secret key
- ParagonIE_Sodium_Compat::crypto_sign_keypair_from_secretkey_and_publickey()
- ParagonIE_Sodium_Compat::crypto_sign_open()Validates a signed message then returns the message.
- ParagonIE_Sodium_Compat::crypto_sign_publickey()Extract an Ed25519 public key from an Ed25519 keypair.
- ParagonIE_Sodium_Compat::crypto_sign_publickey_from_secretkey()Calculate an Ed25519 public key from an Ed25519 secret key.
- ParagonIE_Sodium_Compat::crypto_sign_secretkey()Extract an Ed25519 secret key from an Ed25519 keypair.
- ParagonIE_Sodium_Compat::crypto_sign_verify_detached()Verify the Ed25519 signature of a message.
Source code
public static function strlen($str) { /* Type checks: */ if (!is_string($str)) { throw new TypeError('String expected'); } return (int) ( self::isMbStringOverride() ? mb_strlen($str, '8bit') : strlen($str) ); }Changelog
Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/sodium_compat/src/Core/Util.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.