Get php variable from another file

2

I have 2 PHP files.

The first one is called data.php and it has

<?php

$get = $_GET;

if (isset($get['getBoxes']) && $get['getBoxes'] == 'what-we-do') {
    $boxes = array(
        array(
            'title' => 'e-commerce',
            'color' => '#ff910f',
            'content' => 'As your E-Commerce partner we will identify your specific needs, build solutions that will make your operations stronger, and provide a powerful web presence.',
        ),
        array(
            'title' => 'mobile & social',
            'color' => '#ffd11a',
            'content' => 'Be it iOS, Android or Windows Phone, we can accommodate your specific needs for your ideal mobile application.',
        ),
        array(
            'title' => 'open source cms',
            'color' => '#39a91f',
            'content' => 'Every good open-source CMS system allows for great flexibility to be customized and extended. Let us help you build yours.',
        ),
        array(
            'title' => 'software development',
            'color' => '#17b5ff',
            'content' => 'Our development services help you address evolving business and technology challenges by building applications tailored to meet your business requirements.',
        ),
        array(
            'title' => 'interactive design and usability',
            'color' => '#6b6bff',
            'content' => 'We are a full-service user interface design firm offering information architecture, interaction design, and full visual design.',
        ),
    );

    echo json_encode($boxes);
    exit;
}

if (strpos($_SERVER['REQUEST_URI'], basename(__FILE__)) !== false) {
    header("HTTP/1.1 404 File Not Found", 404);
    exit;
}

And from a second PHP file called html.php I want to use the array $ boxes but I do not know how to access it since it is inside $ get.

    
asked by Juan Pablo B 07.03.2018 в 15:03
source

2 answers

4

As I said, this type of operations are usually done through Ajax.

Now, you can cheat , literally saying, to the file data.php in the following way:

1. In html.php

You build a pseudo_get and then do the include, as follows.

$get=array('getBoxes'=>'what-we-do');
include('data.php');

2. In data.php

You should make a slight modification. It is imperative that you comment or delete this line:

//$get = $_GET;

Since the value of GET would actually be sent from html.php .

You could also build your own variable $_GET in html.php as follows:

$_GET['getBoxes'] = 'what-we-do';

Only in that case, you would have to modify this line of data.php :

if (isset($_GET['getBoxes']) && $_GET['getBoxes'] == 'what-we-do') {

    //resto del código ...
  

Try it and you'll see that it works. Anyway, I repeat, this is not the   normal procedure.

What this situation shows is that always , in this type of cases, data.php will need values from where it is invoked, in order to work properly.

    
answered by 07.03.2018 / 16:03
source
1

You already tried with require_once

require_once('data.php');

According to the php manual

  

When a file is included, the code it contains inherits the scope   of the variables of the line in which the inclusion occurs. Any   variable available in that line of the file that makes the call,   will be available in the file called, from that point on.   However, all functions and classes defined in the file   included have the global scope. Manual include PHP

And as I understand your code $ boxes will be defined depending on

if (isset($get['getBoxes']) && $get['getBoxes'] == 'what-we-do')  

so if the condition is not met then $ boxes will not be defined and be careful with exit because that will end with the execution of the script

    
answered by 07.03.2018 в 15:22