-2

I want to write this line in one outer php tag. May be we can use echo statement with concatination? Or someone can use ternary statement to beautify it?

<?php if ( condition = true ) { ?>

  <p ><label for="user_limit" > User - Limit(&euro;)</label ><input type = "text" name = "user_limit" id = "user_limit" size = "40" value = "<?php echo inout($res['user_limit']);?>" /></p >

<?php } ?>
hammadshahir
  • 350
  • 4
  • 7
  • 2
    Seems like something you could have tried to do yourself, and then if you get a problem you could have asked whats wrong with my attempt – RiggsFolly Feb 05 '20 at 13:00
  • Why the ternary tag? – Markus Zeller Feb 05 '20 at 13:03
  • "May be we can use echo statement with concatination" [sic] ....yes you could but IMHO it'll probably end up less clear / readable / maintainable than it is now - you'll end up with more issues with escaping of quote marks etc for one thing, and the HTML layout will be less clear. – ADyson Feb 05 '20 at 13:03
  • @ADyson Is my `echo` so unmanagable? If so I will delete it – RiggsFolly Feb 05 '20 at 13:21
  • @RiggsFolly it's a matter of personal preference I guess. Personally I wouldn't do it like that. I find it ugly, less readable etc, all the things I said above. You've had to make a change (single quotes for double quotes round the HTML attributes, which isn't a big deal but there are other places it can become more of a problem). But OP asked for it, so leave it there if you want. At least they know what the choice is now. – ADyson Feb 05 '20 at 13:29

2 Answers2

2

Personaly I would not shorten your code because that would make it less readable, only thing I would change is using syntactic sugar to make it easier to understand where the if statement stops like this:

<?php if ( condition = true ): ?>

  <p ><label for="user_limit" > User - Limit(&euro;)</label ><input type = "text" name = "user_limit" id = "user_limit" size = "40" value = "<?php echo inout($res['user_limit']);?>" /></p >

<?php endif; ?>

In my opinion using if/endif combo is more readable when combining html and php.

However if you want to make it more compact, at the expense of readibility you can use this one liner:

<?= ($condition) ? '<p ><label for="user_limit" > User - Limit(&euro;)</label ><input type = "text" name = "user_limit" id = "user_limit" size = "40" value = "' . $someVal . '" /></p>' : ''; ?>

Explanation:

// <?= is called short echo tag and it equals <?php echo
// ?: ternary operator is shorthand for if/else statement
failedCoder
  • 1,346
  • 1
  • 14
  • 38
1

Yes you can use an echo

<?php 
if ( $condition == true ) { 

    echo "<p>
            <label for='user_limit'> User - Limit(&euro;)</label>
            <input type='text' name='user_limit' id='user_limit' size='40' value='" 
                . inout($res['user_limit']) . "' />
          </p>";
}

NOTE: Also removed a lot of unnecessary spaces in your HTML to keep it neat and tidy

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • forgot to mention that this will be displayed always, because of the assignment within the `if`(assuming `condition` is a real condition/variable, but not a `const`) :) – mitkosoft Feb 05 '20 at 14:03
  • @mitkosoft Yea, I used the same Pseudo code as the OP provided :( Assuming they were aware of the fact that was not valid PHP, but as you mention it – RiggsFolly Feb 05 '20 at 14:28