开发者

php include prints 1

开发者 https://www.devze.com 2023-02-12 14:29 出处:网络
i code the following <?php if ($id = mysql_real_escape_string(@$_GET[\'pid\']) 开发者_如何转开发&& $uid = mysql_real_escape_string(@$_GET[\'file\']))

i code the following

<?php 
if ($id = mysql_real_escape_string(@$_GET['pid']) 
    开发者_如何转开发&& $uid = mysql_real_escape_string(@$_GET['file']))
       echo include "foo.php"; 
    else 
       echo include "bar.php"; 
?>

When I use the include function in conjunction with a function that's designed to output to the page (e.g., or echo include 'foo.php'), it returns the include but with a "1" after the content that has been included.


echo include "foo.php"

should be

include 'foo.php';


Note that this can also happen when using include with shorthand echo:

<?= include 'foo.php'; ?>

This will also print out the return value of 1 when used inside a script. To get rid of this you need to use the regular PHP opening tag like so:

<?php include 'foo.php'; ?>

PHP will now include the contents of the file without printing the return value.


Okey so the answers here are actually not entirely correct; in some sense even misleading.

include takes the contents of the file and places them in context. One of the more common uses is to pass variable scope around, ie. passing scoped variables in your view by including them in the handler and using include on the view. Common, but there are also other uses; you can also return inside a included file.

Say you have a file like this:

<?php return array
    (
        'some',
        'php'
        'based'
        'configuration',
        'file'

    ); # config

Doing $config = include 'example-config-above.php'; is perfectly fine and you will get the array above in the $config variable.

If you try to include a file that doesn't have a return statement then you will get 1.

Gotcha Time

You might think that include 'example-config-above.php'; is actually searching for the file in the directory where the file calling the include is located, well it is, but it's also searching for the file in various other paths and those other paths have precedence over the local path!

So if you know you had a file like the above with a return inside it, but are getting 1 and potentially something like weird PEAR errors or such, then you've likely done something like this:

// on a lot of server setups this will load a random pear class
include 'system.php' 

Since it's loading a file with out a return you will get 1 instead of (in the case of our example) the configuration array we would be expecting.

Easy fix is of course:

include __DIR__.'/system.php' 


That is because the include function returns 1 on success. You get, as you say, 'my name is earl1' because the code inside the included file runs first, printing 'my name is earl' and then you local echo runs printing the return value of include() which is 1.


Let the file.txt contain xxx

 echo include("file.txt");

This returns, xxx1

The '1' is the return value of the include function, denoting the success that the file is accessed. Otherwise it returns nothing, in this case parse error is thrown.

  echo print "hello";

This too returns, hello1

Same as above; '1' denoting the success,it's printed.

echo echo "hello";
print echo "hello";

Both the above cases produces an error. Since the echo function has no return value, hence undefined.

echo  echo "hello";           print echo "hello";
(1st) (2nd)                     

Now the second 'echo' in the both cases produces an undefined.The first 'echo' or 'print' can't take in the hello output with the undefined (produced by the second echo).

A verification:

if((print "hello")==1)         
  echo "hey!";

 output: hellohey!    ('echo' in the 2nd line can be a print, it doesn't matter)

Similarly,

if((include ("file.txt"))==1)
echo "hey!";

output: xxxhey! 

Other hand,

if((echo "hello")==1)
echo "hey!";

output: an error

In the first two cases the functions (print and include) returned 1, in the third case 'echo' produces no return value (undefined) hence the third case produces an error.


Well... I am using Codeigniter(php frame work). And I encountered the same problem. What I concluded is that when we try to print/echo the include method then it prints 1 on screen and when we just simply write the include command(example given below) it will only do what it is supposed to do.

<?php include('file/path'); ?> // this works fine for me
<?= include('file/path'); ?> // this works fine but prints "1" on screen

Hope my explaination will be helful to someone


= is assigning operator

== is for checking equal to

check for php operators


I have solved it returning nothing at the end of the included file:

$data = include "data-row.php";
return $data;

Inside data-row.php:

<div>etc</div>
...
<?php return; //End of file


I found the selected answer from this thread very helpful.

Solution 1

ob_start();
include dirname( __FILE__ ) . '/my-file.php';
$my_file = ob_get_clean();

You might also find this thread about the ob_start() function very insightful.

Solution 2

Add a return statement in the file that is being included.

E.g my-file.php

<?php

echo "<p>Foo.</p>";

return;

Drawn from the answer provided by @gtamborero.

To help you understand it the way I do now, just take this oversimplification:

There should always be a return statement otherwise include will return 1 on success.

Happy coding!

M5


Use return null in foo.php and bar.php


echo substr(include("foo.php"),1,-1);

0

精彩评论

暂无评论...
验证码 换一张
取 消