Reputation: 6955
Say I have a the following files
<?php
session_start();
function getSessionData(){
...
// returns array
}
?>
<?php
session_start();
include("functions.php");
$sessionData = getSessionData();
?>
<html>
<head>
<title>test</title>
</head>
<body>
<?php print_r($sessionData); ?>
</body>
</html>
Now the index.php includes functions.php. Because I have session_start()
in index.php, does this mean it's automatically added in the functions.php (seeing as functions is included in index?)
Im not sure if im making this clear or not.
<?php
$url = "www.example.com";
?>
<?php
include("config.php");
function getSomething(){
...
return $url
}
?>
<?php
include("config.php");
include("functions.php");
$some_var = getSomething();
?>
<html>
<head>
<title>test</title>
</head>
<body>
<?=$some_var;?>
</body>
</html>
Now both functions.php AND index.php include config.php...
But because config is already included in index.php...
does this mean it has to be included functions aswel?
I think I've just confused myself tbh:)
Upvotes: 1
Views: 74
Reputation: 46987
Yes. You can think of includes as simply pasting the code in the file where the include statement is.
In example 2, you don't need to include config again in index, as it has already been included in functions (or vice versa) - what you're actually doing there is running the code in config.php twice (which can be prevented by using include_once
for example).
The same goes for session_start()
in example 1. When index is loaded the following happens:
session_start
session_start
(not needed now)function getSessionData() {..}
getSessionData()
Also, in your second example, you won't be able to access $url
in that function without calling global $url
before it (inside the function).
Upvotes: 6