RE: [PHP] i think im crazy..
| From: | Lars Torben Wilson | Date: | Fri, 22 Dec 2000 05:36:05 +0000 |
| Subject: | RE: [PHP] i think im crazy.. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-31541@lists.php.net to get a copy of this message | ||
Maxim Maletsky writes:
> I think so too...
>
> you cannot assign an include() or require() to a variable.
In PHP 4, this is only true for require(). Assuming that you have the
following file (test.inc):
<?php
echo "In an include file.\n";
return 27;
?>
...and the following main file:
<?php
error_reporting(E_ALL);
$retval = include('test.inc');
echo "Returned value is: $retval.\n";
?>
...the expected (and actual) output is:
In an include file.
Returned value is: 27.
This is all explained in the manual:
http://www.php.net/manual/function.include.php
[trim]
> -----Original Message-----
> From: enthalpy [mailto:enthalpy@core.com]
> Sent: Friday, December 22, 2000 8:18 AM
> To: php-general@lists.php.net
> Subject: [PHP] i think im crazy..
>
>
>
> ok.. i have a javascript that i wrote and because of problems ive had to
> resort to doing the following
>
> <?php
> $werd = require("javascript");
> print "$werd";
> ?>
>
> i go to a browser and bring this up.. and its fine except there is a
> "0" at the bottom of the page.. ive gone through all my code and i cant
> find what is printing this. however when i changed the require() to
> include() i get a "1" at the bottom of the page. does anyone know what is
> going on?
Assuming that the file which is named 'javascript' exists on your
include path, and the file contains no <?php or ?> tags, any text in
it will simply be passed straight through and fed to the browser.
I just put together a test file (test.inc) containing only the
following:
<script language='javascript'>
alert("That worked!");
</script>
...and another, called main.html, containing only the following:
<?php
include('test.inc');
?>
When I hit main.html with my browser, I get an alert box up saying
'That worked'. Does this work for you, Jon?
Anyway, when I change main.html to the following:
<?php
$retval = include('reqtest2.txt');
echo $retval;
?>
I got the alert box and a 1. The 1 appears as a TRUE value to indicate
that the file was successfully included; if you do not specifically
return a value from an included file, you can use this to do things
like:
if (!@include('test.inc')) {
echo "Couldn't load 'test.inc'.\n";
}
...since it'll return 0 if the file isn't found or read or
whatever. (The @ is only to suppress the warning message PHP will give
if it can't read the file.)
The require() one you tried would be printing a 0 because it doesn't
do return values like include() does. It either finds the file and
loads it or kills the whole script.
So I'd say you might want to stick with include() here, since at least
then you can catch the error.
Hope this helps,
Torben
> <-----CoreComm-Internet-Services--http://core.com/----->
> (Jon Marshall CoreComm Services Chicago)
> (jonathon.marshall@core.com Systems Engineer II)
> (enthalpy@core.com Network Operations)
> <-----Enthalpy.org------------http://enthalpy.org/----->
> (enthalpy@enthalpy.org The World of Nothing)
> <------------------------------------------------------>
--
+----------------------------------------------------------------+
|Torben Wilson <torben@php.net> Netmill iTech|
|http://www.coastnet.com/~torben http://www.netmill.fi|
|Ph: 1 250 383-9735 torben@netmill.fi|
+----------------------------------------------------------------+