Write a PHP program to create a new string taking the first 3 characters of a given string and return the string with the 3 characters added at both the front and back. If the given string length is less than 3, use whatever characters are there

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

Write a PHP program to create a new string taking the first 3 characters of a given string and return the string with the 3 characters added at both the front and back. If the given string length is less than 3, use whatever characters are there.

الأجوبة

<?php
function test($str) 
{
   if (strlen($str) < 3)
            {
                return $str.$str.$str;
            }
            else
            {
                $front = substr($str, 0, 3);
                return $front.$str.$front;
            }
        }
echo test("Python")."\n";
echo test("JS")."\n";
echo test("Code")."\n";

Sample Output:

PytPythonPyt
JSJSJS
CodCodeCod
هل كان المحتوى مفيد؟

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

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