0

So I've search far and wide to try and find an example of this but found nothing. It seems like a simple thing however I continue to get errors.

Essentially what I've got is:

<?php 
    $articleCount = 3;
    include('/newsArticles.php?id=$articleCount'); 
?>

It seems fairly self explanatory. What I need is an include that I can pass a value on into the file which I can then grab with $_GET['id'].

2
  • possible duplicate of PHP - include a php file and also send query parameters Commented Apr 29, 2014 at 15:57
  • A query string is only meaningful in the context of an HTTP request, an include is not such; its a simple read from the local file system. Commented Apr 29, 2014 at 15:58

4 Answers 4

1

You can't add a query string (Something like ?x=yyy&vv=www) onto an include like that. Fortunately your includes have access to all the variables before them, so if $_GET['id'] is defined before you call:

include('/newsArticles.php');

Then newsArticles.php will also have access to $_GET['id'];

Sign up to request clarification or add additional context in comments.

Comments

0

You don't need to pass a $_GET variable. Just set it, and reference it in your included file.

Your included file will have access to $articleCount without any additional work needed.

Comments

0

Try this:

$_GET['id'] = 3;
include('/newsArticles.php'); 

1 Comment

Except the newsArticles.php file is unlikely to be at the filesystem root; so make sure that you're using the path to your web route
0

Since you are including a script it would seem as though you have access to the script itself. Even if you don't have access to the script you can edit the $_GET variable from within the script you showed above, like this:

<?php
    $_GET['id'] = 3; // or $_GET['id'] = $articleCount;
    include('/newsArticles.php');
?>

That's because the script newsArticles.php has the same access to the $_GET variable, unless the script was specifically created so that it extracts the variables from the URL.

Try it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.