-1

Simply put:

<? include("open.php"); ?>

VS

<? include("open.php?page=about"); ?>

VS

<? $x="varToPass"; include("open.php"); ?>

Why can this not be done, surely this should have been programmed into. And if not can I edit the raw/core PHP C files to achieve this.

Peon
  • 7,902
  • 7
  • 59
  • 100
TheBlackBenzKid
  • 26,324
  • 41
  • 139
  • 209
  • That's because `include` simply includes a file and not a URL. In other words, just echo's out its content – asprin Aug 01 '12 at 11:21
  • 4
    There is nothing wrong with this question. It may be based on a misapprehension, but since when has that made something a bad question? If the OP knew the truth of the matter, he wouldn't be here asking, would he. +1 – Mitya Aug 01 '12 at 11:23
  • Why do people always get this wrong, `include` (as well as `require`) is a **statement** not a **function**. So you should write it as `include 'file.php'`. See http://at.php.net/manual/en/function.include.php – fdomig Aug 01 '12 at 11:24
  • People do not 'always' get this wrong. Beginners may - it is an understandable mistake. You and I have made similar ones in our time, I'm sure. – Mitya Aug 01 '12 at 11:25
  • @Utkanos - I agree I would like a strong explanation. Also Repped +1 – AlphaApp Aug 01 '12 at 11:25
  • They get it wrong because 50% out there write it this way and the other that. Doesn't really hurt anyone, does it? – F.P Aug 01 '12 at 11:26
  • @Utkanos I agree that this is a valid question, but it is pretty heavy stuff of the OP to write "surely this should have been programmed into". If he is such a beginner to ask that question, then he should think about if he really is that much smarter than all those experienced guys who crafted php. So in a way it IS a mad question. – arkascha Aug 01 '12 at 11:28
  • Well, alright, yes, the assertion that this would have been 'programmed into' is perhaps a little bold. But again, if you don't know the difference between include/require vs. cURL, you could reasonable mistake the job of one for that of the other. – Mitya Aug 01 '12 at 11:29
  • @FlorianPeschka it actually **does** make a difference. See Example #4 and #5 in the docs: http://php.net/manual/en/function.include.php – fdomig Aug 01 '12 at 11:29

3 Answers3

2

The simple answer. There is no need to pass parameters like this.

include 'open.php?page=about&something=true&more=rubbish';

versus

$page = 'about';
$something = true;
$more = 'rubbish';

include 'open.php';

I think it's quite easy to see which is more readable, and they both achieve exactly what you are trying to do.

The fact is that the include statement literally includes the code from the indicated file at the point you reference it.

You can treat the file doing the including, as having the source of open.php at the exact place you wrote the include statement.

Edit:

To answer the second part of your question. Yes you could modify the source and achieve this, but it would be a largely pointless exercise.

Edit 2:

It has also occurred to me that perhaps you think you want to include files by URL.

For example include 'http://127.0.0.1/open.php?page=about'

In this case it is completely possible, as the http stream wrapper will be invoked, and variables will be passed to the file you requested.

This behaviour is DISABLED BY DEFAULT BECAUSE IT POSES A MASSIVE SECURITY RISK.

You can turn it on by editing the allow_url_include value in php.ini. But I suggest that you don't.

Leigh
  • 12,859
  • 3
  • 39
  • 60
1

When you include files they will be in the same scope, so you can re-use variables or retrieve parameters:

<?php
    // index file
    $x = $_GET["x"];
    include 'file.php';

And in file.php you could either do:

<?php
    //include file
    if ($x != '')

or:

<?php
    //include file
    $x = $_GET["x"];

So no need to pass things around-

insertusernamehere
  • 23,204
  • 9
  • 87
  • 126
-1

Including/requiring is for physical files, not dynamic representations of files based on incoming parameters.

For the latter, you will need to look into cURL.

Mitya
  • 33,629
  • 9
  • 60
  • 107
  • This does not help the OP tbh. – fdomig Aug 01 '12 at 11:25
  • Was my answer correct and accurate? Yes. Then there is no basis to vote it down, but you may. The question does not require a long answer. Did you want an essay? – Mitya Aug 01 '12 at 11:27
  • What has `curl` to do with the question?! – fdomig Aug 01 '12 at 11:31
  • Because cURL *does* allow you to retrieve files (or, rather, their dynamic representations) by passing query strings, so it is entirely possible this is what he was thinking of. I mentioned it in passing at the end of the answer. – Mitya Aug 01 '12 at 11:33
  • So does php. `include 'http://example.com/foo.php?id=123'` (although the security concerns) works just fine. See example #3 in the docs http://at.php.net/manual/en/function.include.php – fdomig Aug 01 '12 at 11:36