Jump to content
Old threads will finally start getting archived ×

Leaderboard

Popular Content

Showing content with the highest reputation on 02/25/2025 in all areas

  1. In any code that I work with that doesn't use a templating engine I try to label my conditionals, like so: <div class="slew-of-form-elements"> <?php if($myVar === true): ?> <input type="text" name="field_1"> <select name="field_2"> <option value="-1">Select an option</option> <?php foreach($options as $key=>$value): ?> <option value="<?= $key; ?>"><?= $value; ?></option> <?php endforeach; // $options as $option ?> </select> <?php endif; // $myVar === true ?> </div> Obviously this is a contrived example and is missing things like output sanitization and the many, many, many form elements some of the code deals with but hopefully the point comes across.
    1 point
  2. The lack of indentation makes it entirely unclear what the control flow of your script has, since indentation is missing and it's hard to understand by looking at it, what runs or doesn't. This is generally considered to be spaghetti. The best practice for intermingling HTML and PHP is to utilize the PHP alternative syntax tags, which allow your code to integrate more cleanly with your html. You are already using the "<?= ?>" shorthand syntax for individual values, which is good. Extend that to also using the control tag structure, and add indentation. Make sure that your editor is configured to use spaces for tabs! Once you have done that, it's possible in most editors to go back and replace all your tab characters with your standard tab size. Typically people choose either 2 or 4 spaces per tab. If you adhere to https://www.php-fig.org/psr/psr-12/ then it should be 4 spaces of indentation (a tab should create 4 spaces). As this is a snippet from a larger script, we are obviously missing the context of the rest of the code, but I re-wrote your snippet using the alternative syntax for comparison. <?php if ($_SESSION["DE_Retain"] == 1): ?> <input type="text" maxlength="32" size="42" name= "<?= $Comment_Name ?>" value="Des">&nbsp;&nbsp;Max 32 Characters<br><br> <?php else: ?> <input type="text" maxlength="32" size="42" name= "<?= $Comment_Name ?>" value="Dave">&nbsp;&nbsp;Max 32 Characters<br><br> <?php endif; ?>
    1 point
  3. You don't need the braces so long as there are only single statements to be executed but only if they are in the same <?php .. ?> block of code. EG this works... <?php if (date('d')==24) echo 'today'; else echo 'Not today'; ?>
    1 point
This leaderboard is set to New York/GMT-04:00
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.