Advertisement
finderabc

php-zada4i

May 2nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.35 KB | None | 0 0
  1. <?php
  2. //Write a program that receives some info from the console about people and their phone numbers. Each entry
  3. //should have just one name and one number (both of them strings).
  4. //On each line, you will receive some of the following commands:
  5. // A {name} {phone} – adds entry to the phonebook. In case of trying to add a name that is already in the
  6. //phonebook you should change the existing phone number with the new one provided.
  7. // S {name} – searches for a contact by given name and prints it in format &quot;{name} -&gt; {number}&quot;. In case
  8. //the contact isn&#39;t found, print &quot;Contact {name} does not exist.&quot;.
  9. // END – stop receiving more commands.
  10. $phonebook = array();
  11. while(($entry = readline())!="END")
  12. {
  13.  
  14.     $contacts = explode(" ", $entry);
  15.     if ($contacts[0] == "A") {
  16.         // case "A" :
  17.         //echo "adding";
  18.         $name = $contacts[1];
  19.         $number = $contacts[2];
  20.         $phonebook[$name] = $number;
  21.        
  22.     } elseif ($contacts[0] == "S") {
  23.         //echo "search";
  24.         $name = $contacts[1];
  25.         //echo $name."$^
  26.         if (array_key_exists($name, $phonebook)) {
  27.             //echo "/test1";
  28.             echo $name . " -> " . $phonebook[$name]."\n";
  29.         } else {
  30.             //var_dump($phonebook);
  31.             echo "Contact $name does not exist.\n";
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement