1
\$\begingroup\$

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.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ It is about working code and should be ask at Code Review \$\endgroup\$
    – Jens
    Nov 10, 2017 at 8:33
  • \$\begingroup\$ Where is your $c ? \$\endgroup\$ Nov 10, 2017 at 8:46
  • \$\begingroup\$ Oh oops mistype \$\endgroup\$ Nov 10, 2017 at 8:50

1 Answer 1

0
\$\begingroup\$

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>');
}
\$\endgroup\$
3
  • \$\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\$ Nov 10, 2017 at 8:53
  • \$\begingroup\$ Worked like a charm \$\endgroup\$ Nov 10, 2017 at 8:54
  • \$\begingroup\$ I wasnt aware that working code goes here thanks for the heads up on that \$\endgroup\$ Nov 10, 2017 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.