0

i'm trying to understand PHP nowadays and i'm stuck on printing out my app's name from a .env file by using "phpdotenv" directory from composer. i've created these files: Index.php, _env.php and .env i'm using xampp - apache and i've configured a localhost on my machine.

this is my code by far:

index.php:

<?php
require_once __DIR__.'\..\app\config\_env.php';
$app_name= getenv('APP_NAME');
$app_url=getenv('APP_URL');
echo $app_name;
echo $app_url;
?>

_env.php:

<?php
define('BASE_PATH',realpath(__DIR__.'\..\..'));
require_once __DIR__.'\..\..\vendor\autoload.php';
$dotEnv = \Dotenv\Dotenv::createImmutable(BASE_PATH);
$dotEnv->load(); 
?>

.env:

APP_URL=http://buzzstore.test
APP_ENV=local
APP_NAME="Buzz E-commerce store"

#database
DB_DRIVER='mysql'
DB_HOST=localhost
DB_NAME=store
DB_USERNAME=store
DB_PASSWORD=secret

the output should be "Buzz E-commerce store" and then the url http://buzzstore.test but i only get a blank page on chrome without anything (and no errors, after i fixed some of them). if there are some PHP experts here that can help me with this, i'll be thankful :)

Ian
  • 9
  • 2
  • You probably don't have any error reporting / logging enabled. Try adding `error_reporting(E_ALL);` and `ini_set('display_errors', 1);` on top of the script you're executing. – Jeto Jul 31 '20 at 20:06
  • pasted these lines on the top of my index.php file, but nothing happened - still a blank page. – Ian Jul 31 '20 at 20:25
  • Are you sure you're hitting the right index.php script? If you echo something, do you see it appear? – Jeto Jul 31 '20 at 20:31
  • yes, if i echo some string it appears without problems. – Ian Aug 01 '20 at 06:26
  • Well, debug it. Try to print the contents of the .env file for instance, with `echo file_get_contents(BASE_PATH . '/.env')` before initializing dotenv, etc. – Jeto Aug 01 '20 at 06:32
  • I've tested your scripts on my local machine (just changed the folder paths to have my .env in the same folder) and I'm getting the correct result, so you'll have to dig a bit. – Jeto Aug 01 '20 at 06:33
  • if the .env file is not in the root folder then it raises errors.. tried to move it to other directories, not works for me – Ian Aug 01 '20 at 07:00

0 Answers0