Home > Back-end >  I need to include file in array
I need to include file in array

Time:09-17

I tried to include the php file in array like that but it is not working username.php file is : "mohammed", "ahmed", "hafian", "gimmy", "osama" and the main file is : $usernames = array(include 'usernames.php');

CodePudding user response:

In usernames.php do:

<?php
return ["mohammed", "ahmed", "hafian", "gimmy", "osama"];

In foo.php do:

$usernames = include 'usernames.php';

RTM: https://www.php.net/manual/en/function.include.php#example-145

CodePudding user response:

You will need to open and read the file, you can't just include and expect it to parse the file and store into your array. Also, can you make usernames.php a text file and not PHP code? You don't want to be parsing PHP code. I think you'll need to approach it like this:

`<?php
  $fn = fopen("usernames.txt","r");

  while(! feof($fn))  {
     $result = fgets($fn);
     # parse line and append to array
  }

  fclose($fn);
?>`
  •  Tags:  
  • php
  • Related