How to use a variable contained in another PHP page?

Asked

Viewed 19,595 times

11

Hey, guys, good afternoon. How do I use a variable from an X.php file on the Y.php page ? I want to do the following for example:

<?php
    $Frase = "minha frase";
?>

On my Y page I want to get the value of one of these variables, example:

<h3>
<?php $Frase; ?>

How could I receive the result of these variables from other pages? someone would have an example?

  • 1

    Yes it is possible with include/require or passing as parameter via get/post.

  • 1

    How you are getting the value of this variable?

  • include('includes/controllers.php');

  • 1

    It is better to add more details about your problem, how it works include, pq need to pass this value to another page.

2 answers

8

Knowing that the file "other_file.php" has:

<?php
    $frase = "abcdef";
?>

In another file (from the same directory) the variable can be included as follows:

<?php
    include('outro_ficheiro.php');
    echo $frase;
?>
  • You didn’t call me back, pal

  • 4

    @Júliorodrigues are both files in the same directory? The code is between the <?php tags and ?> ?

  • No. X file -> Assets(include) Y file is at the root

  • 2

    You have to set the correct directory of the file where the variable is in the incude.

  • 2

    After the <?php add this line: error_reporting(E_ALL);&#xA; ini_set("display_errors", 1); and see which error appears... probably you are going the wrong way, try so include "assets/outro_ficheiro.php;"...

  • Thanks, Gustavo! It worked! I did via include! the problem was the way I specified the way! Thanks!

  • 1

    Works perfect, thank you very much. My files were a hahaha mess

Show 2 more comments

2

By way of session.

Pagex.php

<?php
    session_start(); # Deve ser a primeira linha do arquivo

    $frase = "Minha Frase";

    $_SESSION['frase'] = $frase;
?>

Pagey.php

<?php
    session_start(); # Deve ser a primeira linha do arquivo

    echo $_SESSION['frase'];
?>
  • Only it returns me Warning: session_start(): Cannot send Session cookie - headers already sent by (output Started at /home/mktbt/mktbt.com/index.php:18) in /home/mktbt/mktbt.com/index.php on line 348 Warning: session_start(): Cannot send Session cache Limiter - headers already sent (output Started at /home/mktbt/mktbt.com/index.php:18) in /home/mktbt/mktbt.com/index.php on line 348

  • You are putting the command in the first line of your file ?

  • Yes, it is in the first line of my file. But now does not return the message

  • 3

    Using a session variable to capture variable values, above all if they are configuration values, is a bit exaggerated, and unsafe don’t you think ? A simple include would be enough. Or, you can explain in which situations the sessions are most beneficial.

  • He asked if there was an example. I gave an example of several, such as the use of include. Now, he didn’t specify anything in the question, so I talked about the session. He didn’t ask anything more than that. If you’re worried give a more complete answer.

  • Many of us have been affected in the past, and even today some are affected, due to transmitted bad practices, or unclear examples of how to correctly use a function or build decision trees, or where best fit each of them, and even today these gaps remain in many of us. That’s more or less what I meant. If you’re going to exemplify something, think 2x how it will be used in a while. It doesn’t have to be perfect, just minimally logical.

  • To me you just philosophized and said nothing.

Show 2 more comments

Browser other questions tagged

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