How can I read php file's output into a variable?

16

I have a php file that looks like the following

<?php

return [
    'first' => 'some value',
    'second' => 'blah blah',
];

I want to set the php output's array as a variable. So my variable is the actual array. I tried to do the following

$config = file_get_contents(__DIR__ . '/config.php');

However, that return the file content as a string.

How can$config be the returned array from myconfig.php file?

670

Answer

Solution:

You have to change the file, removereturn and create assignment, like this:

$variable = [ ..... ]

Then if you need that array in different script, then use include or includerequire

PS: consider doing some basic tutorials on PHP. You are asking elementary question now and reinventing the wheel alone takes a lot of time.

People are also looking for solutions to the problem: regex - invalid regular expression in PHP

Source

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.

Ask a Question

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.

Similar questions

Find the answer in similar questions on our website.