Write a PHP program to create a new string using first two characters of a given string. If the string length is less than 2 then return the original string

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

Write a PHP program to create a new string using first two characters of a given string. If the string length is less than 2 then return the original string.

الأجوبة

<?php
function test($s1)
{
   if (strlen($s1) < 2)
   {
       return $s1;
   }
   else
   {
       return substr($s1, 0, 2);
   }
}
echo test("Hello")."\n";
echo test("Hi")."\n";
echo test("H")."\n";
echo test("  ")."\n";

Sample Output:

He
Hi
H
هل كان المحتوى مفيد؟

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

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