Reverse String – Qawall

Write a program to reverse string in php

We can reverse string in php using the php string function strrev() and without using the string function. Let’s check one by one.

Example : string = HELLO WORLD , reverse = DLROW OLLEH

Program 1 : using strrev()

<?php  
     $string = "HELLO WORLD";  
     echo strrev($string);  
?>  

Click to know more about strrev()

Output :

Program 2 : Without using strrev()

<?php  
   $string = "HELLO WORLD!";  
   $length = strlen($string);  
   for ($i=($length-1) ; $i >= 0 ; $i--)   
   {  
       echo $string[$i];  
   }  
?>    

Logic :

  1. Store the string to a variable
  2. Find the length of the string, this length is used to loop through the string
  3. Start a for loop from $length-1 , the -1 is because php indexing/position is starts from 0 also the loop will be decreased until 0
  4. Print each alphabet according to the position

Click to know more about strlen()

Output :

Click here If you are looking for PHP Number Reverse

Related Posts

forest fire

Forest Fire – Techgig, All testcases passed. Challenge #1

Forest FireĀ (100 Marks) Challenge 1: Forest fire You are camping in a forest area at night. You are living with the forest officers to experience their challenges…

the magic wand

The Magic Wand – TechGig PHP answer, all test cases passed. Challenge #2

The Magic Wand – TechGig PHP answer – all testcases passed The Magic Wand (100 Marks) You are a wizard who possesses the magic wand that can be…

PHP interview Questions and Answers

What is PHP? Answer: PHP stands for Hypertext Preprocessor. It is a widely used server-side scripting language primarily designed for web development. PHP code is embedded within…

install php

A Step-by-Step Guide on How to Install PHP

Install PHP : A Step-by-Step Guide on How to Install PHP Introduction: PHP is a widely used server-side scripting language that powers countless websites and web applications….

what is php

What is PHP? Understanding PHP: The Powerhouse of Web Development

Introduction: What is PHP? Understanding PHP: The Powerhouse of Web Development: In the world of web development, PHP stands tall as one of the most widely-used and…

php form

Complete Php form example

Here we are going to check how to create a php form and insert its values to database table. In this example we will be submitting the…

Leave a Reply

Your email address will not be published. Required fields are marked *