Jump to content

Recommended Posts

I want to use a switch.....case like for example:

<a href="?link=1">Page 1</a>
<a href="?link=2">Page 2</a>
<a href="?link=3">Page 3</a>

<?php
$link = $_GET['link'];

switch($link){
    case 1: $page = "page1.php";
    break;
    
    case 2: $page = "page2.php";
    break;
    
    case 3: $page = "page3/page3.php";
    break;
}
    
@include "$page";
  ?>

will it be hard to process about 800 pages with switch...case?

 

thank you in advance;)

Link to comment
https://forums.phpfreaks.com/topic/297827-switchcase-for-about-800-pages/
Share on other sites

you would not use a switch/case statement to do this. switch/case statements are used when you have to select between different processing logic in each case.
 
if all you are doing is checking if a value is one out of a permissible set, you would define the set of permissible values in a data structure somewhere (database table, array), test the input value against that data structure (db query, in_array()), then produce the output value based on the input value and use it.
 
if you have more than a few pages on a web site you should also be dynamically serving those pages using a content management system, where the content that's different between the pages is stored in a database, and the navigation menus and the logical pages are dynamically produced by simple php code on one physical page that uses the information stored in the database.
 
the PHPFreaks.com Questions, Comments, & Suggestions forum section where you posted this is not for asking programming questions, it's for asking questions or making comments/suggestions about this site.

Edited by mac_gyver

"will it be hard to process"

 

Technically, no. It will be onerous to setup and probably tedious to maintain.

 

But well within the computational power (memory and execution time limits) of PHP.

If you don't mind PHP evaluating up to 800 conditions to get the value of a single variable, sure.

PHP arrays are most undoubtably a better answer:

 

$page = array();
$pages[1] = 'page1.php';
$pages[2] = 'page2.php';
$pages[3] = 'page3/page3.php';

$link = (int)$_GET['link'];

if (isset($pages[$link])) {
    @require_once(realpath($pages[$link]));

} else {
    // hacking url param or page not found
}

i think this is more of a theoretical problem, rather than a real situation, given that there's another current similar thread asking about two different switch/case statements, one for a book selection and a second one about chapters for each book.

the whole point of having a server-side scripting language is so that you don't have to write out and maintain 100's or even 10's of pages for a web site, like you would have had to do 20 years ago with static html pages. instead, you store just the information that's different (category, title, content, ...) between the pages in a database or an array (if you don't have that much different information), then you use a simple server-side program in one physical file to dynamically produce the navigation menu and dynamically produce each logical page from that stored information.

 

if you or someone you know produced 10's or 100's of actual .php files that contain whole html documents, with navigation menus, and different content, you are 20 years out of date. web sites are not made this way in the year 2015.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.