I've following snippet of code that is written using conditional operator :
$iPage=($this->request()->get('search') ? $this->request()->getInt('req4') : ($this->request()->getInt('req3') ? $this->request()->getInt('req3') : 1 ));
I converted this into if-else format as follows :
if($this->request()->get('search')) {
$this->request()->getInt('req4')
} else {
($this->request()->getInt('req3')
}
Would someone please help me in telling that whether I've made the code conversion appropriately? If I've made any mistake in this conversion please correct the mistake and let me know the mistake.
This is the original code (using the conditional operator) formatted to be more readable (the outer parenthesis are not needed at all):
$iPage = (
$this->request()->get('search') ?
$this->request()->getInt('req4') :
(
$this->request()->getInt('req3') ?
$this->request()->getInt('req3') :
1
)
);
On the first step let's extract $this->request()
into a variable; it makes the code easier to read and it runs faster (avoid doing the same function call four time; it returns the same value every time):
$request = $this->request();
$iPage = (
$request->get('search') ?
$request->getInt('req4') :
(
$request->getInt('req3') ?
$request->getInt('req3') :
1
)
);
Now, let's replace the conditionals with if/then/else
:
$request = $this->request();
if ($request->get('search')) {
$iPage = $request->getInt('req4');
} else {
if ($request->getInt('req3')) {
$iPage = $request->getInt('req3');
} else {
$iPage = 1;
}
}
Now, let's notice that the value 1
is the default value of $iPage
. If some conditions regarding the request are met, the value of $iPage
is computed using the values of $request
; otherwise $iPage
becomes 1
. Let's extract this information and simplify the code:
// Default value
$iPage = 1;
// Get the request into a variable for faster and shorter code
$request = $this->request();
// If 'search' is set, use 'req4' else use 'req3'
if ($request->get('search')) {
$iPage = $request->getInt('req4');
} else {
if ($request->getInt('req3')) {
$iPage = $request->getInt('req3');
}
}
Now we can combine the else
with the if
it contains into:
// Default value
$iPage = 1;
// Get the request into a variable for faster and shorter code
$request = $this->request();
// If 'search' is set, use 'req4' else use 'req3'
if ($request->get('search')) {
$iPage = $request->getInt('req4');
} elseif ($request->getInt('req3')) {
$iPage = $request->getInt('req3');
}
You may feel it easier to understand if we put the default value back into the if
/else
construct, like this:
// Get the request into a variable for faster and shorter code
$request = $this->request();
if ($request->get('search')) {
// If 'search' is provided, use 'req4'
$iPage = $request->getInt('req4');
} elseif ($request->getInt('req3')) {
// else, use 'req3' if provided
$iPage = $request->getInt('req3');
} else {
// else default to 1
$iPage = 1;
}
Alternatively, if you don't mind to use the conditional operator (and get a more compact code) you can pack the second if
back into the ?:
form using a feature introduced by PHP 5.3 ("Since PHP 5.3, it is possible to leave out the middle part of the ternary operator"):
// Get the request into a variable for faster and shorter code
$request = $this->request();
// If 'search' is provided, use 'req4'
if ($request->get('search')) {
$iPage = $request->getInt('req4');
} else {
// Use 'req3' if provided else default to 1
$iPage = $request->getInt('req3') ?: 1;
}
Use any of the above transformations of the code; all of them provide the same outcome.
I personally prefer the last one; it is compact and easy to read.