PHP Switch Functions.

Wizet

New Member
Messages
644
Reaction score
0
Points
0
Can anyone give me a snippet of a php code for switch functions. I want to make a url so that it appears like ?action=support or ?index=lalala or something like that. I don't want a template that says 0 equals 1 or 1 equals with something like that (I seriously forgot what that switch function template looked like.). If so please include the mention of php files in your code snippet. *Thanks* xD
 

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
You mean like this:
PHP:
switch($condition)
{
	case 'value':
		//case code
		break;
	
	default:
		//default code
		break;
}
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Note that switch is a statement, not a function. It's an important difference because, as special forms, control structures can do things that functions can't. As for examples, the manual page has plenty. If you ever have questions about any language, check the official documentation first. That's why it's there.
 

Wizet

New Member
Messages
644
Reaction score
0
Points
0
And what would I have to change for the php file to be recognized and what do i change to make it ?action=lala ?
 

brutetal

New Member
Messages
23
Reaction score
1
Points
0
PHP:
$action = $_GET['action']//Gets the url request
echo $ction; //Output value
example url: index.php?action=printme

This should help you.

To prevent sql injections:
PHP:
$action = $_GET['action']//Gets the url request
$strip_action = mysql_real_escape_string($action); //If your using it for querying into mysql db, its highly suggested you do this to prevent sql injections.
echo $strip_action; //Output value
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
The query string is best accessed via the _GET superglobal. Names and values from the query string become names and values in the array. Read the manual for more information.

Just make sure to sanitize the input to prevent an injection attack.
 

zegnhabi

Member
Messages
44
Reaction score
2
Points
8
A switch or an associative array is preferable to a sequence of ifs. It's more readable and potentially more performant. Numerous ifs is a minor smell:biggrin:
PHP:
$pages = array(
    'home' => array('title' => 'Home', 'file' => 'index.html'),
    'about' => array('title' => 'About Us'),
    'contact' => array('title' => 'Contact Us'),
   ...
    'invalid' => array('file' => '404.html')
);
function getFile($pages, $page) {
    if (isset($pages[$page])) {
        return $pages[$page];
    } else {
        return $page . '.html';
    }
}

// prevent injection attacks
if (isset($pages[ $_REQUEST['page'] ])) {
    $page = getFile($pages, $_REQUEST['page']);
    $title = $pages[$page]['title'];
     ...
} else {
    // invalid page requested
    header('HTTP/1.0 404 Not Found');
    $page = $pages['invalid']['file'];
}
include('header.php');
include($page);
include('footer.php');
 
Top