PHP Exercises: Create a new string taking the first character from a given string and the last character from another given string
76. Concat First from One and Last from Another
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.
Sample Solution:
PHP Code :
<?php
// Define a function named 'test' that extracts the first and last characters from two input strings
function test($s1, $s2)
{
// Initialize an empty string to store the last characters
$lastChars = "";
// Check if the first string has a length greater than 0
if (strlen($s1) > 0)
{
// If true, append the first character of $s1 to $lastChars
$lastChars = $lastChars.substr($s1, 0, 1);
}
else
{
// If false, append "#" to $lastChars
$lastChars = $lastChars."#";
}
// Check if the second string has a length greater than 0
if (strlen($s2) > 0)
{
// If true, append the last character of $s2 to $lastChars
$lastChars = $lastChars.substr($s2, strlen($s2) - 1);
}
else
{
// If false, append "#" to $lastChars
$lastChars = $lastChars."#";
}
// Return the concatenated string containing the last characters
return $lastChars;
}
// Test the 'test' function with different strings, then display the results using echo
echo test("Hello", "Hi")."\n";
echo test("Python", "PHP")."\n";
echo test("JS", "JS")."\n";
echo test("Csharp", "")."\n";
?>
Explanation:
- Function Definition:
- The test function is defined with two parameters:
- $s1: the first input string.
- $s2: the second input string.
- Initialize $lastChars:
- $lastChars is initialized as an empty string to store the resulting characters.
- Extract and Append Characters from $s1:
- If $s1 has a length greater than 0:
- The first character of $s1 is appended to $lastChars using substr($s1, 0, 1).
- If $s1 is empty:
- A "#" symbol is appended to $lastChars.
- Extract and Append Characters from $s2:
- If $s2 has a length greater than 0:
- The last character of $s2 is appended to $lastChars using substr($s2, strlen($s2) - 1).
- If $s2 is empty:
- A "#" symbol is appended to $lastChars.
- Return Value:
- Returns the $lastChars string, which contains the selected characters from $s1 and $s2.
Output:
Hi PP JS C#
Flowchart:

For more Practice: Solve these Related Problems:
- Write a PHP script to form a new string by taking the first character from one string and the last character from another, substituting '#' for any missing character.
- Write a PHP function to validate two string inputs and extract their respective first and last characters, using a default value if empty.
- Write a PHP program to combine characters from two strings in a fixed order with fallback for zero-length inputs.
- Write a PHP script to use ternary operators to replace missing values with '#' and then concatenate the specified characters from both strings.
Go to:
PREV : New String of Length 2 with Padding.
NEXT : Concat Lowercase Strings and Omit Doubles.
PHP Code Editor:
Contribute your code and comments through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.