If you mean that echo
needs parentheses as if it were a function: no, it does not need them because it is not a function, but a language construction (such as require
, include
, case
, etc):
<?php
/* Esto es innecesario */
echo (dirname(__FILE__));
The code you have used sends the route directly to the browser, does not store it for later use. If you want to store it for later use, what is missing is assigning it to a variable, and not putting parentheses or anything similar.
Doing what I show you next, you can open files using that base path stored in the variable:
<?php
/* Almacenamos la ruta del script en la variable $ruta */
$ruta = dirname(__FILE__);
/* Hacemos uso de la variable $ruta para acceder a un archivo que
hay en el mismo directorio que el script */
$datos = file_get_contents($ruta . '/archivo.txt');
NOTE: As of PHP 5.3 you have available the constant predefined __DIR__
. At the date of your question the oldest version with support was the PHP 5.5 version, so you should use that macro instead of dirname(__FILE__)
.
<?php
/* Ya no es necesario almacenar la ruta base en ninguna variable */
$datos = file_get_contents(__DIR__ . '/archivo.txt');