Answer the question
In order to leave comments, you need to log in
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
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.
Do you need to use template engines like twig to do it correctly?Yes, you are right here.
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question