How to replace two words in a string by index with PHP

1 Answer

0 votes
$s = "php java c# c++ python c#";

echo $s . "<br>";

$arr = explode(' ', $s);

$index1 = 0;
$index2 = 4;


$tmp = $arr[$index2];
$arr[$index2] = $arr[$index1];
$arr[$index1] = $tmp;

$s =  implode(" ", $arr);

echo $s;



/*
run

php java c# c++ python c#
python java c# c++ php c#

*/

 



answered Dec 1, 2020 by avibootz
...