random_bytes to generate cryptographically secure random bytes

echo random_bytes($length);
$length : Length of the random string returned as bytes.

Generates cryptographically secure pseudo-random bytes which can be used as Salts, keys etc.
Available in PHP 7

Example

$str=random_bytes(7);
echo bin2hex($str);
Output ( Refresh this page to to see how the random number changes )
a4809fb4fe6b5f

Example 1: Generating Cryptographically Secure API Keys

<?php
$key = bin2hex(random_bytes(16));  // Generates a 32-character API key
echo "Generated API key: $key";
?>

Example 2: Creating Random Passwords

<?php
$password = bin2hex(random_bytes(8));  // Generates a secure 16-character password
echo "Random password: $password";
?>

Example 3: Generating Salt for Hashing

<?php
$salt = bin2hex(random_bytes(16));  // 16-byte salt for hashing
echo "Generated salt: $salt";
?>

String Functions how to change all the letters to upper case nl2br() To add Line break
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer