Write a PHP program to create a new string using 3 copies of the first 2 characters of a given string. If the length of the given string is less than 2 use the whole string

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

Write a PHP program to create a new string using 3 copies of the first 2 characters of a given string. If the length of the given string is less than 2 use the whole string.

الأجوبة

<?php
function test($s1)
 { 
         $extra_Front = "";
  
         if (strlen($s1) < 2)
            {
                return $s1 . $s1 . $s1;
            }
            else
            {
                $extra_Front = substr($s1, 0, 2);
                return $extra_Front . $extra_Front . $extra_Front;
            }
 }

echo test("abc") . "\n";
echo test("Python") . "\n";
echo test("J") . "\n";

Sample Output:

ababab
PyPyPy
JJJ
هل كان المحتوى مفيد؟

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

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