w3resource

PHP Exercises: Create a new string using two given strings s1, s2, the format of the new string will be s1s2s2s1

PHP Basic Algorithm: Exercise-59 with Solution

Write a PHP program to create a new string using two given strings s1, s2, the format of the new string will be s1s2s2s1.

Sample Solution:

PHP Code :

<?php
// Define a function named 'test' that concatenates two strings in a specific order
function test($s1, $s2)
{
    // Concatenate the strings in the order: s1 + s2 + s2 + s1
    return $s1 . $s2 . $s2 . $s1;
}

// Test the 'test' function with different input strings and display the results
echo test("Hi", "Hello")."\n";
echo test("whats", "app");
?>

Explanation:

  • Function Purpose:
    • The test function takes two string parameters, $s1 and $s2, and concatenates them in a specific order: $s1 . $s2 . $s2 . $s1.
  • Concatenation Logic:
    • The function returns a single string that combines the inputs in the order: s1 + s2 + s2 + s1.
    • This means $s1 is placed at the beginning and end, with $s2 repeated twice in the middle.

Output:

HiHelloHelloHi
whatsappappwhats

Flowchart:

Flowchart: Create a new string using two given strings s1, s2, the format of the new string will be s1s2s2s1.

PHP Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a PHP program to check three given integers (small, medium and large) and return true if the difference between small and medium and the difference between medium and large is same.
Next: Write a PHP program to insert a given string into middle of the another given string of length 4.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/php-exercises/basic-algorithm/php-basic-algorithm-exercise-59.php