A
A
alex1592014-07-03 15:18:20
PHP
alex159, 2014-07-03 15:18:20

How to improve the skills of building a web application (php)?

Hello! Today I was told that I have fundamental skills in building web applications, but they will not reach the junior level.
So I have a bad application structure, but how can I improve it?
It is clear that you need to practice, but how to do it if you don’t know if it’s right or again a low level of construction?
The most important question: how to develop your application building skills correctly?
Link to my squalor https://github.com/Bleef159/proVisit
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nowm, 2014-07-03
@alex159

Link to my squalor

The first and very important advice: if you have a desire to write: “A link to my squalor”, it’s better to delete all the code right away and don’t distract people over trifles. Look: you call it squalor. So you know why he's miserable. In this case, correct him to such an extent that you do not have the desire to call him miserable yourself. Corrected - publish. If you call him poor just because someone else calls him poor, then ask that person to review, and not everyone in a row.
Ask the person who told you this. Ask what can be improved and in what direction to work. It's just that a Senior PHP Programmer in one company may have even fewer skills than a Junior PHP Programmer in another company. If someone says that the code is shit, ask them to explain why it's bad. If people don't explain, just shut up and code for fun.
There are no clear criteria for dividing into Junior, Middle and Senior. But there are signs by which you can roughly navigate.
This will output something like this:
<html>
. . .
</html>
<!-- Код выполнен за 0.531 секунды -->

This is semantically wrong, as there shouldn't be anything else after the closing HTML tag.
2. Russian and English comments are mixed. In general, it is better to write comments in English, since you post the code on a resource where the majority of visitors are English-speaking (I'm talking about GitHub, not about the Toaster). At the same time, you can write comments in Russian without any problems if you are deeply parallel to the English-speaking audience. But in this situation, you need to write all comments in Russian. You have a hodgepodge there - English-language comments automatically generated by the framework and next to them comments in Russian. In your case, you need to either rewrite English comments into Russian, or translate Russian comments into English.
Additionally, you don't need to write comments for mundane stuff like:
This is the same as sticking a note on the monitor that the Earth revolves around the Sun. Do you personally need to constantly remind yourself of this fact? Perhaps it makes sense to recall some difficult moments, such as the distance between the Sun and the Earth in millimeters, but not ordinary things.
3. Non-uniform approach to code design. For example, the framework generated the following code:
if (defined('ENVIRONMENT'))
{
    switch (ENVIRONMENT)
    {
        case 'development':
            error_reporting(E_ALL);
        break;

        case 'testing':
        case 'production':
            error_reporting(0);
        break;

        default:
            exit('The application environment is not set correctly.');
    }
}

This is PSR-2
code. The code you write does not fall under this standard. For example:
if (strlen($uri)>1) {// если не главная страница...
  if (rtrim($uri,'/')!=$uri) {
    header("HTTP/1.1 301 Moved Permanently");
    header('Location: http://'.$_SERVER['SERVER_NAME'].str_replace($uri, rtrim($uri,'/'), $_SERVER['REQUEST_URI']));
    exit();
  }
}

I suggest that you determine for yourself what is not PSR-2 here. In general, I often score on PSR-2. But I write the way the main code is written. If the main code is written according to the PSR-2 standard, I also make my edits in this style. Interfering with standards, indenting two spaces in one place, four spaces in another, and one TAB in the third is also a sign of juniority. It is immediately clear that you wrote little and cannot understand that there should be uniformity in the code in order for it to look non-junior.
4. Bicycles are another sign of a beginner. Since you are trying to define the main page in this way (as in the listing above), you can shorten the code (about the "bicycle" explanation in paragraphs "c" and "d"):
a)Why add a single IF block to the IF block? Such nesting of blocks is needed only if you have several IF operations inside the first IF block, and not just one. For example:
if (x = 0) {
    if ( isset(y) ) {
        echo 'The Y is set';
    }

    if ( isset(z) ) {
        echo 'The Z is set';
    }

    echo "Echo something";
}

You need to write like this in your case:
if (strlen($uri) > 1 && rtrim($uri, '/') != $uri) {
    // some code
}

b) If you try to use a function call with the same parameters several times, and the function definitely returns the same result, it is better to assign its result to a variable once and use the variable. For example, "rtrim($uri,'/')" is called multiple times. It is quite clear that it will return the same result all the time. Of course, rtrim works fast and the difference will not be noticeable in the end. But, for example, if you have a function that makes a query to the database, and you pull it in this way every time, this will affect performance.
Take it as a rule: the result of the function must be assigned to a variable if this result is the same all the time and will be used in several places of this scope. On the other hand, if a variable is used only once in a given scope, there is no point in defining it - it is enough to simply call the code that returns the result in the right place. For example:
// Плохой подход
echo 'У меня есть ' . get_my_apples_from_database() . ' яблок, потому что ' . get_my_apples_from_database() . ' яблок — это круто';

// Хороший подход
$my_apples = get_my_apples_from_database();
echo "У меня есть $my_apples яблок, потому что $my_apples яблок — это круто";

// Плохой подход
$my_apples = get_my_apples_from_database();
echo "У меня есть $my_apples яблок";

// Хороший подход
echo 'У меня есть ' . get_my_apples_from_database() . ' яблок';

c) If you're using CodeIgniter, why not take advantage of its ability to detect the fact that the main page is currently open?
d) In general, redirects from a slash to a page without a slash should be done using the web server, not the PHP interpreter.
If you have Apache installed, you can add the following code to your .htaccess at the root of your website:
RewriteCond %{REQUEST_URI} ^.+
RewriteRule ^(.*)/$ /$1 [L,R=301]

This is for illustration purposes only. I don't guarantee that this construction will work perfectly, but as an example it works well.
CodeIgnitor is an MVC framework that helps you understand what MVC is and how to write MVC applications correctly. Lots of documentation on this topic. It is proposed to separate the code into model, controller and view. This means that each part performs its own functions. The model, for example, among other things, is engaged in the extraction of data from the database. Only the model should do this. SQL queries in a controller are... there's an expression: "fat stupid ugly controllers". This is the usual definition, nothing offensive. This is the name of controllers that make queries to the database and partially generate HTML code. They appear in a situation where the developer breaks to jump over the files and he starts to sculpt everything into the controller. Or when he doesn't have enough MVC knowledge to properly split code.
* * *
Here. You have a lot of these things (and a lot more things to criticize) in your code. They show that you have only recently started programming, you have a small "vocabulary", a lot of bicycles in the code, a lot of suboptimal approaches, like this:
$i = 0;

foreach($query->result_array() as $row)
{
  $myrow[$i]['url'] = $row['url'];
  $myrow[$i]['title'] = $row['title'];
  $myrow[$i]['text'] = substr(strip_tags($row['text']),0,255);
  $i++;
}

Instead, you could write like this:
$myrow[] = array();

foreach($query->result_array() as $row)
{
  $myrow[] = array(
    'url' => $row['url'],
    'title' => $row['title'],
    'text' => substr(strip_tags($row['text']),0,255)
  );
}

The $i variable is not needed here. You can get along just fine without it. But $myrow must be defined so as not to encounter unexpected errors if $query->result_array() returns 0 in a row.
Read different code. It is clear that this is universal advice, to which you will answer: "I know." But if you do not read, you will remain illiterate, regardless of whether you know about the usefulness of this advice or not.
If you write on CodeIgniter, read its sources and documentation. If you don’t read it, you will produce bicycles, instead of which you could use the framework API.
Are you using MVC? Read, for starters: en.wikipedia.org/wiki/Model-View-Controller There is also about fat controllers.
* * *
In general, a very subtle moment when the conversation comes about Junior, it means that there are both Middle and Senior somewhere nearby, which means you want to work in a specific place with specific people. It makes sense to pester these people, to become students. If you don't think so, send them three letters and program as you see fit.
DISCLAIMER: Just in case, I will clarify that I am not engaged in training or searching for other people's jambs in the code. I had some free time which I spent on the answer. This does not mean that I am ready to babysit someone. I can't do anything myself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question