Using readline to search for a value in a array

hi guys,
its my second week learning php and i am very new to it.
wrote a code to see if i can retrieve a value in an array using readline(""), but the code wont work properly. i know there is something am doing wrong but i am just wondering if someone can put me on the right track. please see below code:

<?php
    $contact = array ("sam"=>"davies","james"=>"wade");
    foreach($contact as $contacts=>$surname)
    {
        while(true)
        {
           $first_name = readline("ENTER FIRST NAME:");
           if ($first_name == $contacts)
            {
                echo $contacts. " " .$surname."\n";
            continue;
      
            }
        }
    }    
?> 

will appreciate any feedback.

Merry Christmas (or Happy Holidays)

The PHP readline function to read console input and is something in my opinion shouldn’t be something a person new to PHP should learn.

Heck, I never heard of readline before I did some investigating myself and my IDE even suggested I add some components to composer.json file as it gave me some minor warnings for security reasons.

Here is what the readline function does:

<?php
//get 3 commands from user
for ($i=0; $i < 3; $i++) {
    $line = readline("Enter a Command: ");
    readline_add_history($line);
}

//dump history
echo "<pre>" . print_r(readline_list_history(), 1) . "</pre>";

//dump variables
echo "<pre>". print_r(readline_info(), 1) . "</pre>";

I would advise to find an easier PHP learning course.

Happy holidays,

Thank you for advise. it is much appreciated and i have learnt something from the code that will help me in my development.

Kind regards

Sponsor our Newsletter | Privacy Policy | Terms of Service