I have this PHP code. I wrote it for a webpage it uses parameters. Only one set of parameters can be set at any one time. If both are set its invalid, if None are set its invalid
How would be the best way to write it, looks too long to me and i Feel like I have complicated it
if($_REQUEST['b_id'] && $_REQUEST['s_id']) {
die('<center style="color: #000; background-color: #FFF; height:100vh; font-size: 4vw;">Not A Valid Request URL</center>');
}
if(!$_REQUEST['b_id']) {
$b = false;
} else {
$b = true;
}
if(!$_REQUEST['s_id']){
$s = false;
} else {
$s = true;
}
if($b === $s) {
die('<center style="color: #000; background-color: #FFF; height:100vh; font-size: 4vw;">Not A Valid Request URL</center>');
} else if($_REQUEST['b_id'] == $_REQUEST['s_id']) {
die('<center style="color: #000; background-color: #FFF; height:100vh; font-size: 4vw;">Not A Valid Request URL</center>');
}
PS the code works as expected. I just need help making it neater little less confusing.
1 Answer 1
You can combine the conditions. It's invalid if both are set: $_REQUEST['b_id'] && $_REQUEST['s_id']
OR if neither are set: !$_REQUEST['b_id'] && !$_REQUEST['s_id']
. The intermediary variables are unnecessary.
Additionally, the <center>
tag has been deprecated for some 20 years now and the use of inline styles is discouraged. You should make the element a <div>
with a class and set the style in CSS, including a text-align: center;
rule to get the same effect.
if( ($_REQUEST['b_id'] && $_REQUEST['s_id']) || (!$_REQUEST['b_id'] && !$_REQUEST['s_id']) ) {
die('<div class="errormessage">Not A Valid Request URL</div>');
}
-
\$\begingroup\$ Ahh I see, let me attempt the code. I am fully aware of the center tag being deprecated. I use it in development then replace it when I am done \$\endgroup\$Andrew Christopher– Andrew Christopher2017年11月10日 08:53:25 +00:00Commented Nov 10, 2017 at 8:53
-
\$\begingroup\$ Worked like a charm \$\endgroup\$Andrew Christopher– Andrew Christopher2017年11月10日 08:54:55 +00:00Commented Nov 10, 2017 at 8:54
-
\$\begingroup\$ I wasnt aware that working code goes here thanks for the heads up on that \$\endgroup\$Andrew Christopher– Andrew Christopher2017年11月10日 08:55:31 +00:00Commented Nov 10, 2017 at 8:55
$c
? \$\endgroup\$