J
J
jasonOk2015-10-11 12:12:06
PHP
jasonOk, 2015-10-11 12:12:06

How will it be correct, competently in php?

Let's say I have an input checkbox <input type="checkbox">and I, provided that the variable $a == 1, set it to "checked".
How do I understand this option?

<input type="checkbox" <?php if($a) echo "checked" ?> >
obviously wrong, right? Do you need to use template engines like twig to do it correctly ? Or have I misunderstood something?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Valery Ryaboshapko, 2015-10-11
@valerium

PHP itself works as a templating engine. That is, if this code is placed in a separate file and not mixed with logic, and the template remains readable, then it has a right to exist.
The main advantage of this approach is speed. The main disadvantage is a potential vulnerability, but it can be covered with proper skill.

O
OnYourLips, 2015-10-11
@OnYourLips

Do you need to use template engines like twig to do it correctly?
Yes, you are right here.

D
Dmitry, 2015-10-11
@mytmid

The option has the right to life, it is not advisable to use a template engine on all projects.
But it is worth making a small change, and write like this:
- if $a should be equal to 1, then this is how it should be written
- any instruction ends with a semicolon
- if the text does not imply the use of \n \t \r, etc., and you are not going to insert variables into this text: " Hello, {$name}", then single quotes should be used.

I
Ivanq, 2015-10-11
@Ivanq

It may not be entirely correct, but I write like this:
<input type="checkbox" <?$a?"checked":""?>>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question