How to echo a variable of this function?

Asked

Viewed 318 times

1

Say I want to give echo in $path out of function. How can I do that? It seems simple but I don’t know!

function armazena_constantes (){

    $path               = '/home/axitech/www/dent';
    $path_admin         = '/home/axitech/www/dent/admin/';
    $base_url           = 'http://' . $_SERVER['HTTP_HOST'];
    $base_url_admin     = $base_url . '/admin/';

}

  • 1

    It seems that it is not clear to you the concept of function: they must do something, and generally return something. This function of yours doesn’t do either! The variables you create inside only exist inside yourself.

  • You need to edit the question again, now there is no variable called path got even weirder O.o

  • @bfavaretto has some function in the stack to return the question to initial form?

  • 2

    Click on the link "edited XX min ago" will open the editing history, go to the editing you want to go back and click on "revert".

2 answers

5

You can transform the way you store the variables.

function armazena_constantes (){
   $array = [];
   $array['path']               = '/home/axitech/www/dent';
   $array['path_admin']         = '/home/axitech/www/dent/admin/';
   $array['base_url']           = 'http://' . $_SERVER['HTTP_HOST'];
   $array['base_url_admin']     = $base_url . '/admin/';
   return $array;
}

To access just use:

$constantes = armazena_constantes();
echo $contantes['path_admin'];

Or you can use by class and set the values in object, giving getPathAdmin and etc to recover the values.

class constantes {
   private $path;
   //..

   public function __construct(){

         $this->path = '/home/axitech/www/dent';
        // ...

   }

   public function getPath(){

        return $this->path;

    }
}


$constantes = new Constantes();

echo $constantes->getPath();

https://ideone.com/Zf14bX

2


Return $path at the end of its function. Local variables (those declared within the function) are not accessible in other parts of the code.

You can define an associative array with the directories, and pay the desired part by informing an argument in the function

function base_url($selecionado){
    $dir = ['imagem' => '/imagem/', 'js' => '/js/', 'css' => '/css/'];
    return $dir[$selecionado];
}

//chamada:

echo base_url('css');

Another option is to create a file with constants of the most important directories and import it in the other files.

cofing.php

<?php
define('ROOT_DIR', dirname(__FILE__));
define('FUNCTIONS_DIR', ROOT_DIR .DIRECTORY_SEPARATOR. 'functions');
define('IMG_DIR', ROOT_DIR .DIRECTORY_SEPARATOR. 'www' .DIRECTORY_SEPARATOR. 'img');
define('CSS_DIR', ROOT_DIR .DIRECTORY_SEPARATOR. 'www' .DIRECTORY_SEPARATOR. 'css');

On the other pages, do a include/require for config.php, stay tuned to define the ROOT_DIR in this example I started from the point that it is at the root of the project.

index php.

<?php
include_once 'config.php';
echo 'Raiz do projeto: '. ROOT_DIR . '<br> imagens: '. IMG_DIR;
<img src="<?php echo IMG_DIR.'/logo.jpg'" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.