How to return a specific variable from within a function?

3

In this code I gave return $array and I'm using the data inside foreach , so far so good.

I'd also like to use the $url_controle variable inside the file that I called the function.

Question: How can I print this variable in the other file? I gave return $url_controle but it did not seem to work.

function list_all_pages() {

    $url_controle = $_SERVER['REQUEST_URI'];
    $url_controle = explode('/', $url_controle);
    $url_controle = $url_controle[2];

    global $pdo;

    $sql    = "SELECT * FROM tb_paginas WHERE tipo = '$url_controle' ORDER BY ID DESC";
    $exc    = $pdo->query($sql);
    $cnt    = $exc->rowCount();
    $array  = $exc->fetchAll(PDO::FETCH_ASSOC);

    return $array;

}

################
EM OUTRO ARQUIVO
################

$pages = list_all_pages();

foreach ($pages as $valores) {
    echo $valores['ID'];
    echo $valores['titulo'];
}
    
asked by anonymous 21.11.2015 / 12:34

2 answers

1

You can create a return array with the information you want, so your code looks like this:

    function list_all_pages() {

        $url_controle = $_SERVER['REQUEST_URI'];
        $url_controle = explode('/', $url_controle);
        $url_controle = $url_controle[2];

        global $pdo;

        $sql    = "SELECT * FROM tb_paginas WHERE tipo = '$url_controle' ORDER BY ID DESC";
        $exc    = $pdo->query($sql);
        $cnt    = $exc->rowCount();
        $array  = $exc->fetchAll(PDO::FETCH_ASSOC);

        $retorno = array("url_controle" => $url_controle, "pages" => $array);
        return $retorno;

}

IN ANOTHER FILE

$pages = list_all_pages();
echo $pages['url_controle']; //valor que vc deseja obter

foreach ($pages['pages'] as $valores) {
    echo $valores['ID'];
    echo $valores['titulo'];
}
    
04.05.2017 / 21:56
-1

The whole secret of the Workaround for this Question is in return which should send array reporting all variables that can be used.

In the example below, you will notice that I passed an associative array that is retrieved in the file where function is called.

function list_all_pages() {

    global $pdo;

    $url_controle = $_SERVER['REQUEST_URI'];
    $url_controle = explode('/', $url_controle);
    $url_controle = $url_controle[2];

    $sql    = "SELECT * FROM tb_paginas WHERE tipo = '$url_controle' ORDER BY ID DESC";
    $exc    = $pdo->query($sql);
    $cnt    = $exc->rowCount();
    $array  = $exc->fetchAll(PDO::FETCH_ASSOC);

    // Aqui eu monto um array associativo que envia várias informações
    // Neste caso estou enviando $array e $url_controle

    return array('array' => $array, 'url_controle' => $url_controle);

}

################
USO DA FUNCTION
################

// Aqui eu armazeno todo o conteúdo da function dentro da variável $conteudo
$conteudo     = list_all_pages();

// Aqui eu armazeno o $array (SQL) dentro da variável $dados
$dados        = $conteudo['array'];

// Aqui eu armazena uma variável de $controle que vem da function
$controle     = $conteudo['url_controle'];

Now that you have the information stored in variables, you just use them wherever you like depending on the type of each.

For example, $url_controle is a string while $array is an array of data to use within foreach for example.

    
21.11.2015 / 13:31