PHP str_rot13() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, str_rot13() applies the ROT13 transformation, which shifts each letter 13 places in the alphabet.

Because the alphabet has 26 letters, running it twice gives back the original text. It is a simple way to hide text, not real encryption.

Syntax

Call str_rot13() with the values you need. Start with this line.

PHP
$secret = str_rot13("Hello");
CallWhat it does
str_rot13("Hello")Returns "Uryyb".
str_rot13(str_rot13("Hello"))Returns "Hello" (it undoes itself).
str_rot13("PHP 8.3!")Returns "CUC 8.3!" (digits and symbols stay).

Only English letters A to Z and a to z change. Numbers, symbols, and other characters stay as they are. Never use ROT13 to protect passwords or private data.

Library declarations
PHP
str_rot13(string $string): string

Parameters

ParameterDescription
stringRequired. The string to transform.

Return Value

Return ValueDescription
stringThe ROT13 version of the string.

PHP Examples

Example 1 — Shift Letters by 13

Transform a simple word.

PHP
<?php
echo str_rot13("Hello"), PHP_EOL;

How It Works

  1. Pass the word "Hello".
  2. Each letter moves 13 places forward in the alphabet.
  3. H becomes U, e becomes r, l becomes y, and o becomes b.
  4. The new text is "Uryyb".

Example 2 — Undo with a Second Call

Calling the function twice returns the original. Other characters are untouched.

PHP
<?php
echo str_rot13(str_rot13("Hello")), PHP_EOL;
echo str_rot13("PHP 8.3!"), PHP_EOL;

How It Works

  1. The first call turns "Hello" into "Uryyb".
  2. The second call shifts by 13 again, which returns to "Hello".
  3. In "PHP 8. 3!" only the three letters change.
  4. Digits, dots, spaces, and symbols stay the same.

Example 3 — Hide a Joke Punchline

ROT13 is often used to hide spoilers.

PHP
<?php
$spoiler = str_rot13("Why did the chicken cross the road?");

echo $spoiler, PHP_EOL;
echo str_rot13($spoiler), PHP_EOL;

How It Works

  1. Pass a full sentence to str_rot13.
  2. Every letter is shifted, so the text looks unreadable.
  3. Spaces and the question mark stay in place.
  4. Run it again to read the original sentence.

Frequently Asked Questions

It shifts every English letter 13 places in the alphabet. Running it twice returns the original text.
No. It is a simple letter swap and is easy to reverse. Never use it to protect passwords or private data.
Call str_rot13() again on the encoded text. The same function encodes and decodes.
No. Only letters A to Z and a to z change. Numbers, symbols, and spaces stay the same.
No. Only basic English letters are shifted. Other characters are left unchanged.

Next: str_shuffle()

str_shuffle() randomly rearranges the characters of a string.

str_shuffle() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful