1

I'm currently stuck on what I thought would be an easy solution... I'm working with PHPFileNavigator, and the only thing I'm stuck on is how I can echo the Title that is returned to an array on a separate file. Every time a file is created/edited for an uploaded file, it generates the following file below when a title is added to the file.

Update

Generally all I'm wanting to do is return the one Array value from my destination file which in this case would be from the 'titulo' key, and then print it back to my source file.

Destination File

 <?php
 defined('OK') or die();
 return array(
    'titulo' => 'Annual 2011 Report',
    'usuario' => 'admin'
 );
 ?>

Source File

<?php
  $filepath="where_my_destination_file_sits";
  define('OK', True); $c = include_once($filepath); print_r($c);
?>

Current Result

Array ( [titulo] => Annual 2011 Report [usuario] => admin )

Proposed Result

Annual 2011 Report

All I'm wanting to find out is how can I echo this array into a variable on another PHP page? Thanks in advance.

4
  • 3
    I'm a little confused; what exactly is it that you're trying to achieve here? There may be a better way of doing what you're trying to do. Commented Sep 4, 2012 at 5:36
  • php files are not supposed to work like 'modules' in node.js where you export variables and functions .. instead you should just define the array, include the file and dump the array.. Commented Sep 4, 2012 at 5:38
  • +1 to @Infiltrator: you are probably right. Commented Sep 4, 2012 at 5:39
  • please clarify your question with your environment/platform restrictions. This seems really easy task else Commented Sep 4, 2012 at 5:46

4 Answers 4

3

Assuming your file is saved at $filepath

<?php
define('OK', True);
$c = include_once($filepath);
print_r($c);
Sign up to request clarification or add additional context in comments.

4 Comments

This is what I was working looking for, and now I should be able to expand on this. Thanks for the help.
Hey sberry, not sure if this needs to go into another question, but I'm seeing how I can get this piece to work into a while loop? It appears there's a conflict when it has to re-use the defin('OK, True): where I'll come back with -> Notice: Constant OK already defined in {filepath}
Just define OK outside the loop (prior to the loop).
Works! Hey, thanks for all of your help on this. Greatly appreciated!
1

If you know the file name and file path, you can easily capture the returned construct of the php file, to a file.

Here is an example:

$filepath = 'path/to/phpfile.php';
$array = include($filepath); //This will capture the array
var_dump($array);

Another example of include and return working together: [Source: php.net]

return.php

<?php

$var = 'PHP';

return $var;

?>

noreturn.php

<?php

$var = 'PHP';

?>

testreturns.php

<?php

$foo = include 'return.php';

echo $foo; // prints 'PHP'

$bar = include 'noreturn.php';

echo $bar; // prints 1

?>

Update

To only print a item from the array, you can use the indices. In your case:

<?php

$filepath="where_my_destination_file_sits";

define('OK', True); $c = include_once($filepath); print_r($c);
echo $c['titulo']; // print only the title

?>

8 Comments

I am curious why session is not working here in this scenarios , forgive me if I am missing the context info :(
@sakhunzai, Are your session codes executing? If they are.. they will work.
This is pretty helpful as well. I probably should've worded my question a little better... Now that I'm able to return the fully array from the destination php file. I'm seeing how I can take the full array and only print out the array value? I tried a few ideas using implode, but they didn't seem to do the trick for me, or perhaps I was driving off of some really bad examples? #Currently Returning Array ( [titulo] => File Title Name [usuario] => admin ) #Proposing to Return File Title Name
@Curtis, This seems to be totally different topic. Please ask another question. I will be happy to help if i understand better.
Sure thing, I reworded my original statement at the top, but I can address this as a new question if needed.
|
0

First we have a file(you want initiate array in it ) first.php

(you can also act profissionaler and play with parameters,with any parameter function pass different type or different array)

 function first_passing() {
      $yourArray=array('everything you want it's be');
      return $yourArray;
     }

And in second.php

require 'yourpath/first.php' (or include or include_once or require_once )
//and here just call function
$myArray=first_passing();
//do anything want with $myArray

Comments

-1

To print/echo an array in PHP you have to use print_r($array_variable) and not echo $array

1 Comment

That's not what's being asked, though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.