Dear All,
I have two .php page. In my main php I call the other via the require_once method. Then in the main .php I declare and assigne variable and I tried to read the value in the require_once php I am not able to see the value. How can I achieve this ? Thank you.

Recommended Answers

All 3 Replies

You should be able to use a variable from the main program unless you are using Functions and the variable was defined in / being used in a Function without being defined as Global. If that is not it, then you need to post some code.

Dear Chris,
Is there any difference between require and require_once?

Pasting from http://www.geekinterview.com/question_details/22645

"Difference between require() and require_once(): require() includes and evaluates a specific file, while require_once() does that only if it has not been included before (on the same page).
So, require_once() is recommended to use when you want to include a file where you have a lot of functions for example. This way you make sure you don't include the file more times and you will not get the "function re-declared" error."

What you said you have tried should work without a problem
main.php

<?php
$test = "Hello";
?>

test.php

<?php
require_once "main.php";
echo $test;
?>

As Chris said, pls paste some code that you have used in case you are still facing problems

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.