4

So i am having trouble getting an array from one PHP file to another.

In my first file (file1.php) i got following code:

print_r($order->delivery);

It will get visible when using echo of course, and it outputs the right things. It gives me an array with order information. Now i got another PHP file I need to use this information in.

What i tried so far is including file1.php to file2.php and then echo the array... But the result is empty.

require_once('path/file1.php');
echo print_r($order->delivery);

And i tried echo my array directly in file1.php adding a div like this

echo "<div id='test'>" . print_r($order->delivery, true) . "</div>";

And then getting the inner HTMl of the div with DOM

    $dom = new DOMDocument();

    $dom->loadHTML("mypageurl");

    $xpath = new DOMXPath($dom);
    $divContent = $xpath->query('//div[id="test"]');

    if($divContent->length > 0) {
        $node = $divContent->item(0);
        echo "{$node->nodeName} - {$node->nodeValue}";
    }
    else {
        // empty result set
    }

Well... none of it works. Any suggestions?

4
  • Are you sure the file actually gets included? Do you have displaying errors enabled? Try using require instead of include just to make sure the file is included (or get an error if it's not). Check out your log files as well. Commented Nov 24, 2015 at 15:41
  • I also used require_once because i got an error when using include, i am sure it gets included, no errors. Will update my post. Commented Nov 24, 2015 at 15:43
  • You're not assigning anything in file1.php. A print_r doesn't change any variables (it just has the side-effect of printing). You need to include wherever the variable $order is actually defined. Commented Nov 24, 2015 at 15:49
  • So can you paste a little bit more of file1.php or may be the whole if it's not too long? Specifically the code surrounding the initialization of $order Commented Nov 24, 2015 at 15:52

4 Answers 4

2

You have to set a variable or something, not echoing it.

file1.php:

$delivery = $order->delivery;

file2.php

include('path/file1.php');
echo "<div id='test'>" . (isset($delivery['firstname']) ? $delivery['firstname'] : '') . "</div>";

Or you use the $object directly if it is set in file1.php

file2.php

include('path/file1.php');
echo "<div id='test'>" . (isset($order->delivery['firstname']) ? $order->delivery['firstname'] : '') . "</div>";
Sign up to request clarification or add additional context in comments.

3 Comments

Well, sadly this is not working. Still i get an empty result. It's like that: file1.php and file2.php get also included into another page which puts everything out. Is it possible that this will not work when file.2php gets included earlier than file1.php?
file2.php needs to be second.
The important part of this answer is that file1.php is different. It is now an assignment: $delivery = $order->delivery;. Now after it is include/require'd from another file, $delivery is defined.
1

You can do this by using $_SESSION or $_COOKIE, See here for more detail; PHP Pass variable to next page

Comments

1

Be careful at the variable scope. See this link: http://php.net/manual/en/language.variables.scope.php

And try this code please:

require_once('path/file1.php');
global $order;
print_r($order->delivery);

Defining $order as global should fix your issue.

Comments

1

You could return an array in a file and use it in another like this:

<?php
/* File1.php */
return array(
    0,1,2,3
);

-

<?php
/* File2.php */
var_dump(require_once('File1.php'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.