How to remove file including with include according to conditions

2

I gave a basic search and found nothing. I imagine it should be something simple, and that I may be "looking wrong" ...

Then for example:

<?php
    if ($var1 != "qualquercoisa") {
        include "arquivo1.php";
    }
    elseif ($var2 == "outracoisa") {
        include "arquivo2.php";
        **remove** "arquivo1.php";
    }
?>

Is there a command that does this, or do I have to do otherwise, type with switch ?

    
asked by anonymous 28.06.2015 / 19:29

3 answers

3

The PHP process is not something "asynchronous", the included file runs as if it were part of the file that is including it.

Assuming you have a.php :

<?php
echo 1;

And b.php :

<?php
include 'a.php';
echo 2;

For the PHP interpreter, it will be the same as

<?php
echo 1;
echo 2;

This is similar to C / C ++ where the included file is compiled "together" with the file it includes.

No has to remove the included file, also note that include and require are not functions.

Another thing (I'm not sure about this), opcache softwares for PHP generate already processed versions of PHP, in which case the included file is rendered next to what it includes, as if it were just a file. I think).

Altenative

As the @rray suggested, you can call the includes after the ifs, using an array for example would look something like:

<?php
$includes = array();

if (condicao1) {
   $includes[] = 'file1.php';

   if (condicao2) {//condição dois
       $includes[] = 'file2.php';
   } else {
       $includes[] = 'file3.php';
   }
} else {
   $includes[] = 'file4.php';

   if (condicao2) {//Aqui repete a condição dois
       $includes[] = 'file2.php';
   }
}

foreach($key as $includes) {
   include $key;
}
  • If condicao1 and condicao2 are true it will include the files file1.php and file2.php

  • If condicao1 is true and condicao2 is false it will include the files file1.php and file3.php

  • If condicao1 is false and condicao2 is true it will include the files file4.php and file2.php

  • If condicao1 is false and condicao2 is false it will include only the file file4.php

28.06.2015 / 19:38
3

There is no way to remove a file already included.

  

The code is interpreted on the server side by the PHP module, which also generates the webpage to be viewed on the client side.

This means that every time the user refreshes the page or opens the page, it sends a requirement to the server and the server interprets the server-side code ( server-side ) .

What I mean by this is that there would be no need to remove the include since when a new requirement is made everything is done from the beginning, so if we have a condition:

if($var == true)
{
    include "file1.php";
} else {
    include "file2.php";
}

In the first request if the variable $var is equal to true the file1.php file will be included, but if another request is made and the $var variable is falsa the file2.php file will be included and the file1.php file would not exist, because, as I said, when a requirement is made it starts again.

I made a small code to exemplify this, test for yourself.

TestB.php

<?php
echo "Hello from A";

TestA.php

<?php
echo "Hello from A";

Test.php

<?php

$var = $_GET['n'];

switch($var)
{
    case 1:
    {
        include "testA.php";
        break;
    }
    case 2: 
    {
        include "testB.php";
        break;
    }
}

You will see that when you change the n value in the link (test.php? n = 1 or test.php? n = 2 ) the existing existing file is not included.

    
28.06.2015 / 20:00
2

As stated in the other answers, it is not possible to remove a file already included. I suggest something flexible as given an input a function it will return a (array) array with all files that should be included, after that a foreach is done.

function gabarito($entradas){
   $arquivos = array('config.php', 'segurancao.php');

   if($entrada['perfil'] == 'admin'){
      $arquivos[] = 'perfil/admin.php';
   }elseif($entrada['perfil'] == 'anon'){
     $arquivos[] = '/perfil.anon.php';
     unset($arquivos[0]); //remove as configurações do usuario como temas
   }

   return $arquivos;
}


$arquivos = gabarito($entradas);
foreach($arquivos as $item){
   include $item;
}

echo '<pre>';
print_r(get_included_files());

You can see the files included with the function get_included_files () , can also be interesting to define a constant with the prefix of some folders.

    
28.06.2015 / 20:31