Mohammed Gameel
Mohammed Gameel

Reputation: 1

I need to include file in array

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');

Upvotes: 0

Views: 24

Answers (2)

user1336980
user1336980

Reputation:

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);
?>`

Upvotes: 0

Lawrence Cherone
Lawrence Cherone

Reputation: 46602

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

Upvotes: 1

Related Questions