0

Having issue getting this to work. I have an array in 'key.php' and I want to include it in 'processor.php' and check if a posted value is in it. I either get 500 errors or it returns the array as a string.

key.php

<?php $foo = array('thing1', 'thing2'); ?>

processor.php

<?php
    $bar = include('/path/to/key.php');
    $email = $_POST('email');
    if (in_array($email, $bar)) {
       echo('in array');
    } else {
        echo('not in array');
    }
?>
3
  • 3
    Please read what include DOES and what it RETURNS... hint: it's not what you think Commented Mar 19, 2019 at 18:38
  • You should look into what causes the 500 errors. Those are usually hidden by default so users won't see them on a production environment. To cause errors to show up, you can do: error_reporting(E_ALL);ini_set('display_errors', 1); Put that code at the top of your PHP script. Here is another resource that can help: stackoverflow.com/questions/1053424/… Commented Mar 19, 2019 at 18:40
  • Will having the '<?php .. ?>' in key.php cause problems? I don't think you can embed php block inside another php block? Commented Mar 19, 2019 at 18:50

2 Answers 2

2

Try changing if (in_array($email, $bar)) with if (in_array($email, $foo)), and instead of including the file like this $bar = include('/path/to/key.php'); just include it like this include '/path/to/key.php';.

The variable name you want to check for is named $foo , not $bar. When including other php files, you don't need to set them as variables. Let me know how it goes :)

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

1 Comment

Got it. Fixed my include path, didn't use it as a variable and it works. Thank you.
1

Documentation says:

The include statement includes and evaluates the specified file.

This is equivalent to copy/paste the entire file into that point in the calling script. You should simply do:

<?php
    include('/path/to/key.php'); //here you are defining $foo
    $bar = $foo; 
    //now you can continue with the rest of your original script
    $email = $_POST('email');
    if (in_array($email, $bar)) {
       echo('in array');
    } else {
        echo('not in array');
    }
?>

(or directly checking $foo, not $bar)

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.