Get input from CLI and create Array in PHP. Is this correct?

My objective is to make a simple app that will allow me to quickly input data from the CLI and create an array, to then pass that as an entry on MariaDB on my localhost (right now I'm just dumping to check it's actually working). This was my solution for today, although it works, I feel it's a bit patchy.
Can I have your experienced take on this? I'm planning to add a check at the end to review the entries before committing the array to the database.
function createNewEntry()
{
echo "Enter Practitioner Name: ";
$getname = fgets(STDIN, 128);
echo "Enter Practitioner email: ";
$getemail = fgets(STDIN, 128);
echo "Enter Practitioner Country: ";
$getcountry = fgets(STDIN, 128);
$thearray = array('Name' => $getname, 'email' => $getemail, 'Country' => $getcountry);
return ($thearray);
}
$newprac = createNewEntry();
var_dump($newprac);
Answer
Solution:
Thereadline
function simplifies the task of prompting the user for info from the command line. The PHP:readline Manual page is fairly straightforward, as well, since there is only one argument for the function - the prompt string.
Usingreadline
would allow you to tighten things up a bit, reading values directly in the array constructor.
<?php
function createNewEntry()
{
$thearray = array(
'Name' => readline( "Enter Practitioner Name: "),
'email' => readline( "Enter Practitioner email: "),
'Country' => readline( "Enter Practitioner Country: ")
);
return ($thearray);
}
$newprac = createNewEntry();
var_dump($newprac);
the output will look the same:
Enter Practitioner Name: Doc Watson
Enter Practitioner email: [email protected]
Enter Practitioner Country: Bluegrass
array(3) {
["Name"]=>
string(10) "Doc Watson"
["email"]=>
string(14) "[email protected]"
["Country"]=>
string(9) "Bluegrass"
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to a member function store() on null
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.