As far as I understand, the description of the basic capabilities of SQL, with which I slightly interrupted our course a few lessons ago, turned out to be not superfluous.

Today I want to again deviate a little from the “party course” and talk about structures. But not SQL, but PHP itself. This will help you read program listings easier, and I will be able to write subsequent functions more concisely without fear that you will not understand me.

Don’t be alarmed, this doesn’t mean at all that I’m going to continue writing everything complicated and vague. No, nothing like that. I just want you to understand that PHP is not BASIC, where you can only write as you can write. And nothing else. Well said, did you understand what you wrote, Kurepin?! Got it, got it...

Meet - period.

Dot "." you already know, it concatenates strings. Plus "+" adds numbers, and a dot combines lines.
$a1="100";
$a2="200"
What happens if you fold $a1 And $a2? How to put this...
$a1+$a2=300
$a1.$a2="100200"
...and never forget about it.

Here's another way to write addition.

Let's assume that we are composing some kind of complex string from various fragments. We can append new data to the end of our variable like this:

$this->string_about_letters = $this->string_about_letters."a few letters..."; $this->string_about_letters = $this->string_about_letters." a few more letters..."; $this->string_about_letters = $this->string_about_letters." and a few more letters..."; $this->string_about_letters = $this->string_about_letters." a few letters again...";
A bit long, isn't it? Repeating a long variable gets in the way $this->string_about_letters. Therefore, we will record it differently:
$this->string_about_letters .= "several letters..."; $this->string_about_letters .= "a few more letters..."; $this->string_about_letters .= "and a few more letters..."; $this->string_about_letters .= "a few letters again...";
More convenient, right?

The same applies to mathematical addition:
$abc+=1;
Add 1 to the contents of a variable $abc.

How else can you add one? Usually, according to S-shno:

$abc++; or +$abc;

These two expressions are commonly used in cyclic constructs. And not by themselves, but in some more complex expression.
The difference between these two expressions is that in the first case, the value of the variable is taken into account in the expression, and then one is added, and in the second, the opposite is true - first the value of the variable is increased, and then the resulting value is used in the expression. An example will make it clear:
$a=1;
echo $a++;
echo +$a;
First echo will print out “1” for us, and print out the second one?.. but that’s not correct! Not 2 it will print out, and "3". Why? Guess for yourself.

Today I also want to break down for you one piece of code that we will often use in our program. And in general in any program where data is drawn from a SQL database.

We are talking, of course, about obtaining data through select-A.

You already know how to query the database, but we haven’t really talked about how to parse the received data. Meanwhile, this can be done in various ways.

Let's start with an example. Suppose we select titles of video films released in 2000 from the video cassette database and want to print them in the form of a table (with the cassette ID, of course).

So, you could arrange the procedure like this (you will find something similar in many textbooks):

$this->sql_query="select * from film where f_date between "2000-01-01 00:00:00" and "2000-12-31 23:59:59 order by f_id""; $this->sql_execute(); $str=" "; $row_count=mysql_num_rows($this->sql_res); for($i=0;$isql_res); $str=$str." \n"; ) $str=$str."
".$film["f_id"]."".$film["f_name"]."
";
Let me explain.

We make a request to the database (by the way, pay attention to the new condition in the request: between date and date, this form is often used to indicate a date range).

We initiate a text variable to enter HTML code into it by adding the first fragment of text - the beginning of the table.

We then open a loop with a number of iterations equal to the number of rows retrieved from the query.

In the body of the loop, we extract the next line from our request into an array variable $film. Function mysql_fetch_array arranges data into associative array, using field names from the table as keys.


    An associative array is the same as a regular (numbered) array, only strings of characters, rather than numbers, are used as names for the cells. And you should access the cells of such an array accordingly: $abc["first"], $abc["mama"]...
Next, we add the next row of the table to our text variable, using the data obtained from the database. To access data, as you can see, table field names are used. This is a property of the mysql_fetch_array function, as I already said.

    Pay special attention: refer to cells associative arrays not allowed in text strings! It is necessary to break the line and “paste” the values ​​using dots (.), as shown.
The cycle is repeated the required number of times, after which we close $str the last html tag. Ready.

But isn't this all too cumbersome? In my opinion, very much so. I propose to write this all down differently: everything is the same, but shorter.

All basic mathematical operations are available in PHP. Both integers and real numbers can be used as operands.

The table shows a list of arithmetic operators:
OperatorSignDescription
Addition + Addition of two values
Subtraction - Subtracting one value from another
Multiplication * Multiplying two values
Division / Dividing one value by another
Getting the remainder of a division % Dividing one value by another and returning the remainder (modulo division)
Increment ++ Shorthand for increasing a number by one
Decrement -- Shorthand for decreasing a number by one
Unary negation - Turning a positive number into a negative or a negative number into a positive

Subtraction, multiplication, division, modulo, and addition operators

The operators of subtraction, multiplication, division, modulo, and addition are used in the same way as in mathematics. Here it is worth paying attention to the division and modulo operators.

The division operator (" / ") returns a floating-point number, unless both values ​​are integers (or strings that are converted to integers) that are divided by an integer, in which case it returns an integer value.

result2 = $result2
"; echo "result3 = $result3
result4 = $result4"; ?>

In modulo division, the operands are converted to integers (with the fractional part removed) before the operation begins. The result of the division remainder operation % will have the same sign as the dividend:

Unary negation

The unary negation operator is denoted by the sign "-", and it reverses the meaning of its only operand:

In this case, the parentheses are not needed because unary negation has the highest priority, but they help organize the code so that it is clear that the numbers -5 and 4 are being added.

Increment

The increment operator is denoted by the ++ sign and can be placed on either side of the operand it operates on. It increases this value by one, just like adding one to a value. The actual result depends on whether the operator was applied before or after the operand with which it was applied. This operator often used with variables, and often this happens inside loops (loops will be discussed later).

Prefix form of increment

Prefix form- this is when the increment operator is placed before the operand, this form of notation means that the increment will be executed first: it increases the value of the operand by one and only then the rest of the instruction is executed:

Postfix form of increment

Postfix form is written a little differently - the increment is located in this case after the operand. In postfix notation, the first use of the operand returns its current value, only after that the value will be incremented by one:

Decrement

The decrement operator is denoted by the sign --, and unlike the increment operator, it decreases, rather than increases, the value of its operand by one. Decrement also allows prefix and postfix notation:

Operations with PHP variables (operators)

There are various groups for implementation.

An operator is something consisting of one or more values ​​(expressions in programming jargon) that can be evaluated as a new value (thus, the entire construct can be considered an expression). It follows that functions or any other constructs that return a value (for example, print()) are operators, unlike all other language constructs (for example, echo()), which return nothing.

Arithmetic operations in PHP

Remember school basics of arithmetic? The statements below work the same way.

The division operator ("/") always returns a real type, even if both values ​​were integers (or strings that convert to integers). Otherwise, the result will be fractional.

The operation of calculating the remainder of division " % " only works with whole numbers, so applying it to fractions may produce undesirable results.

It is possible to use parentheses. The precedence of certain mathematical operations over others and the change in priorities when using parentheses in arithmetic expressions follow the usual rules of mathematics.

Increment and decrement operations

PHP, like C, supports prefix and postfix increment and decrement operators.

Postfix increment and decrement operators

As in C, these operators increment or decrement the value of a variable, and in an expression return the value of the variable $a before the change. For example:

$a=10;
$b=$a++;
echo "a=$a, b=$b"; // Prints a=11, b=10

As you can see, first the variable $b value assigned to variable $a, and only then the last one was incremented. However, the expression whose value is assigned to the variable $b, may be more difficult - in any case, increment $a will happen only after it has been calculated.

Prefix increment and decrement operators

There are also increment and decrement operators, which are specified rather than after the variable name. Accordingly, they return the value of the variable after the change. Example:

$a=10;
$b=--$a;
echo "a=$a, b=$b"; // Prints a=9, b=9

Increment and decrement operations are used very often in practice. For example, they occur in almost any cycle for .

echo "

Postfix increment

" ;
$a = 5 ;
echo "Should be 5: " . $a++ . "
\n" ;

\n" ;

Echo "

Prefix increment

" ;
$a = 5 ;
echo "Must be 6: " . ++ $a . "
\n" ;
echo "Must be 6: " . $a . "
\n" ;

Echo "

Postfix decrement

" ;
$a = 5 ;
echo "Should be 5: " . $a -- . "
\n" ;

\n" ;

Echo "

Prefix decrement

" ;
$a = 5 ;
echo "Must be 4: " . -- $a . "
\n" ;
echo "Must be 4: " . $a . "
\n" ;
?>

String operations

PHP has two operators for working with strings. The first is the concatenation operator ("."), which returns the concatenation of the left and right arguments. The second is an assignment operator with concatenation, which appends the right argument to the left one. Let's give a specific example:

$a = "Hello" ;
$b = $a . "World!" ; // $b contains the string "Hello World!"

$a = "Hello" ;
$a .= "World!" ; // $a contains the string "Hello World!"
?>

Bitwise operations

These operations are designed to operate (set/unset/check) groups of bits in an entire variable. The bits of an integer are nothing more than individual digits of the same number written in the binary number system. For example, in binary the number 12 would look like 1100 and 2 would look like 10, so the expression 12|2 will return us the number 14 (1110 in binary notation). If a variable is not an integer, then it is
first rounded, and then the following operators are applied to it.

To represent one number, 32 bits are used:

  • 0000 0000 0000 0000 0000 0000 0000 0000 is zero;
  • 0000 0000 0000 0000 0000 0000 0000 0001 is 1;
  • 0000 0000 0000 0000 0000 0000 0000 0010 is 2;
  • 0000 0000 0000 0000 0000 0000 0000 0011 is 3;
  • 0000 0000 0000 0000 0000 0000 0000 0100 is 4;
  • 0000 0000 0000 0000 0000 0000 0000 0101 is 5;
  • 0000 0000 0000 0000 0000 0000 0000 1111 is 15;

Bitwise operators:

Example Name Result
$a & $b Bitwise "and" Only those bits that are set in both $a and $b are set.
$a | $b Bitwise "or" Those bits that are set in either $a or $b are set.
$a^$b Exclusive or Only those bits that are set in either $a only or $b only are set
~$a Negation Those bits that are not set in $a are set, and vice versa.
$a<< $b Shift left All bits of variable $a are shifted $b positions to the left (each position implies a "multiply by 2")
$a >> $b Shift right All bits of variable $a are shifted $b positions to the right (each position implies "division by 2")

Comparison Operations

Comparison operators, as their name suggests, allow you to compare two values.

These are unique operations in their own way because, regardless of the types of their arguments, they always return one of two things: false or true. Comparison operations compare two values ​​with each other and, if the condition is true, return true, And if not - false.

PHP only allows scalar variables to be compared. Arrays and objects cannot be compared in PHP. They cannot even be compared for equality (using the == operator), but PHP does not issue a warning when performing such an operation. So, having once wondered why two completely different arrays when comparing them using == suddenly turn out to be the same, remember that before comparison both operands are converted to a word array, which is then compared.

See Array comparison for details.

Comparison operators:

Example Name Result
$a == $b Equals TRUE if $a is equal to $b.
$a === $b Identically equal TRUE if $a is equal to $b and has the same type. (Added in PHP 4)
$a != $b Not equal TRUE if $a is not equal to $b.
$a<>$b Not equal TRUE if $a is not equal to $b.
$a !== $b Identical is not equal TRUE if $a is not equal to $b or if they are different types(Added in PHP 4)
$a< $b Less TRUE if $a is strictly less than $b.
$a > $b More TRUE if $a is strictly greater than $b.
$a<= $b Less or equal TRUE if $a is less than or equal to $b.
$a >= $b More or equal TRUE if $a is greater than or equal to $b.

Logical operations

Logical operators are designed exclusively for working with Boolean expressions and also return false or true.

Here is a table of PHP logical operators:

It should be noted that the calculation logical expressions, containing such operators, always goes from left to right, and if the result is already obvious (for example, false&&something always gives false), then the calculations are terminated, even if the expression contains function calls. For example, in the operator $logic = 0&&(time()>100); standard function time() will never be called.

Be careful with logical operations- don’t forget about doubling the symbol. Please note that, for example, | And || - two completely different operators, one of which can potentially return any number, and the second - only false And true.

The increment (++) and decrement (--) operators do not work with boolean variables.

Equivalence operators

In PHP, starting from PHP4 there is an identical comparison operator - a triple equal sign === ,
or the check operator. PHP is fairly tolerant of strings being implicitly converted to numbers, and vice versa.
For example, the following code will print that the values ​​of the variables are equal:

$a=10;
$b="10";

And this despite the fact that the variable $a represents a number and $b- line. Now let's look at a slightly different example:

$a=0; // zero
$b=""; // empty line
if($a==$b) echo "a and b are equal"; // Prints "a and b are equal"

Although $a And $b are clearly not equal even in the usual sense of the word, the script will declare that they are the same. Why is this happening? The point is that if one of the operands logical operator can be treated as a number, then both operands are treated as numbers. In this case, the empty line turns into 0 , which is then compared to zero. It is not surprising that the operator echo works.
The problem is solved by the equivalence operator === (triple equality). It not only compares two expressions, but also their types. Let's rewrite our example using this operator.