logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Coding & Scripting Corner View previous topic :: View next topic
Reply to topic   Topic: PHP Return value of included code is incorrect
Author
walt



Joined: 24 Oct 2015
Posts: 25

PostPosted: Sun 06 Dec '15 20:56    Post subject: PHP Return value of included code is incorrect Reply with quote

Hello, I have a function that executes an include statement, to load the rest of its own code. The included code also contains the function's 'return' statement.

The function works fine, up to the point where the included code returns 'true'. For some reason this gets turned into 'false'.

Below are my two files. Any thoughts?

Thanks!

Code:
<?php
// Filename: test_code.inc

// other code... All code here works fine
return true;

?>

Code:
<?php
// Filename: test.php

class Test{

    public function __construct() {
        $val = $this->test_code();
        echo "Function returned: ".($val === true ? 'true' : 'false')."<br>\n";
    }

    private function test_code(){
        $no_error = @ include(dirname(__FILE__)."/test_code.inc");
        if($no_error) echo "Included file ok: 'test_code.inc'<br>\n";
    }
}
$test = new Test();

?>

Output:
Code:
Included file ok: 'test_code.inc'
Function returned: false
Back to top
DR_LaRRY_PEpPeR



Joined: 26 Nov 2015
Posts: 7

PostPosted: Sun 06 Dec '15 21:30    Post subject: Reply with quote

Your test_code() isn't returning $no_error. Smile So in __construct() $val is actually NULL, which you could see by adding var_dump($val) in there to debug what's happening. Cool
Back to top
walt



Joined: 24 Oct 2015
Posts: 25

PostPosted: Sun 06 Dec '15 22:00    Post subject: Reply with quote

Thank you, I just tested it again to see:

Code:
<?php
// Filename: test_code.inc
return true;
?>

Code:
<?php
// Filename: test.php

class Test{

    public function __construct() {
        $val = $this->test_code();
        if($val == null) echo "Return value is null dummy!";
        else
          echo "Function returned: ".($val === true ? 'true':'false')."<br>\n";
    }

    private function test_code(){
        $no_error = @ include(dirname(__FILE__)."/test_code.inc");
        if($no_error) echo "Included file ok: 'test_code.inc'<br>\n";
    }
}
$test = new Test();

?>

Code:
Output:Included file ok: 'test_code.inc'
Return value is null dummy!


Quote:
Your test_code() isn't returning $no_error

Sorry, I don't know what this means. I guess after a few thousand lines of php I was getting too confident. Could you explain, while we are here, what '$no_error' is? Once code has been 'incuded' why doesn't it act like it was just there all the time? I'm missing something, and will do some googling.

Thank you!

ps Just realized that $no_error is a variable in my test code function. Now I'm really confused. However, the value being returned is definitely NULL. What it should be returning is 'return true' from the included file.
Back to top
DR_LaRRY_PEpPeR



Joined: 26 Nov 2015
Posts: 7

PostPosted: Sun 06 Dec '15 23:02    Post subject: Reply with quote

The true IS being return from the include'd file (since your if ($no_error) succeeds), but it's only assigned to $no_error (a variable that's only local to test_code(), nowhere else Smile). So in order to get it back to __construct(), you have to return it. test_code() currently isn't returning anything, so NULL is the default return value (if you didn't know that). That's where the NULL is coming from.

To get what you want, just change test_code() to this:

Code:
    private function test_code(){
        $no_error = @ include(dirname(__FILE__)."/test_code.inc");
        if($no_error) echo "Included file ok: 'test_code.inc'<br>\n";

        return $no_error;
    }


Hope that helps more? Very Happy
Back to top
walt



Joined: 24 Oct 2015
Posts: 25

PostPosted: Mon 07 Dec '15 0:10    Post subject: Reply with quote

It does thank you. I have also been reading and found some interesting information:
Quote:
http://php.net/manual/en/function.include.php
Also, it's possible to return values from included files. You can take the value of the include call as you would for a normal function

Which is what you have been trying to tell me, it just took a while to take in. However, while testing, I found that returning '$no_error' has some quirks. For example, if your included code returns 'false' this will be interpreted as the 'include' command having failed, and you will be returning 'null' instead of the real data 'false'.

So, what I think I'm going to do, is not include the 'return' statement in the included code. It should make things more predictable.

Since the rest of the include behavior makes sense:
Quote:
If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function.

I will use an external return, with the value I want to return from the include file code.

However, I have just tried the new knowledge, and still it's behaving inexplicably:

Code:
<?php
// Filename: test_code.inc
$value = true;
?>

Code:
<?php
// Filename: test.php

class Test{

    public function __construct() {
        $val = $this->test_code();
        if($val == 'anything whatever') echo "Error including file 'test_code.inc'<br>";
        echo "Value = ".$val;
    }

    private function test_code(){
        $ok = @ include(dirname(__FILE__)."/test_code.inc");
        return $value;
    }
}
$test = new Test();
?>

Output:
Code:
Error including file 'test_code.inc'
Value = 1


Mystery: if in the include file I set
Code:
$value = true;

then
Code:
if($val == 'anything whatever')

always evaluates to 'true'. This only happens if I set 'value' = true in the included file. Any other value: 67, "I'm clueless", "false", works as expected.
Back to top
DR_LaRRY_PEpPeR



Joined: 26 Nov 2015
Posts: 7

PostPosted: Mon 07 Dec '15 0:33    Post subject: Reply with quote

For that comparison stuff, see: http://php.net/manual/en/language.types.type-juggling.php Smile

Yep, gotta remember those PHP quirks!

'anything whatever' gets converted to boolean type for the comparison with the actual boolean $value of true. (Only "" (empty string) and "0" get converted to FALSE, anything else is TRUE -- for strings, anyway; NULL, empty array, etc. also get converted to FALSE if compared with a boolean.)

For your other strings that work "as expected," the expected string comparison is done!

For comparing with a number (67), the boolean gets converted to an integer (1/0 for TRUE/FALSE).


Of course if you use the strict comparison operator === no type juggling happens (I noticed you had that in part of your code before). Of course sometimes you may not want that, and being strict with checks might not always behave to way you'd like... Shocked
Back to top
walt



Joined: 24 Oct 2015
Posts: 25

PostPosted: Mon 07 Dec '15 0:38    Post subject: Reply with quote

Sorry! I just figured it out and was about to erase the nonsense. Of course, everything == true in php except for 'false' and 0 Smile.

It's just been a long day.

Thank you very much for all your help.

Walter

PS You know that everything will ok, if after having figured everything out, you computer crashes.
Back to top


Reply to topic   Topic: PHP Return value of included code is incorrect View previous topic :: View next topic
Post new topic   Forum Index -> Coding & Scripting Corner