proper inline variable concatenation in PHP

This shortpost will be about good practice concetanation of variables in PHP and using PHP in HTML inline. My coding style is based on Zend.

Concatening variables

What you often see is are lines like this:

PHP is able to handle such, but it’s not the proper way of doing it. Not only for readability reason, also because member variables, functions and special characters behave differently inbetween quotations. ALWAYS put array keys into single quotations. Have a look at the following snippet:

Will the output be 111, 999 or null? Always put strings into quotations, associative array keys are strings!

Let me introduce how I suggest to concat variables and array fields:

Inline PHP in HTML

true code

Shortopen tags are never used because on most servers they are Off by default.
Don’t put too much logic inside HTML anyways. See MVC pattern.



Leave a Reply

Your email address will not be published. Required fields are marked *