Include file and nothing else in PHP? - php

I am currently including a new php file to an already existing page in the form of
if (...) {
...
}
include("old_file");
Is there a way to include this new file in the conditional without changing the part outside / including an else?
e.g.
if (...) {
Code that should be executed if condition is set.
include("new_file");
}
Code that should not be executed if condition is set.
include("old_file");
...

You can call exit to stop execution, or call return inside an included file (probably works fine outside an include as well).
if (...) {
Code that should be executed if condition is set.
include("new_file");
exit(); // terminates execution
}
// Code that should not be executed if condition is set.
include("old_file");
...

if (...) {
// Code that should be executed if condition is set.
include("new_file");
return; // <-- now you skip the rest of the file. Actually, everything after the if block.
}

Related

Ignoring included file's exit or die

I'm looking for a method to ignoring die() or exit functions.
Here is an example
Main file:
//Here some process
include 'seconfdile.php';
Second file:
//Some process
die();
//more process
Instead of die() use return in second file.
The die() in include/child file will kill the parent script execution as well.
So, its better to use return instead of die() it will terminate only child/include file not the parent file

Use of PHP Return vs Else in include files

I understand that both of the below are valid, however I was wondering which was considered the best solution.
I have some code in an included file. If certain conditions are met, I would like to stop execution of the remaining code in the included file and return to the calling file.
Example 1 has the following code in the included file:
$error = false;
// Some code here that can trigger $error = true
if ($error) {
return; // return to calling file
}
// More code below, only to be executed if $error = false
Example 2 has the following code in the included file:
$error = false;
// Some code here that can trigger $error = true
if (!$error) {
// Execute remaining code within the conditional statement
}
// Return to the calling file
Thanks in advance.
I prefer Example 1, because the early return pattern leads to less indented code. If you have more than one check like the one in Example 2, you will end with heavily indented code:
$error = false;
// Some code here that can trigger $error = true
if (!$error) {
// Execute remaining code within the conditional statement
// Some more code here that can trigger $error = true
if (!$error) {
// Execute remaining code within the conditional statement
}
}
// Return to the calling file
which forces you to remember unnecessarily "this block is in the successful branch of the if".
There's virtually no difference, but I use the 1-st approach for one simple reason: I want the main code body to be more visible, not hidden under tones of brackets and intends.
Include is NOT the same as calling a function, so it gives no meaning talking about to "stop execution of the remaining code in the included file and return to the calling file"
To include something in PHP just means that the text from one file is put into a specific location in another file.

PHP die() from included page without dying from main page

Is there a way to use the die() function to stop executing PHP statements on a page included on another page, but continue the execution of PHP statements on the page on which the file containing the die() function was included?
use return; in your included file. It will stop this include execution. It works like a function. Also you can return a value from your included file
No. die is an alias for exit which immediately stops all script execution.
But you can use return instead, which does exactly what you want:
If called from the global scope, then execution of the current script file is ended. If the current script file was include()ed or require()ed, then control is passed back to the calling file. Furthermore, if the current script file was include()ed, then the value given to return() will be returned as the value of the include() call. If return() is called from within the main script file, then script execution ends.
As stated in the excerpt from the PHP docs, you can even use it to give a exit code / return value back from the include:
$include_retval = include('file_like_function.php');
if ($include_retval) {
die("include returned error code: " . $include_retval);
}
No. You could use try blocks instead?
try
{
include $file;
}
catch (Exception $e)
{
// Whatever
}
And throw an exception where you would use die() in $file.

Skip the rest of the (included) file in PHP

I'm including file inner.php in outer.php, I have a condition in inner.php on which I want to stop executing inner.php but NOT the whole script, i.e. I want to jump to the first line in outer.php after the inclusion of inner.php, and I don't want to wrap all of the code in inner.php in an if statement.
Is there a way to do this otherwise?
Just do return; or return($value); on top level of the inner.php file.
If called from the global scope, then
execution of the current script file
is ended. If the current script file
was include()ed or require()ed, then
control is passed back to the calling
file. Furthermore, if the current
script file was include()ed, then the
value given to return() will be
returned as the value of the include()
call.
You can just call return in your include file, but if you're having to do this then it suggests there is something wrong with your architecture. For example, consider this include file:
<?php
// include.php
echo "This is my include";
return;
echo "This is after the include";
..included on the following page:
<?php
// index.php
include('include.php');
The output you'd get is: This is my include.
How about having two files for inner. The first and the second part and place the condition on the second include?
Throw an exception on the point where you want to stop
// in inner.php:
// ...some code...
throw new Exception('Error description');
// ...some code which will not always execute...
and catch it in the file where you want to resume
// in outer.php
try {
include 'inner.php';
} catch (Exception $e) {
//TODO: add error handling here
}
UPDATE
Unlike using return; as other answers here suggest, using exceptions will break anywhere, even if you're in some function inside inner.php

conditional while loop in php?

I need to do a PHP while loop, but only if a variable is true. And I can't really put the while loop in an "if" statement, which seems like the obvious thing to do, since the code block is huge and it would be ugly and confusing. Do I need to break out the code in the loop into a function, or is there an easier way to deal with this?
Here's the basic idea:
if(condition){
while(another_condition){
//huge block of code loops many times
}
} else {
// huge block of code runs once
}
I want the huge block of code to execute regardless of the state of the condition variable-- but only to execute once if condition is false, and execute for as long as another_condition is true if condition is true.
The following code doesn't work, but gives an idea of what I want to accomplish:
if(condition){ while(another_condition){ }
// huge block of code
if (condition){ } } // closes the while loop-- obviously throws an error though!
thanks in advance.
If I understood your problem correctly, you could use the do ... while() structure:
do
{
// your code
} while(condition);
This will execute // your code once regardless of any factor, and then only for the second iteration and those after will it check for the condition.
For readability if your huge block of code can be separated in several specialized functions, do it. It will surely pay out if you need to debug later.
I would put the huge block of code in a function so it can be used again without having duplicate code.
function hugeBlockOfCode() {
// Huge block of code.
}
while (condition && another_condition) {
hugeBlockOfCode();
}
if (!condition) {
// Run code once.
hugeBlockOfCode();
}
or
do {
hugeBlockOfCode();
} while (another_condition);
Is this kind of what you are looking for?
while (condition && another_condition) {
// large block of code
}
if (!condition) {
// do something else
}

Categories