w3resource

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


59. String Pattern: s1s2s2s1

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.

For more Practice: Solve these Related Problems:

  • Write a PHP script to concatenate two strings into a mirrored pattern, forming the sequence s1s2s2s1.
  • Write a PHP function to merge two strings by sandwiching one between two copies of the other in a symmetric order.
  • Write a PHP program to output a composite string pattern that mirrors two given inputs.
  • Write a PHP script to generate a new string by concatenating two input strings in a fixed symmetrical format.

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.



Follow us on Facebook and Twitter for latest update.