How does the include command work?

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
musicmouse
New php-forum User
New php-forum User
Posts: 5
Joined: Fri Jan 04, 2019 5:04 am

Wed Jan 15, 2020 8:49 am

I have some code that looks somethings like

Code: Select all

for(i=0; i<100; i++)
   myfunc(i)

function myfunc(i)
{ do something...
  if(file_exists('paidextension.php')
     include('paidextension.php');
}
paidextension.php contains just a number of php lines and no functions.

It does work. But I am puzzled how those many calls to include() work out.

Does anyone know more about this?
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 904
Joined: Mon Feb 22, 2016 5:52 pm

Wed Jan 15, 2020 11:01 am

Include does just that, it includes a file in exactly that place:

file1.php

Code: Select all

<?php
echo 'file1.php';
file2.php

Code: Select all

<?php
echo 'file2.php';
runthis.php

Code: Select all

<?php
include 'file1.php';
include 'file2.php';
PHP creates this to parse:

Code: Select all

echo 'file1.php';
echo 'file2.php';
Includes are included at the that point, so where an include is included inside a function, the loaded file will be loaded inside that function.

Have a play with includes to see how it behaves.
Post Reply