Interesting

How to start programming in PHP from scratch (on your own). Why do many people fail? Personal experience ... trial and error

Good day!

Probably, each of us, having watched films about hackers and cool programmers, would like to understand at least a little in this matter. And I will note that no, no, and they throw similar questions to me (although I can program, but I am far from a professional programmer. And to people far from the IT sphere, this cannot be explained at all ...).

In general, now there are quite a few different training courses, books and articles on this topic on the Internet. However, when I myself tried (learning the language from them) from the first lines of code "hello world!" in PHP to move to something more serious and applicable in life - nothing worked! As if there is a certain gap or emptiness in these books: it is not clear how to go from the very basics and knowledge of some functions to something more ...

Over time, I managed to figure out PHP to the level of writing my own simple CMS (CMS is a website engine) and one article helped me in this (unfortunately that resource no longer exists on the network, however, I am very grateful to that author for this "kick" in the right direction).

Based on that article and my vision, I will sketch this note for those who really want to start programming in PHP, but they are failing. In general, I have my own very radical opinion on this view, so here you will not find advice on books and courses - just a look at what exactly to do based on your experience ...

*

Where to start programming (in PHP)

STEP 1 - hobby, desire to solve some problem

Perhaps the most important thing is to "catch fire" with some task (without this it is very difficult to study something new). For example, at one time I really needed (VERY!) So that a PHP script could read a certain text file, and cut some pieces out of it according to my template. On the one hand - everything seems to be simple, on the other - for a person who has never written 2 lines of code on his own, the task is extremely difficult!

I had to buy this first script ... But later I needed another similar one (and then another). And here, here, I got carried away with PHP ... By the way, after a few evenings such scripts were already able to write on my own, after tormenting for an hour or two ...

Well, in general, in general, I want to note that if there is no interest in programming, then quickly starting the study, as a rule, quickly and finish. And I'm afraid that no courses and teachers will fix it.

Until you start typing the code yourself and see what is "displayed" for you, what is in your variables, and how the data changes in them when using some functions, you will not be able to start programming (this is purely my opinion!). No amount of "smart" books will help you.

It's good to dream!

By the way, many beginners (including myself once) have one big mistake: they spend a lot of time preparing, downloading various courses and videos. Then they look at it all, study it, write it down in notebooks, etc. But they don't do the most important thing - they don't start typing the code themselves, and watch how it is executed ...

Okay, now closer to practice (finishing philosophy).

STEP 2 - installing Denwer, Open Server, Notepad ++, Total Commander tools. Start!

You don't have to buy hosting to write your first scripts and experiments. Much better to use a free local web server: Denwer or Open Server (I personally recommend Open Server, and this is where I'll show you how and what to do) ... By the way, if you chooseOpen Server - then the most minimal version is enough for a start (note: there are just several of them on the site, with different libraries).

For direct dialing of code (text) it is convenient to use special. notepad with syntax highlighting. I am impressed by Notepad ++ in this regard. Simple, lightweight, free and convenient notepad (significantly more features compared to the built-in Windows notepad).

Hello World! (Notepad ++)

Well, I would also recommend using some kind of commander. in the explorer it is very inconvenient to navigate through different folders, and open several files at once (you get lost and forget what is where). I would recommend choosing Total Commander (a very convenient and versatile commander).

First script: preparation!

1) After downloading and installing OpenServer, you should have a folder of the same name in the selected location on the disk - in my case it is "e: \ openserver5.2 \" ... This folder should contain a "domains" directory - in it you need to create a folder with the name of your site (for example, "my-site.ru" - example below).

Remember the name of this folder, it is this folder that will need to be typed in the address bar of the browser when writing scripts.

OpenServer - example of creating a "site" (local)

2) Next, create a file "index.php" in the "my-site.ru" directory and enter the following lines into it:

echo 'Hello World!' ;

Hello World! The first "program"!

Save the file.

Note: if someone is working with php for the first time, pay attention to the 'echo' command - it means to display everything after it in quotes.

3) Then start OpenServer (or restart it if you had it running). Also close and reopen your browsers. This is necessary in order for OpenServer to be able to change some files in your Windows: so that our created site "my-site.ru" will open in your browser.

OpenServer - starts a local web server

In general, if OpenServer was successfully launched, a green checkbox should be lit in your tray (if it is red or yellow, try restarting it again. In some cases, you need to run it as administrator. In any case, the red flag will not allow you to work further ...) .

OpenServer - up and running

4) Actually, it remains to type our " my-site.ru "(without quotes) and it will show the greeting from the generated file index.php (see screenshot below).

Works!

5) Please note that if in the first example we immediately displayed a line of text, then in the next example I slightly changed the code. As a rule, when developing scripts, you are dealing with variables , which can contain various data: strings, numbers, etc.

Let's create one variable ("$ p") and put our text in it. See example below:

$ p = 'Hello World!';

echo $ p;

First variable

As shown below, the result will be the same! Those. the browser will print our string as well. However, the code is somewhat different: now the text is contained in the "$ p" variable, which we output ...

Displaying data from a variable

6) Another illustrative example below. This time, I created two variables "$ p" and "$ p2", and then concatenated these lines, and put them in the variable "$ p3". Note that the lines are connected using a simple dot.

$ p = 'Hello World!';

$ p2 = 'It's me!';

$ p3 = $ p. $ p2;

echo $ p3;

Another example (two variables)

The result is predictable ...

Works...

I draw your attention to the fact that all examples are recommended to be driven in manually for faster understanding and mastering.

STEP 3 - breaking down the task into its components. Debugging each step

Perhaps this is the most important step to start programming, for the sake of it I started this article ...

No matter how difficult your task that you want to solve, it can be conditionally divided into three stages:

  1. obtaining initial data (data entry);
  2. data transformation;
  3. data output: to the screen, or just to a variable. (see the screenshot below: I conditionally divided one of the examples of the article into these three steps, so that it is clear what it is about)

What stages can the script be split into

As a rule, the most difficult are the stages receiving and transforming data ... And many beginners have no idea how you can write 100-line code without getting confused or mistaken in them.

In fact, after each line of code, you can look at what you have in the variables. And gradually, adding one line of code at a time, and looking at how the variables change, bring them to the desired form (and solve the problem!). For this, PHP has wonderful functions "var_dump ($ p);" and "print_r ($ p);".

And for beginners, I recommend using them all the time! Yes, just like that, programming is sometimes such a boring thing ...

I will consider an example below.

Just imagine, you don't know what you have in the "$ p3" variable (in the code, in the example below, you see the line that is loaded into it, and in theory it should be there. But in more complex real problems - this does not always happen, the variable may contain a complete mess, instead of the required line ...).

So, to see what is in the variable, you just insert the command "var_dump ($ p3);" after it (and save the file).

Var_dump example

As a result, in the browser (when refreshing the page), you will see that "string (31) ...." is displayed. Those. var_dump you were told the data type (string is a string) and the length of this string (31 is a character). This is very useful when debugging long programs (scripts)!

string (31)

As an example, before "var_dump ($ p3);" I set $ p3 to 1. The result is different (see below).

If you add a number to the variable

Int (1)

Example: reading a text file and clipping the first sentence

Everything that I wrote above, it would be good to explain with an example. As an example, I took a simple task: open a text file, find the first point in it (that is, the end of a sentence), and cut everything from the beginning of the file to this point.

On the one hand, it can seem difficult for someone who has never done PHP. On the other hand, by breaking the task into its components and using debugging (var_dump), you can write even more complex things ...

And so, it is logical to break this task into several stages:

  1. read a text file into a variable;
  2. find a point in a text file (i.e. find out its ordinal number, for example, the 35th character);
  3. cut off a piece of string: from the very first character to a point (up to its ordinal number).
  4. display the result on the screen.

1) Reading a file into a string

As a text file - I took the text of this article, and put it in the "text.txt" file, which I copied to the folder "e: \ openserver5.2 \ domains \ my-site.ru \" (I mean, put it next to "index.php").

Next, you need to find a function to solve the first component: reading the file into a variable. This is where "Google" helps a lot: a query like "PHP file reading" copes with its task and helps to find a function file_get_contents () - we use it!

By the way, in this regard, I recommend the //php.net directory (a great helper!).

$ p = file_get_contents ('text.txt'); // read the file into a variable

var_dump ($ p);

We read the file into a variable

As a result, if everything is entered correctly, we will see the contents of the "$ p" variable: it will contain a long text (line). If this is not the case for you, a mistake was made somewhere ...

string (1278)

In fact, this is our initial data. Now we need to find the first dot (".") In the article.

2) Finding a point symbol

In general, PHP has a lot of functions for working with strings. To find the first point in this example, I decided to take " strpos (); ". Below I do not give the full text of the script code, however, pay attention to the function itself and what variable I added to var_dump (the one in which the ordinal number of the point should appear, ie "$ t", and the variable "$ p" - we have already debugged in the first step, it contains the text).

$ t = strpos ($ p, '.');

where: $ p - the variable in which we are looking for the desired symbol;

'.' - the one we are looking for.

Point search

223 is a symbol, this is a point. We found her!

3) Cutting a line to a point

For trimming strings, PHP has a function like substr (). It is very simple to use it, I will show you with an example:

$ s = substr ($ p, 0, $ t);

Where:

$ p - the string from which we will cut;

0 - the ordinal character of the beginning (the beginning of the desired piece);

$ t is the ordinal end character (end of the desired chunk). In our example, this is the point we found.

As a result, the complete code will look like this:

$ p = file_get_contents ('text.txt'); // read the file into a variable

$ t = strpos ($ p, '.'); // look for the first point in the line $ p

$ s = substr ($ p, 0, $ t); // cut the string from the 0th character to the point

var_dump ($ s);

Cut off the line

Note that we also changed the variable in "var_dump ($ s);" ... Thus, we see that our string has become small, only 223 characters. We have practically solved the problem, there is not much left ...

Cut off!

4) The problem is solved!

In general, replacing "var_dump" to "echo" - we get a solved example. Now, when you open "my-site.ru" in the browser, the first sentence from the text file is displayed.

The script is ready!

Note that "echo" does not print data type details. You see just a line ...

The proposal was cut

STEP 4 - new challenge ...

As you can imagine, the task in the example above is just a drop in the ocean! Of course, it could be solved in one line and in more elegant ways (everything that was shown above is just an example to show the essence and how you can move to create a script with hundreds of lines and that solves much more complex problems).

Of course, you can search not only for a point, but also for sequences of certain symbols, pictures, tags, etc. You can read not only the file (i.e. get the initial data from it), but also the pages of other sites, read the user's current URL, get data from the form that the user fills in, etc.

Thus, by dividing one large task into several medium ones, and the medium ones into several small ones, and the small ones into lines of code and specific functions (each of which you will manually go through and debug), you can solve even very large problems. Yes, even if it is crooked and oblique at first, but the main thing is that you will do it yourself, and you will have an interest in reaching a certain level in learning.

Moreover, with each new line - your experience, speed of development, and skills will be pumped. And if so, perhaps I wrote these lines for a reason ...

So, set new tasks, and go ahead: look for functions step by step, change variables, solve small problems step by step. The main thing is not to get hung up on books and training courses - until you start doing it yourself, you will not learn ...

What they don't talk about in advertisements for books and courses

They usually promise to teach you programming in a week or a month (maximum 2), promise big salaries and high status. However, they completely forget to add a few important things, which I will say below ...

1) Heavy eye strain

Sitting all day and going through the code, editing it, etc. - this is far from the same as watching a movie all day, relaxing in an armchair. Do not believe me - try at least half a day to thoughtfully edit a Word or Excel document (roughly comparable).

2) The need to constantly learn

Nowadays in many professions you always have to learn, but programmers - ten times more! In general, in general, the IT sphere strides every year by leaps and bounds, just keep up with it (remember, 10 years ago, the Internet began to appear only in cities, and now even in the villages they talk about bitcoins ...) ...

3) Sedentary work

If in many areas, you can spend part of your working day somewhere "walking", then the programmer's workplace is only at the PC, and nowhere else ... Such a way of life, nevertheless, over time begins to affect health: the back, hands, shoulders, eyes ... - by the end of the day they start to get very tired.

4) Salaries of ordinary programmers are not always high

Until you gain experience, most likely, you will not receive much more relative to other areas of activity. In my opinion, the era of too high salaries in this area is passing (passed?). However, the work of good specialists in this area is well paid!

5) It's boring to solve other people's problems ...

If you are interested in programming and "translating" your idea into reality, this does not mean at all that it will be interesting to solve other people's problems - this is not at all the same! Sometimes, digging into the code becomes so boring and gray ... that even a "howl" (I think not everyone will understand these lines).

PS: Should you quit your current job and try to become a programmer?

A very popular question ... Especially in the wake of very high salaries of some categories of programmers.

Quitting everything at once is definitely not (at least, my point of view). Whatever it is, and whatever you decide, to exercise effectively, load the brain and learn something new - a person is capable of no more than 2-2.5 hours a day (this is really so, you can check for yourself - even on a weekend can do more). So, to start learning your new craft - it is quite possible to carve out an hour in the morning or in the evening at the end of the working day, somewhere to "steal" part of the day off.

If you succeed, you will gain some experience, etc.- you can try to take a vacation and get a job as an intern (maybe half-rate), see if you like everything and work out ...

Also, it will not be superfluous to have a financial "pillow" for a rainy day, otherwise you never know, suddenly a new job does not meet expectations. At least this approach will not drive you into a difficult life situation if something goes wrong ...

*

Well, on this I finish my "manual". If you have any additions on the topic or criticism, they are welcome (comments are open).

All the best!

$config[zx-auto] not found$config[zx-overlay] not found