S
S
Senseich2018-03-06 19:56:10
PHP
Senseich, 2018-03-06 19:56:10

How to write php code correctly and beautifully?

Today I touched upon the issue of cleanliness and literacy of the code, meaning its readability. For example:

if (isset($apples)) {
  echo $apples;
}
else {echo "Переменная удалена";}


Or this is better:

if (isset($apples)) {

  echo $apples;
}
else {

echo "Переменная удалена";
}

This is just a spontaneous example to make the question clearer. So that other progers do not spit at the sight of my code))
Advise, maybe some rules or good articles about it!

Answer the question

In order to leave comments, you need to log in

5 answer(s)
D
DieZz, 2018-03-06
@DieZz

Everything has long been invented https://www.php-fig.org/psr/psr-2/

N
neol, 2018-03-06
@neol

There are tons of manuals for different projects besides the PSR-2 mentioned here:
https://make.wordpress.org/core/handbook/best-prac...
https://symfony.com/doc/current/contributing/code/ ...
https://framework.zend.com/manual/2.4/en/ref/codin...
https://www.drupal.org/docs/develop/standards/codi...
Plus in each team can be your own rules.
So the style of code design depends on which framework or CMS you use or in which team you work.
If you are writing something completely independent, then PSR-2 is a good choice, but using it in a project with its own established rules can be a strange decision.
PS I will add that there are tools for automatically correcting the design that are not tied to the IDE (for examplecs.sensiolabs.org ), so you can write whatever you want and then customize everything to some standard.

M
Maksim Fedorov, 2018-03-06
@Maksclub

There is such a way to write, starting with PHP 7:
php.net/manual/ru/language.operators.comparison.ph...

<?php
echo $apples ?? "Переменная удалена";

There is a wonderful solution - install it and run it through it after writing the code, it will swear at you for spaces, indents and everything. You can even put a hook for the git and when you commit, it will check your code, and if there are jambs, it will not accept the commit

A
asd111, 2018-03-06
@asd111

For php there is a PSR standard.
You can install php storm and it will align you properly in PSR using CTRL + L

W
w0key, 2018-03-06
@w0key

if (isset($apples)) {
   echo $apples;
} else {
   echo "Переменная удалена";
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question