D
D
DANIIILKA2014-10-28 21:29:48
Programming
DANIIILKA, 2014-10-28 21:29:48

include and return construct?

Example 5. include() and return()
statement return.php
$var = 'PHP';
return $var;
noreturn.php
$var = 'PHP';
testreturns.php
$foo = include 'return.php';
echo $foo; // prints 'PHP'
$bar = include 'noreturn.php';
echo $bar; // prints 1
but there is no function for it to work return?why prints php

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Surin, 2014-10-28
@BedwaRe

Maybe it will be clearer this way:
File foo.php

$foo = "foo-php";
return $foo;

File bar.php
Connection:
$x = include("foo.php"); // $x хранит "foo-php", т.к. использовался return
$y = include("bar.php"); // $y хранит 1, т.к. нет return, соответственно функция include вернула 1

If the file could not be connected, false would be returned in both cases.

D
Dmitry Entelis, 2014-10-28
@DmitriyEntelis

Return Value Handling: The include statement returns FALSE on error and issues a warning. Successful includes, unless overridden in the include file, return a value of 1. It is possible to issue a return statement within an include file to end the execution process in that file and return to executing the include file. It is also possible to return a value from include files. You can get the include value as if you were calling a normal function. Although this is not possible when including a remote file, only if the output of the remote file does not contain the correct PHP start and end tags (just like the local file). You can define the required variables inside these tags and they will be present in the place where the file was included.
Because include is a special language construct, parentheses are not required to enclose an argument. Be careful when comparing the return value.
php.net/manual/en/function.include.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question