Write a PHP program to create a new string of length 2, using first two characters of a given string. If the given string length is less than 2 use '#' as missing characters

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

Write a PHP program to create a new string of length 2, using first two characters of a given string. If the given string length is less than 2 use '#' as missing characters.

الأجوبة

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

echo test("Hello")."\n";
echo test("Python")."\n";
echo test("a")."\n";
echo test(" ")."\n";

Sample Output:

He
Py
a#
 #
هل كان المحتوى مفيد؟

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

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