How to remove file including with include according to conditions

Asked

Viewed 880 times

2

I did a basic search and found nothing. I imagine it must be something simple, and I might be "looking wrong"...

So 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 it another way, like with switch?

  • Hi Gustavo, I replied, but I would like to know the purpose of this, why so I can perhaps suggest you an alternative to do something similar :)

  • 2

    It would not be better to create a function that returns an array with all includes and then just invoke them?

  • @rray Great strategy +1 is worth a response

  • @Guilhermenascimento variables receive the information of a form, and depending on the choice of a select for example, I include a file with a block of text (will form a full text joining these blocks)... Do you understand that? So sometimes I have to swap these blocks, because a later choice (a select on another page) includes another file instead of the one that would be included if the previous choice did not affect the later one...

  • @rray and how would I do it? Can you give an example?

3 answers

3


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

Supposing that 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 included file.

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

Another thing (I’m not sure about this), software option for PHP generate already processed versions of PHP, in this case the included file is processed along with what it includes, as if it were just a file (I think).

Altenativa

As @rray suggested, you can call includes after ifs, using an array for example, it would be 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 were true it will include the files file1.php and file2.php

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

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

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

  • Cool! Everything became clearer! : -) Thanks! + 1

3

No way to remove an already included file.


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

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

What I mean by this is that there would be no need to remove 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 file file1.php will be included, but if another request is made and the variable $var for falsa the file file2.php will be included and the file file1.php it will not exist, this because, as I said, when a requirement is made it starts again.


I made a little code to exemplify that, 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 find that by changing the value of n no link (test.php?n=1 or test.php?n=2) the existing previous file will not be included.

  • Good answer, thanks! I liked the idea with the switch. I will study better here the options that emerged. + 1

2

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

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(), it may also be interesting to set a constant with the prefix of some folders.

  • So, I’m still not using sessions, but I liked the solution with foreach... the idea of the other answer with switch also looks good... I’ll study here better, I think I’ll have to review the logic I was thinking (only with ifs). Thanks for the answer, I think soon (when deploy user control) will be even more useful than now! + 1

  • 1

    Depending on the complexity you can create settings/profiles and write/recover from the database and deal with the session. Example profile 1, loads the config and the admin, profile 2 load, somente_leitura and interface_basica. @gustavox

  • That’s pretty much what I’m gonna have to do soon. For now I am only using in localhost, developing still, but soon I will have to delve into sessions, I have already arranged some books, because I know that this part will be one of the biggest (database, sessions etc) challenges of my project... thanks even!

  • +1 for the great idea (rray is the strategist of the codes the/ )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.