Title: What is the mechanism of "using $_GET instead of a new file"?
Tags: php;methods;get
Question: I saw some websites that (for example):
if you want to view your message box, then it is: example.com/file.php?action=pm and also if you want to send a message again the address and the file is the same, but the $_GET is different : example.com/file.php?action=sendpm
How does it work?
Is it just:
if ($_GET['action'] == "pm")
//lots of html code(divs, forms, etc) paste here for action=pm.
//lots of html code paste here for action=send
instead of having files : pm.php, send.php ... ?Or the mechanism is different?
I saw that SMF use this mechanism and also I saw something like this for facebook.com/?ref=something.
How does it work?

Here is the accepted answer: The easiest way is this:
$action = isset($_GET['action']) ? $_GET['action'] : 'default';
if(!in_array($action, array('list', 'of', 'allowed', 'pages')))
$action = 'default';
include($action . '.php');
Note that the validation step is incredibly important. Not properly validating the value leads to nasty security holes from users being able to read any files your script can access to users being able to execute arbitrary code.
Putting all code in a single file like you suggested in your question is a bad idea since it results in unreadable code. Especially when mixing PHP and HTML in the same file like your question suggested (tip: use a template engine!).
Comment for this answer: Is "include()" function like in C++, and will paste the code of action.php in this page, then will it be something like my suggestion, but too cleaner?Is this the main idea?
Comment for this answer: It is similar to `#include` but it is not executed by a preprocessor. See php.net/include

Here is another answer: This is called routing and allows a single entry point to many functions within an single application.
Requests are taken, processed and routed to the correction function. This routing pattern fits well into the Model View Controller (MVC) pattern for organizing code.
Take a look at how the Zend Framework describes MVC and Routing:
http://framework.zend.com/manual/en/learning.quickstart.intro.html#learning.quickstart.intro.mvc

Here is another answer: You can have a switch statement. For example
example.com/file.php?action=pm or example.com/file.php?action=sendpm
if (isset($_REQUEST['action'])) {
$param = $_REQUEST['action'];
switch($param) {
case "pm":
// Code goes here
break;
case "sendpm":
// Do something here
break;
default:
// handle the default case
break;
} else {
// Handle it here
Well there are even other ways to do it !!
Switch is one of the easiest way !!

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:06/14/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><methods><get>', 'Title': 'What is the mechanism of "using $_GET instead of a new file"?', 'ViewCount': 90, 'id': 11031095, 'user_AboutMe': '<p>Hi!</p>\n', 'user_AccountId': 1141019.0, 'user_DisplayName': 'Milad R', 'user_Reputation': 1814.0, 'user_Views': 158.0, 'user_id': 1124914.0}}