Learning php from scratch. Adding PHP code to markup and the result of the hypertext preprocessor

Language PHP programming is definitely the most popular web development language today. Has both a huge army of admirers and enough large number ill-wishers (especially representatives of “serious” programming languages ​​such as Java, Python or Perl). One way or another, with its advent, the process of creating dynamic websites has ceased to be the preserve of the “chosen few”. This compass provides basic information about PHP for beginners.

What is PHP? Brief information.

For starters. Large Internet projects developed using PHP

For those who believe that c using PHP nothing serious can be done. It is worth noting, however, that large projects tend to use several different languages programming.

History of origin.

First steps. What to do?

You have decided to start learning PHP, where to start? First, read the guide “the very basics” and “I want to learn PHP and Mysql. Where to start? Then install PHP on your local computer. The PHP distribution can be downloaded from the official website (the introductory articles describe the process step-by-step installation), or, more simply, install Denver. Gentleman's Web Developer's Kit(“D.n.w.r”, read “Denver”) - a project by Dmitry Koterov, a set of distributions (Apache, PHP, MySQL, Perl, etc.) and shell, used by Web developers to develop websites on a “home” (local) Windows machine without the need to access the Internet. Main feature Denver - convenience when working remotely on several independent projects at once and the ability to be placed on a Flash drive. This package is very widely known in Russia; according to some data, 80% of PHP developers use it. The website has detailed instructions for installing and configuring this distribution.

The simplest PHP program



Example

echo "Hi, I'm a PHP script!";
?>




How to edit?

Let's study further.

Useful resources for further immersion in the topic. Lessons, articles, code examples.

From the author: Is it necessary to know the PHP programming language? If you are going to throw in your lot with website building, you will have to. Even if you really don’t want to! Why is this so? We will try to answer all the common “whys” associated with this language in our material.

The solution that “holds” the Internet together

This is exactly how one of my acquaintances, who has been creating Internet resources for more than a decade, described PHP. And he’s right, probably 200% percent (or maybe more).

The history itself of this language suggests that he was born to “blind” the current version of the Network to which we are so accustomed. This is probably why some people still cannot realize its power, even after learning the basics of PHP programming.

Proving the role of PHP in creating the Internet is a thankless task. Here, as they say, supporters of other server-side languages ​​can begin to speak out. They say that the “hypertext preprocessor” (as the abbreviation PHP stands for) has already outlived its usefulness. Many of its functions are implemented even in…

Well, shouting and waving your arms, extolling your programming language, which you specialize in, is a simple matter. But the best “evidence” indicating the demand for PHP is its prevalence.

Next rating program languages, compiled by TIOBE specialists last summer, tells us that PHP already has been consistently ranked among the top for several years. Haven't you started looking for a tutorial on the PHP programming language yet? You'll be running soon.

If you are wondering why Java has jumped so much, then the answer is: thanks to the Android mobile operating system, applications for which are developed in this language.

Own rake

Back in the early 2000s, I was engaged in “research” in the field of programming. I kept trying to figure out which “party” of developers I should join, which discipline I should devote my life to studying.

In principle, I did not consider PHP as a programming language for beginners. He seemed too confused. Here's the other thing about ASP.NET: a commercial language based on C#. Oh, what castles in the air I built back then! And there were all the prerequisites for this:

The technology was developed by such a “thought giant” as Microsoft.

Availability of specialized software - a powerful editor Visual Studio.

Extensive and well described documentation.

Hope for a “healthy” future.

ASP.NET, unlike PHP, was much easier for me. Visual Studio has automated a large number of server-side scripting operations. For example, thanks to the excellent visual editor it only took a few minutes to create a web page. By dragging and dropping controls and styling them in the editor, you could make a website in less than an hour!

But the PHP programming language from scratch didn’t work out for me right away. Honestly, I tried to make friends with him (I had to pass the exam somehow). But when studying its basics, it seemed callous and even cold to me.

In general, I took up ASP.NET. I created my first website. Well, everything seems to be working in the development environment. Cross-browser compatibility checked in several popular browsers. I think it’s time to “fight.” So to speak, show your “brainchild” to the whole country (at least). This is where the “rakes” turned out to be.

My attempts to find hosting with ASP.NET support on the RuNet were never successful. There were, of course, several paid platforms, but the prices for hosting a website on them were clearly not “student” prices.

Somehow I dug up in Burzhunet free option hosting (by the way, sponsored by Microsoft). Well, I clicked, enjoyed the breadth of my thoughts, but things didn’t go any further. And all due to the fact that most of the sites were already made in PHP.

Once you decide and understand exactly what you are ready to study PHP language programming, look for a tutorial for beginners “to suit your needs.” The main thing is that you understand what the author of the publication wants to convey to his readers. Immediately get ready to pay a good amount of money for quality literature.

But it’s better to take an integrated approach - add a smart one too. This will allow you to hone your skills immediately practical examples. And my advice to you is to do less copy-pasting code! I’ll explain why now.

When you type example scripts yourself, you will quickly get used to it and remember the features PHP syntax. As for literature, choose a tutorial with the basics of programming in PHP. And it is advisable that the author post the code of all the examples given on his resource on the Internet.

Also, immediately before starting training, download and install Open Server. This software package includes all the environments you need to write and test code.

Well, let me take my leave for today. Forgive (if anything) for the excessive overview of the content of the article, but this is necessary to understand the features of this language (after all, it is server-based). I wish you to quickly grasp the basics of the PHP programming language and start real coding!

Today, many people talk about functional programming, but not everyone can say with confidence that they followed all the principles of this method of writing programs. The main reason This is due to our one-pointed thinking and ordinary ignorance. In this article we will look at the basic principles of functional programming in PHP.

Important aspects of functional programming

Let's start with the terms. Wikipedia defines functional programming as “a programming paradigm in which the process of computation is treated as the calculation of the values ​​of functions in the mathematical sense of the latter.” IN this method In programming, the main object of manipulation is functions, while in imperative programming, the main principle is the sequential execution of commands to achieve the desired result.

When I said that the main objects of manipulation are functions, I meant that we can do anything with them: pass them as arguments to other functions, define functions in functions, use functions as return values! In general, there are only functions all around.

Now let's get closer to some of the features of functional programming.

Immutability

Values ​​that cannot be changed have the property of immutability. In PHP and many other languages ​​they are called constants.

Recursion

Recursion is one of the commonly used techniques in functional programming. With an imperative approach, to scroll through arrays we can use for loops or foreach, writing the current value to a temporary variable. Due to the principle of “immutability”, this approach is not suitable for functional programming.

Let's say we want to get the sum of the elements of an array (forget about array_sum()). With a functional approach, the solution to this problem will look like this:

If the array is empty, then the sum of the elements will be 0. If the array has more elements, we use recursion.

Pure functions

If a function does not in any way change the values ​​of variables that are outside its boundaries and does not perform actions to input/output data into a file, database, etc., then such a function can be called pure.

The return value of pure functions will always be the same for the same set of input parameters. In this case, we don’t have to worry about the performance of the program as a whole if we replace the call to a pure function with a calculated value. All math functions are pure functions, while date functions and rand() are not.

Higher order functions

While pure functions can be implemented with almost any approach, higher-order functions are a very distinctive feature of functional programming. A special feature of higher-order functions is that they can take other functions as arguments and return them. In PHP these are lambda functions and closures.

Lambda functions

Lambda functions (also known as anonymous functions) are nothing more than functions without a name. Work with such functions is carried out through the variable to which this function was assigned.

The ability to create similar functions is available in many languages. You've probably already written them in your JavaScript code when you implemented callback functions when working with ajax.

$("#myButton").click(function () ( // do something ));

The above code fragment is nothing more than creating an anonymous function.

In PHP, lambda functions appeared in version 5.3:

When it comes to anonymous functions, it is very important to understand the scope of variables. For example, in JavaScript, anonymous functions have access to variables outside of them, but in PHP they do not.

Closures

Sometimes there is still a need to have access to some variable that is outside the anonymous function, but is not an input parameter. To access such variables, you can use closures using the use keyword:

In this case, we do not pass the variable as an input parameter to the function, but access it using use.

Currying and partial application

Partial functions are functions containing other functions that operate on a set of parent arguments.

In PHP, we can create functions like this using closures. Below you can find an example of calculating the volume of a box. All arguments are optional. However, if you pass less than 3 parameters, then another internal function will be launched.

In the first check we check the number of arguments. If it is equal to 3, then we return the result. If the initial number of arguments is less, we call another function that tries to find a solution with the given data.

Thus, we can calculate the size of the box by passing one argument first, and then others.

Currying is a special type of partial application function where each argument can be taken into a separate function. Something like f(x,y,z) to f(x)(y)(z). This syntax is not possible in PHP, but the principle can be implemented.

Pros and cons

There are many benefits to using a functional approach. For example, anonymous functions can be used as callback functions. Like in the Slim framework:

get("/home", function () ( // show home page ));

IN in this example, the anonymous function will run when the user visits home.

With a functional approach, you need to write functions that perform some narrow-profile task that does not affect any global things. Following this paradigm, your programs will not bloat.

However, it is worth noting that not all techniques of this paradigm can be implemented in PHP. An example is working with parallel processes.

It is also not always possible to determine the complexity of a recursive function and its effect on code performance. Sometimes it is worth moving away from the functional principle.

Perhaps the biggest disadvantage of the functional approach is that you need to abandon imperative thinking. If you do this, then a new approach to programming can even be exciting.

Bottom line

Functional programming is more than just an approach. It is more a way of thinking, regardless of language.

Bootstrap framework: fast adaptive layout

Step-by-step video course on the basics of adaptive layout in the Bootstrap framework.

Learn to typesetting simply, quickly and efficiently using a powerful and practical tool.

Layout to order and get paid.

Free course "Site on WordPress"

Do you want to master the WordPress CMS?

Get lessons on website design and layout on WordPress.

Learn to work with themes and cut layouts.

Free video course on drawing a website design, layout and installation on CMS WordPress!

*Mouse over to pause scrolling.

Back Forward

PHP Fundamentals: An Overview for Beginners

PHP has been consistently on the list of the most popular programming and web development languages ​​for many years. Of course, there are other languages, but the ubiquity of sites based on the WordPress engine has largely served as an additional impetus for an even greater increase in the popularity of this language.

What is PHP?

PHP stands for Hypertext PreProcessor(something like an "HTML preprocessor").

What does this mean? Let's start a little from afar: there are two types of languages. One type is called "client", and the other - "server".

This means that client languages ​​work in each individual's browser. A typical representative of client languages ​​is JavaScript, which you have probably heard about and the results of which you have seen more than once.

If you want to learn more about JavaScript, then check out the material.

All actions and commands that we specify, say, in the JavaScript language, are performed by the browser, which means that the same code written by us is processed in one case by Internet Explorer, in another by Firefox, in a third by Opera, in fourth - Google Chrome, i.e. the browser that each specific person uses to view our page.

The browser thus has an alternative name - client.

In the case of server-side languages(to which PHP belongs) we see a different picture.

Our website is always located on some server, i.e. a powerful computer specifically designed to accommodate many people's websites.

All commands and scripts written in PHP are executed on the server, and nothing else. After the PHP script is executed on the server, the server “gives” the result of its work, which we see in the browser.

It is important to understand the following point: by the source code of the web page, which can be viewed in any browser through an option like "Page source code" it is impossible to determine whether PHP was used to create a given page or not.

It is impossible to do this precisely because PHP scripts are processed on the server, and a ready-made, processed version is transferred to the browser. Basically just HTML code.

The difference compared to regular static HTML pages is one additional code processing step.

In the case of an HTML page, there is only one step: The browser processes the HTML code, i.e. page layout in accordance with certain rules, as a result of which we see the web page in its normal form.

In the case of a PHP page, there are two stages: first the so-called PHP interpreter(handler) executes the PHP code (as a result, a simple HTML code is obtained), and after that the browser processes the result of this processing, i.e., in fact, the same stage is performed, which is unique in the case of HTML page.

In general, PHP works great when paired with HTML. Moreover, you can insert PHP code into HTML code, and use PHP to output HTML markup. It's important to remember this simple point: It doesn't matter how complex your PHP code is, it will eventually end up as plain HTML.

Why use PHP?

HTML is 100% static. By embedding PHP code into our pages, we can ensure that the content of the same page was different depending on certain conditions(dynamic pages). Over the many years of its existence, the PHP language has established itself as an excellent solution for creating dynamic websites.

Is PHP similar to other languages?

Yes. PHP is similar to ASP.NET, Perl, JavaScript, C#. You may not know any of them now, but learning PHP will give you more confidence in mastering other languages ​​in the future.

What do you need to get started?

To fully work with PHP on your computer you need the following things:

1. Apache web server (it is used in most cases);
2. Database Management System (DBMS) MySQL (the content of the site is stored in the database);
3. Installed PHP interpreter;
4. Text editor in which you will write code;
5. Browser.

Now a little more about the first three points.

1. Web server is designed to simulate on your computer the very server on which your website will then be hosted on Internet hosting. This is necessary so that you can write any PHP scripts on your computer and watch how they work, make changes and edits to them. In a word, this is necessary for so-called debugging.

2. MySQL DBMS needed to store information that will be on your website. In the case of HTML pages, all the content of the site is located directly in them. Each page contains a certain amount of information (content).

When using PHP, a database is usually used to store useful website content. In the vast majority of cases this is MySQL.

3. PHP interpreter is a kind of program that processes PHP code on a web server. Without it, we will not be able to execute our PHP scripts and see the result of their work.

How to install all these components on your computer?

There is a good solution that greatly simplifies this process and does not require you to have any knowledge in setting up a web server, MySQL and PHP interpreter.

This is a special set Denwer, which already includes all three components. It is installed on your computer as a regular program and is ready to work without any preliminary settings.

Denwer is an ideal solution in the vast majority of cases, and for beginners it will be a lifesaver, as it allows you to start developing websites in PHP without having to study a bunch of additional information on setting up a web server, MySQL DBMS and PHP interpreter.

Basics

In order to tell the server to process PHP code, you must use the following syntax when adding PHP to an HTML document:

Opening a block of PHP code is indicated as ", and closing - "?>" . Now let's change our code as follows:

Please note that in this example we wrote everything in one line. Spaces and line breaks do not play a role here and will not affect the final result.

In the example we give the server the command echo(command for displaying information on the screen) and indicate that we want to display the phrase This is PHP in action. Each command in PHP is separated from the previous one by a semicolon at the end of that command.

PHP is more strict about strict adherence to syntax and will not forgive you for the absence of a semicolon, brackets, quotes, etc., as might be the case with HTML. PHP in this case will display a message about syntax error, which needs to be corrected and will indicate the line where this error was made.

By the way, in in this case we might not have used a semicolon at the end of the command, since it is the only one we have in this case (command). However, it is always better to stick good practices when working with code.

Declaring Variables

Variable in PHP- this is a kind of container that can contain certain information. In order to create such a “container”, we need to name it and indicate what should “lie” in it. This is done using a sign "$" , which means we are dealing with a variable. Let's put it in a variable named test phrase This is PHP in action.

The result of processing this code will be exactly the same as in the previous example. The phrase will simply be displayed on the screen This is PHP in action. However, before this we directly output this phrase, but now we have added this phrase to a variable test, after which they gave the command to display the value of the variable on the screen test.

How can we add some more text or other information to display on the screen?

Let's look at this with the following example:

As you can see, after outputting the variable test there is a space, then a period, then a space again. After this, quotes open, first there is one space, and then a sentence My name is Dmitry Naumenko., followed by closing quotes.

Let's go through the steps and see what is needed here and why.

After outputting the variable test we seem to “add” additional text to the existing output. This is done using the dot symbol (.) .

The dot symbol means in PHP addition , but not addition of numbers, but addition precisely text information, like the one we work with. After the period, we indicate in quotation marks what exactly we want to add to the conclusion, and write a new sentence.

Note that there is a space after the opening quotes. When displayed on the screen, it will be saved, so our phrases will not merge. Those. we will get you output:
This is PHP in action. My name is Dmitry Naumenko.

If you remove that space, you get:
This is PHP in action. My name is Dmitry Naumenko.

I would also like to draw your attention to the spaces on both sides of the addition point. These spaces do not play any role and are used only for clarity and ease of perception. The following code will give us exactly the same result:

Therefore, write in the way that is most convenient for you in this case.

Inserting comments in code

First question - "What are they even for?"

Comments are needed so that you can make notes, notes, explanations, etc. in your or someone else’s code. while working with the code. Now it seems to you that everything is clear and obvious. As long as you remember perfectly well why this variable is needed and what that function does.

Not much time will pass and without comments you will hardly be able to understand even your own code, not to mention other people's scripts. That's why proper use comments are an urgent need.

Typically used in PHP two types of comments:

Of course, comments only work within blocks of PHP code And ?> .

Outputting HTML using PHP

As I mentioned earlier, PHP and HTML work great together. Just because we're inside a block of PHP code doesn't mean we can't display text in a paragraph or in bold.

This is the text in bold."; ?>

As you may have guessed, the result of this code will be the output of this line in bold to the browser.

Create the first function

First of all, a few words about what a function is.

Function- this is a kind of mini-program that performs a certain useful work and gives us the result.

It makes sense to use functions in cases where you need to perform similar, template actions. In this case, we write such a mini-program, which significantly reduces the amount of code and, accordingly, our efforts.

The syntax when creating a function is as follows:

Say, if we want to write a function that calculates the sum of the numbers 10 and 5, we can do this:

We create a function called sum and in its body we indicate that it should display the sum of the numbers 10 and 5. After this, we call the function sum. Calling a function essentially means executing it.

In this example we do not use any arguments (see function syntax above). What are arguments and what are they needed for, we will look directly at the example. Let's modify the code that we have, making it more flexible and functional:

Now when creating a function sum we indicate two arguments in parentheses, separated by commas - $slagaemoe1 And $slagaemoe2. In the body of the function, we add and display not specific numbers, but the values ​​of variables $slagaemoe1 And $slagaemoe2(i.e. these same arguments).

Now we just need to call the function, passing it two arguments in parentheses that it “expects”. We specify the numbers 10 and 5, resulting in 15. By passing the function sum other numbers, of course, we will get a different answer.

I hope that this review helped you understand what PHP is and what it is needed for, what advantages it has over static HTML, and how you can use some of its simplest features.

Dmitry Naumenko.

P.S. Do you want to move further in mastering PHP? Pay attention to premium lessons on various aspects of website building, including PHP programming, as well as free course to create your own CMS system in PHP from scratch. All this will help you master this faster and easier. powerful language web development:

Did you like the material and want to thank me?
Just share with your friends and colleagues!