PHP str_shuffle() Function with Example

By IncludeHelp Last updated : December 27, 2023

PHP str_shuffle() Function

The str_shuffle() function is a string function and it is used to shuffle all characters randomly in the string.

When this function executes, all time it returns shuffles string with random characters (characters from the given string).

Syntax

The syntax of the str_shuffle() function:

str_shuffle(string);

Parameters

The parameters of the str_shuffle() function:

  • string: An input string.

Return Value

The return value of this method is string, it returns shuffled string.

Sample Input/Output

Input: 
$str = "This is IncludeHelp"

Output:
str_shuffle($str) = "enHeIdcupll"
str_shuffle($str) = "IcduneeplHl"

Example of PHP str_shuffle() Function

<?php
$str = "IncludeHelp";	
//printing shuffled string some of the times
echo (str_shuffle($str) . "\n");
echo (str_shuffle($str) . "\n");
echo (str_shuffle($str) . "\n");
echo (str_shuffle($str) . "\n");
echo (str_shuffle($str) . "\n");

$str = "ABC 123 @!#~&*()";	
//printing shuffled string some of the times
echo (str_shuffle($str) . "\n");
echo (str_shuffle($str) . "\n");
echo (str_shuffle($str) . "\n");
echo (str_shuffle($str) . "\n");
echo (str_shuffle($str) . "\n");	
?>

Output

The output of the above example is:

enHeIdcupll
IcduneeplHl
eenulHpcIld
enucIHepdll
ldIlceeupHn
2~B*()@# !3A1& C
 @C&(#)12*~!3BA 
@* &2CA( !~31#)B
 B!2C1~(3@ A*&#)
&A ~3 (CB2!)@1*#

To understand the above example, you should have the basic knowledge of the following PHP topics:

All PHP Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.