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

  • برمجة بي اتش بي

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#
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...