Need explanation of PHP code

jinsatkilife

Senior Member
Joined
May 4, 2019
Messages
2,115
Reaction score
2,103
anyone able to explain the code?

Code:
<label for="category--' . $category . '" class="label label--checkbox">
<div id="option--'. $category .'"' . (($category == 'DRSS' || $category == 'SKTS') ? (($women_selected || !$men_selected) ? '' : ' style="display:none;"') : '') . '>
 
Last edited:

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Code:
<label for="category--' . $category . '" class="label label--checkbox">
<div id="option--'. $category .'"' . (($category == 'DRSS' || $category == 'SKTS') ? (($women_selected || !$men_selected) ? '' : ' style="display:none;"') : '') . '>

I don't think those PHP codes above are complete on their own.

For example this fragment of codes
Code:
<label for="category--' . $category . '" class="label label--checkbox">

It is more likely in this form
Code:
$var = '<label for="category--' . $category . '" class="label label--checkbox">';

Thus dependong on the value of the variable $category, if it is 'DRSS',
then the following similar code below will yield the same result_metadata()

Code:
$var = '<label for="category--DRSS" class="label label--checkbox">';

Another way of writing the above in a more readable manner is using PHP's variable interpolation, hence you can rewrite it as follows
Code:
$var = "<label for=\"category--$category\" class=\"label label--checkbox\">";
Since normally HTML strings can be single quoted or double quoted and the meaning is the same,
thus you can avoid escaping double quotes in a PHP string as follows
Code:
$var = "<label for='category--$category' class='label label--checkbox'>";






For the other fragment,
Code:
<div id="option--'. $category .'"' . (($category == 'DRSS' || $category == 'SKTS') ? (($women_selected || !$men_selected) ? '' : ' style="display:none;"') : '') . '>

It should be of this form too
Code:
$var = '<div id="option--'. $category .'"' . (($category == 'DRSS' || $category == 'SKTS') ? (($women_selected || !$men_selected) ? '' : ' style="display:none;"') : '') . '>';

The following code is analogous
Code:
if ($category == 'DRSS' || $category == 'SKTS') {
    if ($women_selected || !$men_selected) {
        $var = '<div id="option--'. $category .'">';
        // or using variable interpolation and switching to wrapping double quotes
        // $var = "<div id='option--$category'>";
    }
    else {
        $var = '<div id="option--'. $category .'" style="display:none;">';
        // or using variable interpolation and switching to wrapping double quotes
        // $var = "<div id='option--$category' style='display:none;'>";
    }
}
else {
    $var = '<div id="option--'. $category .'">';
    // or using variable interpolation and switching to wrapping double quotes
    // $var = "<div id='option--$category'>";
}
 
Last edited:

jinsatkilife

Senior Member
Joined
May 4, 2019
Messages
2,115
Reaction score
2,103
I don't think those PHP codes above are complete on their own.

yes, i tried to post my full code but encounter error 403

waste 1 hour trying to figure out what was this error before I just give up

and thanks for the explanation! :s12:

I think my issue was 1) the nested ternary operator made it much complicated to decipher;
if its like if else statement, its much easier to see and understand

and 2) the dynamic variable naming

i learnt ternary from javascript; didn't know it applies here also in php
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top