Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to exit only the php file being included?

Tags:

php

So, I have a sidebar.php that is included in the index.php. Under a certain condition, I want sidebar.php to stop running, so I thought of putting exit in sidebar.php, but that actually exits all the code beneath it meaning everything beneath include('sidebar.php'); in index.php all the code would be skipped as well. Is there a way to have exit only skip the code in the sidebar.php?

like image 442
Strawberry Avatar asked Jul 17 '11 20:07

Strawberry


People also ask

How do I exit PHP file?

The exit() function in PHP is an inbuilt function which is used to output a message and terminate the current script. The exit() function only terminates the execution of the script.

How do I stop a PHP script from running in the background?

If you started it in background use ps aux | grep time. php to get PID. Then just kill PID . If process started in foreground, use to interrupt it.

How do I close a PHP terminal?

You have to type ctrl + C to exit.

How do I exit an if statement in PHP?

You can't break if statements, only loops like for or while. If this if is in a function, use 'return'.


2 Answers

Just use return;

Do also be aware that it is possible to actually return something to a calling script in this way.

if your parent script has $somevar = include("myscript.php"); and then in myscript.php you do say... return true; you will get that value in $somevar

like image 53
Olipro Avatar answered Sep 17 '22 23:09

Olipro


Yes, you just use return;. Your sidebar.php file might look something like this:

<?php  if($certain_condition) {     return; } else {     // Do your stuff here }  ?> 
like image 28
Emmanuel Avatar answered Sep 19 '22 23:09

Emmanuel