Hi all, I have taken from net MVC tutorial, but it is in German language. Can anyone explain it me why there were joined two superglobal? What is a purpose?
its below

<?php

// unsere Klassen einbinden
include('classes/controller.php');
include('classes/model.php');
include('classes/view.php');

// $_GET und $_POST zusammenfasen, $_COOKIE interessiert uns nicht.
$request = array_merge($_GET, $_POST);
// Controller erstellen
$controller = new Controller($request);
// Inhalt der Webanwendung ausgeben.
echo $controller->display();

?>

The all code i have attached

that could be old?

All i know is $_REQUEST is a standard array like $_GET or $_POST, it's in php.ini which overwrites the other

$_GET;//get1
$_GET;//empty
$_GET;//empty

$_POST;//post1
$_POST;//post2
$_POST;//empty

//depending on php.ini
$_REQUEST;//post1
$_REQUEST;//post2
$_REQUEST;//empty

$_REQUEST;//get1
$_REQUEST;//post2
$_REQUEST;//empty


The only reason i can think of to do that is if you could get data coming in in both arrays that could over-write each other like above and you want to keep it. But even so array_merge is pointless cause it will overwrite it just like the $_REQUEST global!

Can you me simple example for this (array_merge($_GET, $_POST)) if possible?

$array1 = array('apple'=>25,'orange'=>35);
$array2 = array('banana'=>40,'pineapple'=>80,'tomato'=>50,'orange'=>55);
$array3 = array_merge($array1,$array2);

$array4 = array(5,4,8,3,9);
$array5 = array(1,7,5,6,4);
$array6 = array_merge($array4,$array5);

echo count($array1).'<br/>';
var_dump($array1);
echo '<br/>';
echo count($array2).'<br/>';
var_dump($array2);
echo '<br/>';
echo count($array3).'<br/>';
var_dump($array3);
echo '<br/>';
echo 'orange overwritten';
echo '<br/>';
echo count($array4).'<br/>';
var_dump($array4);
echo '<br/>';
echo count($array5).'<br/>';
var_dump($array5);
echo '<br/>';
echo count($array6).'<br/>';
var_dump($array6);
echo '<br/>';
echo 'arrays with integar indexes added as new entry';

Biiim,
I would like know what principles serve array_merge($_GET, $_POST)
i would like to learn its working principle only joining with $_GET and $_POST methods

I'm not quite sure what you're asking, if you're asking for the philosophy of life for a php function and 2 global arrays im not sure there is one, array_merge joins 2 arrays together, the $_GET array takes variables from a GET request(variables in the url eg page.php?id=5&new=true), $_POST takes variables in from a POST request which is sent in the HTTP request headers (id=5&new=true).

Very similar things GET holds much less data and is visible in the address bar and history etc and POST can hold much more data (which can be set in the php.ini file its default is 1kb) and is much more hidden client side.

If you're asking for a real world example, i have never joined together the post and get arrays. I use POST for receiving user submitted data and GET is generally for viewing already existing data.
eg.
addproduct.php would use POST
viewproduct.php?id=5 would use GET

On a page where you could both view and submit data such as a forum/blog comment - the thread id in the url(GET) and your comment/post(POST) even then you know which is GET and which is POST so there is still no need to join them the id is usually passed into a hidden input field so even the thread id is POST too.

The only time i can imagine using a post & get merge is for ease of use such as a function/app written for other php developers where people can just throw their data at it however they like, post or get, and it pulls the data and runs, much like the files you first posted

array merges combines an array based on the keys. so if you have $_GET = 'foo' and $_POST = 'bar'... easier said in an example

$_GET['test1'] = 'foo';
$_POST['test1'] = 'bar';

$array = array_merge($_GET['test1'], $_POST['test1']);

//$array['test1'] becomes bar

//here's how I usually use array_merge

class foo{
    
    protected $_config = array();
    
    public function __construct ( array $array ) {
        $default = array(/*default options*/);
        $this->_config = array_merge($default, $array);
    }
}

kinda like setting options in a plugin and having defaults as a fallback

Member Avatar for diafol

You can pass data with get and post with a posted form:

<form action="page.php?id=12&colour=red" method="post">
<input name="year" type="text" /> <!-- e.g. 1978 -->
...
</form>

I tend to use one OR the other, but neither is deemed 'safe' - the validation and sanitization process for any data input is key.

Using $_REQUEST is bad. I can only imagine that the CMS tries to emulate a safer version of this superglobal, but without the cookie aspect.

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.