Wednesday, September 22, 2021
How can we help?
< All Topics
Print

How To Reverse String in PHP

Let’s understand a one more program to create your logic stronger in php with a string reverse program for example INFOJACKERS reverse string will be “SREKCAJOFNI”.

In Php you can perform this task in two way with using in-built function and without in-built function. we will see here both one by one.

1.REVERSE STRING WITH IN-BUILT FUNCTION IN PHP


 <?php

     var $str = "INFOJACKERS";

     echo strrev($str); //Output - "SREKCAJOFNI"

 ?>

2. REVERSE STRING WITHOUT IN-BUILT FUNCTION IN PHP

Here will use some logic to and understand how it’s works.

first let’s see program below

 <?php
     var $str = "INFOJACKERS";

     var $len = strlen($str); // to get length of string

     for($i= $len -1; $i>=0;$i--){

       echo $str[$i]; // print one by one string from last "SREKCAJOFNI"

     }

 ?>

In the above program we take string and every string start from index 0. with the help of array string we can print the values from last till the index 0 of string. that how we can get reverse string in php without in-built function. I hope you guys will understand it and please give your feedback to our blogs and post for improvement.

Previous How to make first program in php- As a beginner
Next Simple Star Pattern in PHP
Table of Contents