PHP - include a php file and also send query parameters

后端 未结 13 1138
长发绾君心
长发绾君心 2020-11-27 03:46

I have to show a page from my php script based on certain conditions. I have an if condition and am doing an \"include\" if the condition is satisfied.

if(co         


        
相关标签:
13条回答
  • 2020-11-27 04:37

    Your question is not very clear, but if you want to include the php file (add the source of that page to yours), you just have to do following :

    if(condition){
        $someVar=someValue;
        include "myFile.php";
    }
    

    As long as the variable is named $someVar in the myFile.php

    0 讨论(0)
  • 2020-11-27 04:37

    You can use $GLOBALS to solve this issue as well.

    $myvar = "Hey";
    
    include ("test.php");
    
    
    echo $GLOBALS["myvar"];
    
    0 讨论(0)
  • 2020-11-27 04:40

    You could do something like this to achieve the effect you are after:

    $_GET['id']=$somevar;
    include('myFile.php');
    

    However, it sounds like you are using this include like some kind of function call (you mention calling it repeatedly with different arguments).

    In this case, why not turn it into a regular function, included once and called multiple times?

    0 讨论(0)
  • 2020-11-27 04:44

    Imagine the include as what it is: A copy & paste of the contents of the included PHP file which will then be interpreted. There is no scope change at all, so you can still access $someVar in the included file directly (even though you might consider a class based structure where you pass $someVar as a parameter or refer to a few global variables).

    0 讨论(0)
  • 2020-11-27 04:44

    I was in the same situation and I needed to include a page by sending some parameters... But in reality what I wanted to do is to redirect the page... if is the case for you, the code is:

    <?php
       header("Location: http://localhost/planner/layout.php?page=dashboard"); 
       exit();
    ?>
    
    0 讨论(0)
  • 2020-11-27 04:50

    The simplest way to do this is like this

    index.php

    <?php $active = 'home'; include 'second.php'; ?>
    

    second.php

    <?php echo $active; ?>
    

    You can share variables since you are including 2 files by using "include"

    0 讨论(0)
提交回复
热议问题