Friday, January 8, 2016

Limbajul PHP. The proper way to adress someone ... in English :-)


Conform multiplelor surse, e o mica bataie de cap sa te adresezi "cu eticheta" unei persoane in limba engleza, presupunand ca nu-i cunosti dinainte sexul, varsta si starea civila.

Teoria spune ca daca e vorba de femei majore, nemaritate, folosesti formula ambigua Ms. in fata numelul de familie (last name) sau chiar in fata numelui intreg. Daca femeia-i maritata, folosesti fara teama Mrs. in fata numelui (intreg sau de familie). Ambele formule isi au originea in "Mistress".

Enuntul urmator de pe programmr.com introduce in plus urmatoarea regula fara discriminare de sex: daca varsta este sub cea a majoratului, persoana este chemata pe numele complet. Daca e vorba de un barbat major, se foloseste Mr. inaintea numelui de familie.

"Write a program which displays an appropriate name for a person, using a combination of nested if's and compound conditions. Ask the user for a gender, first name, last name and age.
If the person is female and 20 or over, ask if she is married. If so, display "Mrs." in front of her name. If not, display "Ms." in front of her name. If the female is under 20, display her first and last name.
If the person is male and 20 or over, display "Mr." in front of his name. Otherwise, display his first and last name.
Note that asking a person if they are married should only be done if they are female and 20 or older."

Rezulta ca pot aparea cinci situatii distincte, motiv pentru care problema e tratata usor cu clauze if/else.


Codul si printscreen-ul.

<?php
   $finalName;
   
   echo "What is your Gender (M or F):";
   
   $gender = trim(fgets(STDIN));
   
   echo "First Name:";
   
   $firstName = trim(fgets(STDIN));
   
   echo "Last Name:";
   
   $lastName = trim(fgets(STDIN));
   
   echo "Age:";
   
  $age = trim(fgets(STDIN));
  
   if ($age >= 20)
   {
       if($gender === 'F')
       {
           echo "Are you married?\n";
           $mstatus = trim(fgets(STDIN));
           
           if($mstatus === 'Y') $finalName = "Mrs. $lastName";
           elseif($mstatus === 'N') $finalName = "Ms. $lastName";
       }
       else if($gender === 'M') $finalName = "Mr. $firstName $lastName";
   }
   else $finalName = $firstName." ".$lastName;
  
   echo "Then I will call you ".$finalName;
   exit;
?>


No comments:

Post a Comment