What type of programming language is php. PHP - What is PHP. PHP can store user information

1 year ago | 98.5K

Hello everyone, my name is Sergey Nikonov, for those who do not know me, I will tell you a little about myself. My experience in developing websites and web applications is more than 10 years and in this video I will try to answer one of the most frequently asked questions - Which programming language should I learn first?

Watch the video The first programming language. Which one to choose in 2018?

By itself, a programming language is just a tool by which this or that goal is achieved, and all object-oriented programming languages ​​have common properties, such as variables, arrays, functions, methods, classes, interfaces, and other properties.

And when you learn how to use these properties at least at an intermediate level in one programming language, you can very easily get used to any other programming language.

But I also want to emphasize that do not try to learn all programming languages ​​​​at once, as some students tried a little, for example, write in Java, after a couple of days they switched to Python, then to . As a result, with this approach, they did not master a single language, since there is a lot of information that students need to learn in their heads, and at the same time they lose motivation to study programming at all.

Therefore, until you learn at least an intermediate level of one programming language, do not switch to another.

What programming language to choose as the first?

I recommend choosing PHP as it is very simple, has a huge community, and a large number of jobs around the world if you want to learn programming in order to change your current profession.

Someone may object, say that PHP is slow, bad code is written on it, etc. but one notable example is the Facebook site. It is written in PHP and the Facebook site is the 2nd most visited site in the world among millions of websites. By the way, social network vkontakte, also written in PHP.

And as for bad code, in PHP, this is possible, since the language itself gives a lot of freedom of action and if PHP is not properly learned, your code will be confusing and the site will be slow. About how to learn PHP on your own and in what order, I will tell in one of the following videos.

Quite often I get asked the question:

First of all, you need to understand that HTML is a markup language, not a programming language, and refers to the layout of html pages. With the help, you explain to the browser how your page should look like, what size and color the blocks on the site should be.

How to start programming in PHP?

Programming in PHP is very easy and all you need is

"SEVERAGE SCHOOL №5"

Research work on the topic:

"Web programming language - PHP"

Done: student

9 "A" class

Ryabov Artem

Teacher: Abdrazakova A.K.

INTRODUCTION………………………………………………………………………………….…...3

Chapter 1. Introduction to PHP and MySQL………………………..……………..…….………….5

1.1. What is PHP?.…………………………………………………………………………5

1.2. PHP Capabilities……………………………………………………………………….5

1.3. Short story PHP……….…...…………………….……………………...…......6

1.4. Why choose PHP ………………………………………………………7

1.5. What MySQL ……………………………………………………………………..7

1.6. MySQL Features…………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………

Chapter 2 Examples PHP usage……………………………………………………...9

2.1. Working with Forms………………………………………………………………………..9

2.2. Working with MySQL (storing data in a database) ...…………………………………………………………………………………………………………………………………………………………………………………………………………………….

2.3. Working with MySQL (retrieving data from the database).

CONCLUSION………………………………………………………………………..……….15

BIBLIOGRAPHICAL LIST …………………………………………………...…………………………………………………………………………………………16

APPENDICES………………………………………………………………………………...17

Introduction

PHP is a programming language used on the WEB server side to dynamically generate HTML pages. This is evidenced by the decoding of its name: PHP - Personal HyperText Processor.

PHP is one of the few programming languages ​​created specifically for developing web applications. Therefore, it includes all the features required specifically for running on a web server, and at the same time it does not have the redundancy inherent in many of its competitors.

A very nice feature of PHP is that its commands are included in normal HTML pages using special tags, which make the PHP machine perform the necessary actions on the server. PHP programs do not need special CGI directories with special permissions. Moreover, on one page, you can arbitrarily alternate "plain" HTML and PHP code.

PHP is platform independent. PHP integrates perfectly into all popular web servers: Apacce and IIS, Zens and Netscape Enterprise Server, runs under Windows and OS / 2, MacOS and almost all UNIX-like systems. As a result, PHP works for almost all hosts that allow their own executable scripts.

A remarkable feature of PHP is its integration with almost all modern Internet technologies. PHP supports most modern web protocols: IMAP, FTP, POP, XML, SNMP, and more. PHP works great with databases. It is difficult to find a DBMS that is not supported by PHP. MySQL and MS SQL Server, PostgreSQL and Oracle, Sybase and Interbase... The list of databases supported by PHP alone would probably fill a whole screen.

PHP includes a huge number of built-in functions: processing strings and arrays, working with file system and with HTTP, email, date and time, Cyrillic and other national alphabets... When I first started programming in PHP, I was simply amazed by the abundance of built-in functions! Thanks to them, many algorithms that in most languages ​​require writing a program code several screens in size are implemented in PHP with one command (more precisely, by calling one function).

Modern trends in the development of programming languages ​​have not bypassed PHP. Object-oriented programming tools appeared in PHP3. And the PHP4 object model fully implements the classic concepts of object-oriented programming: inheritance, encapsulation and polymorphism.

All of the above allows, without any exaggeration, to call PHP the undisputed leader among web programming languages.

Target research– Learn and see examples of executing PHP scripts

An object research– PHP language, MySQL databases.

Subject of study– functional significance and relevance of the language.

Hypothesis research is that this language is very simple, easily integrated into HTML, in conjunction with PHP + MySQL + HTML is much superior to simple HTML.

Based on the hypothesis, the following tasks :

Tasks:

Explore Features and Opportunities PHP language;

Compare the functionality of PHP and HTML;

Familiarize yourself with MySQL databases;

Process the results and draw conclusions.

The theoretical basis of the presented material is historical data presented in reference books, material from the Internet.

Research methods: in the process of conducting research, a description, analysis of the literature on the topic, and the study of some variables were used.

Practical significance of the study: this material can be used by teachers and students for subsequent, in-depth study of this language.

The structure and content of the work include:

An introductory part in which the choice and relevance of the topic is justified.

Chapter 1, "Introduction to PHP and MySQL" contains 6 sections. The first section is an explanation of what PHP is. The second describes its capabilities. The third section of the chapter describes the history of the PHP language. In the fourth - it is said why it is worth choosing this particular programming language. The fifth and sixth sections are devoted to information about MySQL databases.

Chapter 2. PHP Examples consists of 3 sections. The first section shows how PHP can easily process data from HTML forms. In the second, we will enter information into the database. In the third section, we will receive information from databases.

Conclusion contains the main conclusions on the topic, confirming the hypothesis of the study.

Chapter 1. Introduction to PHP and MySQL

1.1. What is PHP?

PHP is a scripting language embedded in HTML that is interpreted and executed on the server. The easiest way to show this is with an example:

Example

After executing this script, we will get a page that will say:

Hi, I'm a PHP script!(see annex 1)

Very simple and useless.

The main difference from CGI scripts written in other languages ​​like Perl or C is that in CGI programs you write the output HTML code yourself, while with PHP you embed your program into the finished HTML page using the opening and closing tags (in the example).

The difference between PHP and JavaScript is that the PHP script is executed on the server, and the result of the work is transmitted to the client, while the JavaScript code is completely transferred to the client machine and only executed there.

Internet Information Server lovers will find that PHP is very similar to Active Server Pages (ASP), and Java enthusiasts will say that PHP is very similar to Java Server Pages (JSP). All three languages ​​allow you to place code that runs on a Web server inside HTML pages.

1.2. PHP features

In a nutshell, you can do everything in PHP that you can do with CGI programs. For example: process data from forms, generate dynamic pages, receive and send cookies (cookies).

In addition, PHP includes support for many databases, which makes writing database-enabled Web applications incredibly easy.

Here is a partial list of supported databases:

Adabas D InterBase Solid
dBase mSQL Sybase
Empress MySQL Velocis
FilePro Oracle Unix dbm
Informix PostgreSQL

On top of that, PHP understands IMAP, SNMP, NNTP, POP3 and even HTTP protocols, and also has the ability to work with sockets (sockets) and communicate using other protocols.

1.3. A Brief History of PHP

The beginning of PHP can be considered the fall of 1994, when Rasmus Lerdorf decided to expand the capabilities of his Home-page (Home page) and write a small engine to perform simple tasks. Such an engine was ready by the beginning of 1995 and was called Personal Home Page Tools. He was not very good at it - he understood the simplest language and only a few macros.

By the middle of 1995, a second version appeared, called PHP / FI Version 2. The prefix FI - was added from another Rasmus package that could process forms (Form Interpritator). PHP/FI was compiled inside Apache and used the standard Apache API. PHP scripts turned out to be faster than similar CGI scripts, since the server did not need to spawn a new process. The PHP language has come close in features to Perl, the most popular language for writing CGI programs. Support has been added for many well-known databases (such as MySQL and Oracle). The interface to the GD - library, allowed to generate pictures on the fly. Since then, PHP/FI has been widely adopted.

In late 1997, Zeev Suraski and Andi Gutmans decided to rewrite the internal engine to fix interpreter bugs and improve script execution speed. Six months later, on June 6, 1998, a new version was released, which was called PHP 3. By the summer of 1999, PHP 3 was included in several commercial products. As of November 1999, NetCraft reported that PHP was being used on over 1 million domains.

1.4. Why choose PHP

Web application developers need not be told that web pages are more than just text and pictures. A noteworthy site should support some level of interactivity with the user: searching for information, selling products, conferences, etc. Traditionally, all this has been implemented by CGI scripts written in Perl. But CGI scripts don't scale very well. Each new CGI call requires the kernel to spawn a new process, which takes CPU time and wastes RAM. PHP offers another option - it runs as part of a Web server, and in this way is similar to Microsoft's ASP.

Bill Carwin, web developer for over 20 years

Most recently, Stack Overflow released its annual Stack Overflow Developer Survey 2017 of the top trends. In it, PHP still occupies one of the leading roles.

  • JavaScript - 62.5%
  • SQL - 51.2%
  • Java - 39.7%
  • C# - 34.1%
  • Python - 32.0%
  • PHP - 28.1%
  • C++ - 22.3%
  • C - 19.0%
  • TypeScript - 9.5%
  • Ruby - 9.1%

PHP is still used more than Ruby. Forgive me Ruby fans, as this information is primarily related to the popularity of the programming language, rather than its usability.

PHP is slowly but surely losing its positions, but based on the huge number of applications written in this language, as well as a sufficient number of developers who know it, most likely PHP will stay in the top ten programming languages ​​for another 10 years.

Decades must pass before the once popular language disappears. There are still applications developed in languages ​​such as Perl, Pascal, BASIC, and even COBOL. Perhaps these languages ​​are now almost never used in the development of new projects, but the applications created with them continue to exist.

Nirbhai Naik, Digital Marketing

If this question had been asked 2-3 years ago, the answer would have been undoubtedly positive. The main reason for the bad reputation is not the most user-friendly design. In addition, many developers do not like the changes that this language has undergone over the past 10 years.

But for now, PHP7 may well be the salvation.

So what's improved in PHP 7? Compared to the previous version, PHP 7 has a huge amount of changes for the better. Here are some of them:

  • Improved performance
  • spaceship operator
  • Null-coalescing operator
  • Types of Scalar Parameters
  • Return type hints
  • Anonymous classes

And much more. New features can bring PHP back to life, but for that we'll have to wait and see how the developers implement the innovations in PHP 7.

Where is it movingPHP7?

PHP7 is the real salvation for PHP. Undoubtedly, the design before PHP left much to be desired.

Function naming inconsistencies can also be a problem for developers. Don't be surprised if you find a feature that's out of place. Many bugs have been fixed in PHP7, but the development team still needs to do enough work to improve the reputation of this language. Many online articles talk about PHP dying or about to die.

What does he sayTOBIE INDEX about futurePHP?

The Tobie Index is an index that evaluates the popularity of programming languages ​​based on a count of the results of search queries.

As you can see from the picture above, PHP is slowly moving up thanks to PHP7. If we were looking at this image in 2014, then we would definitely say that PHP will soon disappear. After 2004, when the language received the title of the year, its popularity has steadily declined. The graph looks amazing, showing that in the beginning developers liked what PHP had to offer, but later on newer and more promising technologies supplanted it.

WordPress switches toJavaScript

And now let's talk about the far from rosy prospects for PHP in the future. WordPress, the most popular CMS (Content Management System), is slowly moving towards JavaScript. WordPress, covering 25% of the websites on the internet, has shown a clear interest in JavaScript.

Matt Mullenweg, CEO of WordPress, horrified many with his statement: “ We realized that the old technologies will not allow us to move towards the future.“What does this mean for WordPress and PHP developers? Is PHP dying? At the moment it is very difficult to assume anything, but this situation does not look good for PHP.

Market situation

It would be foolish to try to predict the future of a programming language without taking into account the preferences of professional software developers. If you are a PHP developer or work for an IT firm, then you probably have an idea of ​​where PHP is in the market.

Currently, there are the following trends in the market with regards to the PHP language:

  1. PHP developers are much easier to find than experts in other programming languages.
  2. Hiring a PHP specialist is much cheaper.
  3. Due to the presence of a sufficient number of specialists, maintenance and support of products in this language is not expensive
  4. PHP is probably one of the easiest programming languages ​​to learn. This allows companies to hire young promising professionals and train them accordingly.
  5. There are a huge number of frameworks and CMS (content management systems) that run on PHP. For example, WordPress, Joomla, Magento, Drupal, etc.

Taking into account all the above points, it is safe to say that PHP is unlikely to disappear anytime soon. After all, users don't really care what technology is used to build the application. For them, the main thing is that it works.

What should you do if youPHP-developer?

This is a good question. Many developers switched from different technologies to PHP, while others, on the contrary, stopped using it and switched to other options. It all depends on what you want to do in the future. In this regard, PHP is reliable and will not disappear anytime soon.

A good tip in this case would be to learn another language. This will help you feel more secure in the future.

findings

Is PHP dead? Unlikely. Will this language disappear in the future? It's hard to say, as too many factors will influence it.

Vlad Ka, writes about web development

Today, web developers themselves can choose a specific tool for each individual project.

The PHP language can be used for a variety of tasks: for example, ReactPHP allows a developer to run a full-fledged server that constantly processes requests. With PHP, you can create long running processes. In addition, there are a huge number of tools to support and manage these processes (for example, supervisord).

William Harley, developer. Working with web development since 1996

According to some sources (Historical trends in the usage of server-side programming languages, March 2017), about 80%+ of the entire available Internet runs on PHP. This figure varies depending on the data collection methodology, but anyway, this is a very high figure!

Richard Kenneth Eng, uses Fortran, Tandem TAL, C/C++, C#, Obj-C, Java, Smalltalk, Python, Go

Most sites in the world are in PHP. It is unlikely that the owners and creators of sites are going to throw the money spent down the drain.

PHP has always been in the top ten programming languages ​​according to the versions of various publications: TIOBE, RedMonk, IEEE Spectrum, PYPL, CodeEval, HackerRank, etc.

In the IT field, programming languages ​​rarely “disappear”. Even COBOL left us a legacy. If a programming language proves useful, then it will probably last forever.

A language is only dead when no one uses it. This is hardly what is happening now with PHP.

Vakhroh Vein, Delphi / PHP / C++ Builder / JS / C# Developer and Securities Trader

In recent years, the popularity of this language has been declining. Meanwhile, the developers were rewriting it in accordance with the 201X standards. By the way, unlike Phyton, PHP was rewritten without loss of backward compatibility (99% preservation) of the existing code base.

With the advent of PHP 7+, the language has received modern functionality (syntax, constructions), and now it is extremely pleasant to work with it. Also, what most people have forgotten is that JavaScript, by taking over the design of the front end, has influenced every major programming language except PHP.

Max Chistokletov, is fond of developing in Haskell / Scala languages

Depends on what you mean by "disappear". Applications written in PHP won't just go away one day (or even within a couple of months). Therefore, no.

On the other hand, I haven't met a developer in a few years who was passionately working on an existing PHP project or creating a new project in this language. PHP may well be dead in the hearts of many (or even most?) developers.

I would advise you to familiarize yourself with such a phenomenon as the Lindy effect - Wikipedia. I think it can give a rough idea of ​​how long the existing technology can live.

The PHP language feels confident in the tops of the most popular programming languages, but everything has an end. Is PHP in danger of being sidelined, what could replace it, and what language would you choose to build a site like Facebook? These questions were answered by Quora users.

George Mauer, teaches JS

PHP is dying, Java is dying, C# is dying, SQL is dying, JavaScript is dying, C is dying. And still, there are much more projects in these languages ​​than there are projects in all non-listed programming languages ​​combined. Yes, sometimes new trends can crowd out something established, but this process is extremely slow. It does not mean at all that this is the oldest - bad.

I can't agree that PHP was the best option for creating Facebook. The creators of this social network actually only partially use PHP. Nowadays, it can be said that instead of this language, they have their own dialect called Hack. PHP was just the language that Zuckerberg and other developers used to start working on this project.

This is not to say that PHP was such a bad choice. Yet it is a language that meets the requirements of rapid application development. And this, in turn, is one of the key factors for a startup - in the case of Facebook, this factor played a decisive role. Initially, PHP was a kind of prefabricated collection of scripts. And this makes it easy to go to a lower level of abstraction and create C code when there is a real need for optimization. All this has been carefully studied and successfully implemented in most modern languages.

Please get the idea out of your head that a social network requires something specific from a programming language. The creation of most of the code will be completely standard and familiar. Yes, there are some problem areas, like matching prospective and potential friends, but similar issues are most often solved with the help of specialized software. For example, graph-oriented databases. You will need to build your system very carefully and carefully, and also have an excellent understanding of DevOps, but the same will have to be done with other languages. Rather than having an ideal development language, experienced and knowledgeable developers are more important to you.

And that actually brings me to the crux of the matter. PHP is extremely attractive, if only because a huge number of specialists at the moment know it well. But you are unlikely to want any of these specialists to work on your startup. You will want to hire people who speak several languages ​​at once, people who have a well-formed and well-grounded worldview. And such people, for the most part, do not particularly favor PHP.

Picture this: I'm sitting in a brand new office rented to develop my newly registered startup, staring at my desk with a bunch of money raised to develop the project, and puzzling over which programming language to choose. I understand that this is a problem somewhere in the 250th in the list of things that determine the success or failure of my future enterprise. And I'm starting to realize that this decision needs to be made together with the development team, which I don't have yet. Therefore, I myself begin to look for options for a language that would be relatively widespread, would easily allow developers to move to a lower level of abstraction, and, most importantly, in which I would have familiar developers who are the best in their field.

At the moment, this language is Elixir. It is based on the Beam virtual machine and is a fairly close relative of the Erlang language, which has shown itself well in a number of projects over the course of several decades. In addition, this language shows simply unimaginable results in terms of performance.

Go is another great choice. It is mainly a low-level language, but you can do everything in it that you can do in high-level languages. Even though I don't like some of the decisions regarding its structure, it deserves attention anyway.

Another option might be Clojure. There is a chance that developers in this language will not be easy to find, but given that this language is a direct descendant of LISP, I can sincerely call it incredibly powerful and giving a lot of options for development.

A few important notes:

  • look at F# on the .Net framework, or even the ubiquitous C#. If you do choose the latter option, then be prepared for the fact that in order to create a great team you will have to review and sift through a lot more resumes;
  • you can do amazing things with Scala;
  • Node.js I just have to mention Node.js. Personally, I think that Javascript is at the top of the chain at the moment, but Node has also been on the list of fairly widely used languages ​​for a long time. In addition, there are a large number of highly qualified specialists.

© Andrey Kukharchik
computer newspaper

Introduction

The Internet has long been firmly established in our lives. This bold statement can be proved or disproved many times, but one way or another everything changes, and no one will argue with this.

Gone are the days when many personal computer users, in order to save disk space, removed the Internet Explorer program from the operating system we all adore, because they simply did not need it. Now even those who do not have access to the Web try not to touch this program, since the formats of the Internet have long since moved into other areas of our computer life. And if a modem has settled in the computer, it is simply impossible to avoid the presence of a browser, since you really want to see at least one eye, but what is there, beyond the telephone line? And when the acquaintance took place, it is no longer so easy to give up the advantages of the network. After a while, you begin to realize that you need to somehow invest yourself and your ideas and manifest yourself in the vast expanses of the Global Network. It has become even prestigious to make your own page, and even if there is little useful in it, and the counter records only your infrequent visits, what you have done can be the beginning of a serious project and change your whole life.

Traveling through the expanses of the global Internet, you, of course, have repeatedly paid attention to hyperlinks, which sometimes reach outwardly very large sizes and simply lead to bewilderment by the presence of strange characters. And of course, everyone paid attention to the forms that needed to be filled out and received something in return. But few think about how it all works and what it all means.

I suggest you plunge into the world of programming. This world is in many ways unlike anything else, but it has its own unwritten laws and rules, its pros and cons. The world that opens before us is visible for only one more step, but this step must be mastered, and then it will be much easier to go further.

History of RNR

You should always start with the simplest, and now I will try to introduce you to programming in the PHP scripting language. PHP (read as pi-h-pi) was born in the fall of 1994, and its creator - Rasmus Lerdorf - used the language for his own purposes, in order to have an idea of ​​\u200b\u200bthe people who visit his site and get acquainted with his resume.

According to the author, PHP was written literally in a day between business meetings. At first it was just a nondescript CGI shell written in the Perl language, and it served only for specific purposes. For reference - CGI (Common Gateway Interface) - a common gateway interface, is a standard that is designed to create HTTP server applications.

This application, called a gateway or CGI program, is run by the server in real time. The server passes the user's requests to the CGI program, which processes them and returns the result of its work to the user's screen.

Thus, the visitor receives dynamic information, which can change as a result of the influence of various factors. The gateway itself (CGI script) can be written in various programming languages ​​- C/C++, Fortran, Perl, TCL, Unix Schell, Visual Basic, Apple Script and other similar languages. But in the case of PHP, Perl was chosen to write it in the original version, as the most simple and accessible.

As a result of further exploitation, it turned out that the shell has little performance, and the author had to rewrite everything again, but in C, which made it possible to increase the speed of PHP. Users of the server where the site with the first version of PHP was located asked for the same tool, and although the author did not assume that someone else would use this language, PHP quickly grew into an independent project, and in early 1995 the first known to us product version. The name of this first package was Personal Home Page Tools. At that time, RNR had more than modest capabilities. It had a basic code analyzer that understood a few special commands, as well as various utilities for use on the home page, needed to build such useful things as a guest book, counter, chat, statistics systems, and the like. By the middle of 1995, the language was thoroughly redesigned, form processing appeared, database functions were added, and in this form the second version of the product was released.

Today, PHP is a powerful cross-platform set of tools that resides on the server and is designed to process special code that is embedded in an HTML page. Thanks to this, it becomes possible to easily create dynamic sites. The files created in this way are stored and processed on the server, and when a visitor requests a document with PHP, the script is processed not by the visitor's browser, like Java Script, for example, but by the server, and only the results of the work are transmitted to the visitor. This is exactly how a CGI program written in C or Perl works. But, unlike CGI, PHP code can be embedded anywhere in an HTML page, which is a major advantage over CGI. And besides, the PHP language itself is very easy to learn and does not require any specific knowledge. For example, for me personally, the experience gained about ten years ago in computer science lessons at a school where we studied the BASIC language on very fashionable and expensive Yamahas was quite enough for me. Despite such an iridescent characteristic, PHP also has disadvantages. It is worth noting the rather slow (compared to CGI programs) work of PHP scripts, as well as the difficulty of writing large and complex programs. Either way, PHP remains an interpreted language, which will inevitably lead to performance degradation in case of very large and complex programs, but for performing simple manipulations on the site, PHP is the best choice. Not without reason, by the middle of 2000, PHP was used on more than 2.5 million sites!

Features of PHP

Like any programming language, PHP has its own syntax. It is very similar to the syntax of C or Perl. Programmers writing in these languages ​​will be able to master PHP in just a few days. But even if you've never programmed, PHP is easy for you and provides a base for moving into more advanced languages. All commands are quite logical, and the rules are simple. The syntax includes statements separated by semicolons. One of the main mistakes of novice programmers is the lack of a semicolon between statements. Fortunately, errors in PHP are displayed on the screen by default (unlike CGI, where all errors are written to a log file), and finding them with a certain care and experience will not be difficult. Moreover, a smart interpreter will tell you the line number in which the error occurred.

To program in PHP, you will need any text editor, but for convenience, it should provide syntax highlighting and line numbering. I'm using CuteHTML, which comes with the latest CuteFTP, a good FTP manager. The editor is very convenient, without unnecessary unnecessary functions, it is built into the context menu itself, does not require installation and has everything you need for programming. We also need a kit to work with PHP. Typically Apache+PHP is used, although it is not required, any server will do, such as Microsoft's IIS. But the first option is free and has great support with documentation (including in Russian) and forums where you can find out any question.

Now let's get down to business - write our first script. In order for the server to know which of the files contains the PHP code, its (file) extension must be made either phtml, or php3, or php. Strictly speaking, any extension can be assigned, but I recommend that you always use phtml for compatibility reasons. Every command in PHP usually starts with "" (hereinafter, without quotes). As you remember, several commands are separated by a semicolon. You can put a comment anywhere in the PHP script, it starts with "/*", and ends with "*/" If the comment is small and takes only one line, you can put "//" and thus easily comment out any line to its end. As usual, spaces, tabs and newlines are simply ignored and can be used to improve the readability of PHP code.

Screen output and variables in PHP

PHP makes it very easy to organize the output of text on the screen. Consider an example script:

This script can be placed anywhere in an HTML document, and by itself is not useful, as it only prints the phrase "Hello, world!" to the screen. But in this way we get acquainted with one of the most common PHP commands - displaying information on the user's screen. In order to give our script useful features, let's get acquainted with the variables. A variable is characterized by a name, type and value. The name can be anything and can include numbers, English letters, and allowed characters (such as underscores or dashes). By type, variables are divided into integer, floating point and character. The value according to the type can be almost anything. For example, the variable a=5. This tells us that the name of the variable is a, the type is integer, and the value is 5. Here are more examples of names and values:

As you can see, all variables in PHP (but not only in it) must begin with the $ symbol, which allows the interpreter to accurately distinguish them from PHP commands. In the first line of our script, the $name variable is assigned the value 6, and this variable automatically becomes an integer. By the way, you don't need to declare the type of a variable in advance, as in Pascal or Visual Basic, but although the division into types is purely conditional, each variable automatically tends to use the correct type, according to the value. The second line of code sets the $h12 variable to 4.89, which is a floating point value. The third and fourth lines of code assign values ​​to their variables, which are character strings. Anything enclosed in quotes (including numbers) will be interpreted as a character string. If variables are not previously defined but are used, their value is taken to be either zero or the empty string, depending on the type.

As in any language, any arithmetic operations can be performed on variables, and this does not require the presence of a special operator, it is enough to specify a variable for the result, an equal sign and list the variables or values ​​\u200b\u200bin the natural order with the necessary arithmetic signs. Example:

The result of the script is displaying the number 4 on the screen. All arithmetic operations and functions are supported, multilevel brackets, logical operations, increment or decrement operations, and more. In addition, the if-then-other comparison is very simple and natural. To do this, PHP uses the if () ( ) else ( ) construct. There are various syntaxes for this operator, but this one is the most basic and most logical of all. (if) if (condition) (then) ( what is in quotes is done ) (otherwise) else ( what is in quotes is done). It is not necessary to put a semicolon after the quotation marks, as usual between operators. But inside the quotes - the separation of operators among themselves is carried out only through a semicolon. Nesting multiple validation statements one into one is allowed. In this case, you need to be very careful about the number of closing quotes, because if even one is missing, the interpreter will generate a code error. Consider a simple example:

When comparing to true, two equal signs are used so that the interpreter can easily distinguish between a comparison and an assignment. The result of the script is 95, because $a is not equal to $b, and the command echo $b.$a; (between variables there is a dot, not the sign of an arithmetic operation) displays the specified variables in a row. Inequality (false) is indicated by the symbols !=, all other arithmetic and logical symbols and operators are allowed (for example, or, and, >,<= и т.д.).

PHP has a means of rapidly changing a variable by one, either up or down. To do this, you need to specify the name of the variable and after it, without the equal sign, two pluses or minuses in a row, respectively. For example $a++; - the variable $a will be incremented by one. Simultaneous assignment of one value to several variables is supported - $a = $b = 4;. Both variables will be equal to four. Here are some more examples of non-standard arithmetic operations in PHP:

File attachments in PHP

Everyone who has made at least one page on the web has encountered the problem of changing certain data on it. Of course, it is not difficult when there are several pages or there is only one :-), but if you have made a large site, a small addition (for example, to the menu) of hundreds of files can turn into a real nightmare! PHP solves this problem at a time by allowing you to nest one page within another. This is achieved using the REQUIRE and INCLUDE statements. These statements must be followed by the path to the attached file in parentheses. For example, INCLUDE("text.phtml"). The difference between these operators is that REQUIRE is replaced by the contents of the specified file and can only be used once, while INCLUDE inserts and executes the contents of the specified file, which allows you to apply it several times, for example, in a loop. In any case, when the file is executed, the PHP interpreter (correctly speaking - the parser) is in the HTML state, and to include it, the code must be enclosed in the construction. File attachments can only occur within the server space available to PHP. In other words, you cannot use http:// in the file name.

Quite often there are sites whose links include special characters - &, ?, %. All this can be the result of the work of PHP. The fact is that if you add? name = value at the end of the link, this value will be available under the same name in the file where the link points. If multiple names need to be added, they can be separated by &. Now we can make a site that will be accessible with just one page. And this page will display all the rest of the information based on the data received from the link. The appearance of such a link will be something like this: http://imya.ru/index.phtml?link=1. The unit at the end of the link is our parameter, which will be substituted in the index.phtml file. For example, like this:

...beginning of file......end of file...

Please note that the code we wrote takes into account the situation when the visitor specified the wrong parameter for various reasons. In this case, a prepared page with an error message is displayed. If the parameter corresponds to any of the site files, it is embedded in the code of the index.phtml file and executed. Thus, the beginning and end remain the same, but only the middle changes. And any changes no longer seem as scary as before. After all, you need to make them only in one file, and this will be reflected on the entire site.

There is another way. Its essence lies in the fact that PHP has access to the so-called server environment variables. One of these variables is the path requested by the visitor relative to the site address. And this path becomes available to us for use. In this case, our links will look like this: http://name.ru/index.phtml?patch/name.phtml. The second part of the link - patch/name.phtml - will be available to us if we consider the $QUERY_STRING parameter. For example, like this: $add = $QUERY_STRING. Now let's change our main index.phtml file so that everything works automatically. And if the requested parameter is not specified (correctly speaking, it will be equal to an empty string), in order to open something, we will assign the $add variable the name of the file that should be opened as the main page. Let this be the main.phtml file. Then the code will look like this:

...beginning of file......end of file...

As you can see, it's even easier. But I want to warn you right away that this method, although simpler than the first, opens the way to obtaining information about the server where the site with such a structure organization is located. An attacker or just a curious person, under certain circumstances and knowledge, will be able to learn a lot about your server, and this opens a direct path to hacking. So be careful and don't get in big trouble. You can protect yourself from such problems, but that's a completely different story.

PHP hit counter example

If you read everything carefully, you will easily receive the starting capital of knowledge, and it will give you the opportunity to independently further study the PHP language. And in the end, I suggest you get acquainted with the script code that will allow you to organize a visit counter on any of the pages of your site. This counter will not be fully functional, as it has a lot of drawbacks, but as an example of using PHP it is quite suitable. Anywhere on your page (but only where needed) paste the following code:

Page visitors -

In the same directory as your page, create a counter.dat file, upload it to the server and use your FTP manager to change the attributes of this file so that it is writable. Usually you need to set the checkboxes on all file attributes. If you don't, the script will constantly throw an error when trying to write to a file. By the way, in order to prevent this from happening, it is worth putting the @ symbol before the command to write and open a file, it will cancel the display of an error message on the visitor's screen. When the attributes are changed, refresh your page on the server and refer to it at its address in the browser. You will see that where you inserted the PHP code, the line appears: "Visitors to the page -" and then a number corresponding to the number of visits. And no trace of code! It was processed on a server on the Internet, and the result of this execution was simply transferred to the browser.

The algorithm of this script is very simple. In the first line, we assign to the selected variable the name of the file where the number of visits will be stored. In the second - open a connection to this file for reading. Next, we check the success of the connection, and if the file exists and it is available for reading, we read a line of 10 bytes from it, which is more than enough for the counter, and close the connection to the file. We increase the counter reading by one and display its new value on the screen. At the next stage, we need to write a new counter value, and for this we again open a connection (descriptor) with the file, but for writing with clearing the contents of the file. If it is successful, we write the new value of the counter there and close the file descriptor. That's all.

On this, our first acquaintance with the PHP language will be considered complete. Of course, there is still a lot of useful and interesting things left, but, unfortunately, it is impossible to cover everything at once. However, that's why smart people invented the Web, to make accessible the inaccessible. There are a lot of useful resources on the Internet on the subject of PHP, and with minimal skills in working with search engines, you can easily find them. And I advise you to subscribe to the PHP for everyone! mailing list, in which new articles, sample scripts appear regularly once a week, there is a section of questions and answers, as well as a lot of other useful information. You can find the subscription form and subscribe at the address on the web.

All previous issues of the mailing list are also posted there. And at the address you can see how it all works in action. Everything you will find on this site is made in PHP. Chat, forum, private announcements, statistics system - this is only a small part of the possibilities that PHP programming gives us.