Sort Words in Alphabetical Order in PHP

sort() function in PHP is used to sort the array elements in an Ascending Order.

Source code :


<?php
$string = "code with codecry"; //any string

echo "Original String :\n".$string."\n";

$split=explode(" ", $string); // breaks sentence in to elements

 // $split[0] will be code

sort($split); // sorts the elements
echo "After sort:\n";
echo implode(" ", $split); //combine and print the elements 

Output :

Original String :
code with codecry

After sort:
code codecry with

Notes :

explode("seperator",string,limit) breaks the string in to elements like array. limit parameter is to define how to split and it is optional.

implode("seperator",string) joins the array elements back to the set of strings.


Output :


                

Comments :