⬆️ ⬇️

Learn PHP programming. Introductory





The main task of programming is to force the computer to perform certain and necessary for the programmer actions. Therefore, our first program will look like this:



<? php

$ me = 'Programmer';

makeHappy ($ me);


')

Let us consider in more detail what is written in this program. Experienced programmers are advised to read the code like a book, trying to understand the meaning of what is written based on the meaning of the words and symbols that come across. Well, use logic, of course.



For those who already know what this code does, the article contains harlots and the spirit of a black and white camel for you.







We will follow this advice and analyze the code, trying to understand its meaning.



The first line - <?php - tells us that the code in PHP will continue. All of our programs written in php, we will begin without fail with this line. Otherwise, PHP will simply pass by, without even knowing what an important task we are trying to impose on it.



Let's go further.

Second line:

$ me = 'Programmer';




The first word that we meet in this line is the word “me”, on the basis of which we conclude that the conversation here is about the creator of this program. A dollar in front of a word means that a given sequence of characters acquires its meaning as a variable.



What is a variable? A variable is the place where our data is stored, and for ease of reference, we give the name of this part of the memory.



If we have a dedicated memory, then it's silly to leave it empty, right? Therefore, with the next "=" character, we tell the computer that in this place we just created we need to write data, in fact assigning some value to the variable. Therefore, this symbol is not called “equal”, as we are used to in mathematics, but “assignment operator”.



Before proceeding, I wanted to say a few words about the vocabulary used by programmers. People are not dedicated, the world of programmers seems strange. After all, we do not need to make a factory of bridges, and put the thongs on the spell. But, like everything in programming, the vocabulary is subject to logic, and has a well-defined semantic meaning. And reading PHP programs out loud can invoke the spirit of the Black and White Camel, which will say something about the template engine.



As you guessed, we’ll talk about the word “operator”, which is quite familiar to the ear. Most often, the word "operator" is used next to the names of vehicles that require certain knowledge and skills to work with it. In fact, the operator is someone who performs certain actions, following the tasks set by the authorities. But there are no living people in the program who can do the work for us, and the programmer is the only and permanent boss. Therefore, when we talk about “operators” in a program, we mean something that does certain actions for us. These are the simplest orders or instructions that the computer will execute exactly as described in the documentation. Well, or not, if offended, tired and wants ice cream.



Therefore, for programmers, “+” is not a “plus”, but an “addition operator”, “-” means “subtraction operator”, “*” means “multiplication operator”, and “\” is a division operator. Of course, this list of operators is far from complete, but the meaning remains the same - “operator” is something that performs certain actions (for programmers it is called “performs an operation”) on the source data, which are called operands.



Speaking programming language -

$ a + $ b


Means an addition operator that performs an addition operation on the operand variable $ a and operand variable $ b. Well, or "a plus b", if you are not in the programming exam.



If the operator works not with two, but with one operand, it is called "unary" from the Spanish word "uno" - one. An old friend from mathematics minus is an example of such an operator performing the operation of negating its operand. If $ a is positive, then $ a is negative, modulo $ a.



Like mathematical equations, we can combine operators together.

For example,

<? php

$ a = 1;

$ b = 2;

$ c = - ($ a * 2 + $ b);




What does $ c end up with?



With the line "$ c = - (2 * $ a + $ b)" we tell the computer to assign the variable $ c "the negation of the sum 2 to be multiplied by $ a and $ b". Since the two lines above assign the variable $ a a value of 1, and the variable $ b 2, the calculation of the result is reduced to the solution of a simple example - (2 * 1 + 2), the result of which is "-4"



The answer is -4.



But in our program a variable is assigned not a number, but a sequence of letters in quotes. This turns a variable into a “string type variable”, or simply a “string”. A sequence of letters in quotes means exactly what you see - a sequence of letters. Quotation marks are used to indicate the boundary of this sequence. Sometimes the content of the line is absolutely meaningless, but a good programmer has no meaningless places in the program, so the line

$ me = 'Programmer', we are talking about ourselves, appropriating the string “Programmer”, and from this moment we consider ourselves a programmer (specifically in this program).



With the power of the code, you declared yourself a programmer, and all that's left is to finish the thought. For this is the symbol ";". This symbol terminates the statement, indicating that no more actions will be performed on this statement.



For example, we know well what will be in the $ c variable after executing the code



<? php

$ a = 1;

$ b = 2;

$ c = - ($ a * 2 + $ b);




but what if we remove the brackets and put a semicolon in another place? So to speak:

<? php

$ a = 1;

$ b = 2;

$ c = - $ a * 2; + $ b;




Now the assignment operator for $ c ends not after adding $ b, but before it, and therefore, summation with $ b does not occur, because operator ends with - $ a * 2. Now the value of $ c is the negation of the result of multiplying $ a by 2.



Answer: -2;



Having completed the assignment of a programmer title, we move on to the next line.



makeHappy ($ me);




We already know about the operators, and their ability to unconditionally fulfill our orders. But what to do when you want to perform an action that goes beyond the capabilities of the operator? Here function comes to the rescue. If the variable gives the name of the memory area, then the function is the name for the sequence of actions. When we need to perform these actions on data, we need to call a function.



To call the function you need, dressed in a black cloak with a hood, read a Latin textbook on a full moon, standing westward inside a circle drawn with the blood of old harlots.



Well, or just write the name of the function, in brackets separated by commas, listing the data with which this function will work.



And if the effectiveness of the first method is questionable, the second method always works. And back - if we see the name, after which the enumeration of variables, other data or just nothing goes in brackets, then most likely we have a function call.



When passed to a function, the data becomes the “arguments” of the function. We can also use operators as arguments.



For example, func ($ a) calls a function for variable argument $ a, while func ($ a + $ b) calls a function, taking as its argument the result of adding $ a and $ b.



How to guess what the function does? Need to look at her name. Good programmers always use verbs in function names, meaning actions performed on arguments. Nouns standing next to say that they have been chosen for impact, and adjectives reveal additional features of nouns.



Difficult, right? So just read what is written. If it says " printReport ", then most likely the function prints the report, and if it says " eatCat ", then the function will send your cat there. Or eat your cat.



Returning to our code -



makeHappy ($ me)




Judging by the name, it makes you happy, and judging by the list of operands, it makes you happy the programmer who created it. This program is a universal personal happiness generator. At this point I finish the story about programming - because now you have a universal way to bring happiness to anyone. To the customer, the boss and myself, of course. Everything. Curtain, turn off the switch and diverge.



What, what forgive? The function is not defined. Oh, sorry, I forgot a little detail.



Before the computer brings you happiness, you must teach him how to do it. How? With magic, of course! The magic of programming - so we wear black cloaks, take the keyboard, the Latin textbook and go ahead - to create personal happiness generators. And enough for today is the full moon, and I have an interesting evening planned in the company of old harlots.

Source: https://habr.com/ru/post/188136/



All Articles