Write a PHP program to create a new string taking the first character from a given string and the last character from another given string. If the length of any given string is 0, use '#' as its missing character
<?php function test($s1, $s2) { $lastChars = ""; if (strlen($s1) > 0) { $lastChars = $lastChars.substr($s1, 0, 1); } else { $lastChars = $lastChars."#"; } if (strlen($s2) > 0) { $lastChars = $lastChars.substr($s2, strlen($s2) - 1); } else { $lastChars = $lastChars."#"; } return $lastChars; } echo test("Hello", "Hi")."\n"; echo test("Python", "PHP")."\n"; echo test("JS", "JS")."\n"; echo test("Csharp", "")."\n";
Sample Output:
Hi PP JS C#
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer