BonifatiusK
BonifatiusK

Reputation: 2331

PHP Variable scope and globals

I have two php files. the first file will include the second one. This all works. However, int the second file I have an array:

//set required items
$reqSettings = array(
    "apiUser" => true,
    "apiPass" => true,
    "apiKey" => true,
);

In a function called in the first file I want to loop through this array, however it is not recognized by the function:

function apiSettingsOk($arr) {
    global $reqSettings;

    $length = count($reqSettings);

    echo $length; //returns 0 and not 3
}

As you can see I tried using 'global' but this doesn't work either. Can you help me out fixing this issue?

Here are the two files just for completeness sake ;)

file 1:

$apiArr = array();

if (isset($_POST['api-submit'])) {

    $gateWay =  $_POST['of-sms-gateway'];
    $apiArr['apiUser'] = $_POST['api-user'];
    $apiArr['apiPass'] = $_POST['api-passwd'];
    $apiArr['apiKey'] = $_POST['api-key'];

    //including the gateway file
    include_once('of_sms_gateway_' . $gateWay . '.php');

    if (apiSettingsOk() === true) {
        echo "CORRECT";
    }

}

?>

of_sms_gateway_test.php :

<?php

//set required items
$reqSettings = array(
    "apiUser" => true,
    "apiPass" => true,
    "apiKey" => true,
);

function apiSettingsOk($arr) {
    global $reqSettings;
    $returnVar = true;

    $length = count($reqSettings);

    echo $length;

    return $returnVar;

}
?>

Upvotes: 0

Views: 251

Answers (3)

php_nub_qq
php_nub_qq

Reputation: 16055

You have put an argument $arr to your function which you do not supply. Have it like so:

if (apiSettingsOk($reqSettings) === true) {
    echo "CORRECT";
}

And the function

function apiSettingsOk($arr) {
echo count($arr); //returns 0 and not 3
}

Upvotes: 1

BonifatiusK
BonifatiusK

Reputation: 2331

Great thanks for the help.

Using that I also found out it helps to declare the $reqSettings as global in the first file and in the function in the second file to do the same so

file1.php

<?php
    global $reqSettings;
    $apiArr = array();

    if (isset($_POST['api-submit'])) {

        $gateWay =  $_POST['of-sms-gateway'];
        $apiArr['apiUser'] = $_POST['api-user'];
        $apiArr['apiPass'] = $_POST['api-passwd'];
        $apiArr['apiKey'] = $_POST['api-key'];

        include_once('of_sms_gateway_' . $gateWay . '.php');

        if (apiSettingsOk($apiArr) === true) {

            echo "OK";

        } else {
            echo "ERROR";
        }

    }

?>

file2.php

<?php

    $reqSettings = array(
        "apiUser" => true,
        "apiPass" => true,
        "apiKey" => true,
    );

    function apiSettingsOk($arr) {
        global $reqSettings;
        $returnVar = true;

        $length = count($reqSettings);
        echo $lenght; //now shows 3

        return $returnVal;
    }

?>

Upvotes: 0

Hasina Ansari
Hasina Ansari

Reputation: 845

Please include "file1.php" to "file2.php", then it will work.

Example :

file1.php

<?php

$array = array(
    "name" => "test"
);

?>

file2.php

<?php

 include_once("file1.php");

 function test()
 {
     global $array;
     echo "<pre>";
     print_r($array);
 }

 test();
?>

Here, you can see, it will print $array in file2.php. which is declared in file1.php.

Hope it will help.

Upvotes: 2

Related Questions