Instruction separation. PHP Basics How instructions are divided in PHP

TagsPHP

When PHP processes a file, it looks for opening and closing tags, such as , which tell PHP when to start and stop processing the code in between. This way of processing allows PHP to be embedded in all sorts of different documents, since anything outside of a pair of opening and closing tags will be ignored

PHP parser.

PHP also allows short tags, however it is not advisable to use them as they are only available if enabled via the php.inishort_open_tag configuration directive, or if PHP has been configured with the --enable-short-tags option.

If the file contains only PHP code, it is preferable to omit the closing tag at the end of the file. This helps avoid adding random space characters or newlines after the PHP closing tag, which can cause unwanted effects because PHP begins to output data to the buffer when there is no programmer intent to output any data at that point in the script.

Isolation from HTML

Anything outside of the opening and closing tag pair

is ignored by the PHP interpreter, which has the ability to handle files with mixed content. This allows PHP code to be embedded in HTML documents, for example to create templates.

This will be ignored by PHP and rendered by the browser.

This too will be ignored by PHP and rendered by the browser.

This works as expected, because when the PHP interpreter encounters closing tags?>, it simply starts printing whatever it finds until it encounters another opening tag, except in the case of a conditional statement contained within the code, in which the interpreter determines the result of the condition before accepting deciding what to skip.

Using Structures with Conditions

This will be displayed if the expression is true. Otherwise this will be displayed.

Example #1 Advanced isolation using conditions

In this example, PHP skips blocks where the condition is not met. Even though they are outside the opening/closing tag pair, PHP will skip them according to the condition, since the PHP interpreter will jump over blocks contained within a condition that is not met.

When outputting large blocks of text, exiting PHP parsing mode is usually more efficient than sending the text using the echo functions or print .

There are four sets of tags that can be used to tag PHP code. Of these, only two and ) are always available. The other two are short tags and ASP style tags, which can be turned on or off in the configuration file php.ini. While short and ASP-style tags can be convenient, they are not as portable as the long versions and are therefore not recommended. Additionally, if you intend to insert PHP code into XML or XHTML to comply with XML standards, you should use the .

1. 2. echo "some editors (eg FrontPage) don't like processing instructions"; 3. This is a synonym for "" 4.

Short tags (third example) are only available when they are enabled using the short_open_tag directive in the php.ini configuration file, or if PHP was compiled with the --enable-short-tags option.

ASP style tags (fourth example) are only available when they are enabled using the asp_tags directive in the php.ini configuration file.

Comment:

You should avoid using short tags when developing applications or libraries that are intended to be distributed or hosted on PHP servers that are not under your control, since short tags may not be supported on the target server. To create portable, compatible code, do not use short tags.

Instruction separation

Like C or Perl, PHP requires ending statements with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically applies a semicolon; those. There is no need to put a semicolon at the end of the last line of a block of PHP code. The block's closing tag will "absorb" the immediately following newline, if one is found.

Single-line comments only go to the end of the line or the current block of PHP code, whichever comes before them. This means that the HTML code after // ... ?> or # ... ?> will be printed:?> ends PHP mode and returns HTML mode, and // or # cannot influence this. If the asp_tags directive is enabled, then similar behavior will occur with // %> And # %> . However, the tag does not end PHP mode in a one-line comment.

That's an example

The title at the top will say "This is an example".

"C" comments end at the first detected sequence */ . Make sure you don't nest "C" comments within each other. It's very easy to make this mistake when commenting out a large block of code.

Operators: comparisons

$a == $b // TRUE if $a is equal to $b.

$a === $b // TRUE if $a is equal to $b AND has that

same type

$a != $b // TRUE if $a is not equal to $b.

$a !== $b // TRUE if $a is not equal to $b OR in

case if they are of different types.

$a< $b // TRUE если $a строго меньше $b.

$a > $b // TRUE if $a is strictly greater than $b.

$a = $b // TRUE if $a is greater than or equal to $b.

Operators: logical

$a and $b // TRUE if both $a and $b are TRUE.

$a or $b // TRUE if either $a or $b

! $a // TRUE if $a is not TRUE.

$a && $b // TRUE if both $a and $b are TRUE.

$a || $b // TRUE if either $a or $b

$a and $b and $c; $a and $b or $c

$a and ($b and $c ) $a and $b && $c

$a and ($b or $c ) $a and $b || $c

Data types

PHP supports eight simple types.

Four scalar types:

    float (floating point number, also known as double)

Two mixed types:

Two special types:

Typically, the programmer does not set the type of a variable; This is usually done by PHP at runtime depending on the context in which the variable is used.

Comment:

If you want to check the type and value of a certain expression, use var_dump() .

If you just need a human-readable representation of the type for debugging, use gettype() . To check for a specific type,Notuse gettype() , use for this is_type functions.

If you want to force change the type of a variable, you can either cast the variable or use the settype() function.

Please note that a variable, depending on its type in this moment, may have different meanings in certain situations.

Boolean type

This is the simplest type. boolean expresses the truth value. It can be either TRUE or FALSE.

To specify boolean, use keyword TRUE or FALSE. Both are case-insensitive.

Typically, some operator returns a boolean value, which is then passed to the control structure.

To explicitly convert to boolean, use (bool) or (boolean) . However, in most cases a type cast is not necessary, since the value will be automatically converted if the operator, function, or control construct requires boolean argument.

When converted to boolean, the following values ​​are treated as FALSE :

    the value itself is booleanFALSE

    integer0 (zero)

    float0.0 (zero)

    empty string, string "0"

    arraywithout elements

    borderless object (PHP 4 only)

    special type NULL (including unset variables)

    SimpleXML objects created from empty tags

All other values ​​are treated as TRUE (including any resource as well as the number -1).

Whole numbers

Integer is a number from the set ℤ = (..., -2, -1, 0, 1, 2, ...).

Integers can be specified in decimal, hexadecimal, or octal notation, optionally preceded by a sign (- or +).

If you are using the octal number system, you must precede the number with a 0 (zero); to use the hexadecimal number system, you must precede the number with 0x.

Example #1 Integers

The size of the integer varies by platform, although typically the maximum value is around two billion (this is 32-bit signed). PHP does not support unsigned integers.

Exceeding the size of a whole

If you define a number that exceeds the limits of the integer type, it will be interpreted as a floating point number. Also, if you use an operator that results in a number greater than the limits of an integer, a floating point number will be returned instead.

There is no integer division operator in PHP. The result 1/2 will be a floating point number 0.5 . You can cast the value to an integer, which always rounds it down, or use the round() function.

Floating point numbers

Floating-point numbers (also known as doubles or real numbers) can be defined using any of the following syntaxes:

A string is a set of characters. In PHP, a character is the same as a byte, which means that there are exactly 256 different characters possible. This also means that PHP does not have native Unicode support. Some Unicode support is provided by the utf8_encode() and utf8_decode() functions.

Comment:

There is no problem if the string is very large. There are virtually no restrictions on the size of strings imposed by PHP, so there is absolutely no reason to worry about their length.

A string can be defined in three different ways.

    single quotes

    double quotes

    When is a block of loop actions executed?
    for (expr1; expr2; expr3) (
    // action block
    }

    √ if the second expression (expr2) evaluates to true
    if the first expression (expr1) evaluates to true
    if the third expression (expr3) evaluates to true

    Display all elements of the array $arr = array(4,3,2,1);?
    √ foreach ($arr as $a) echo "$a ";

    for ($i=1; $i

    Given an array $arr. Print the fourth element of the array if it is equivalent to the string "4".

    Command given:
    if ($var) echo "Hello";
    else echo "Bye";
    In what case will the word “Bye” be displayed on the screen?

    √ if $var === false
    if $var == "true"
    √ if $var == ""

    The command given is: if(!$var) echo “Hello”; In what case will the word “Hello” be displayed on the screen?
    if $var == true
    √ if $var is converted to boolean false
    √ if $var = 0

    What can PHP be used for?
    for creating operating systems
    √ to create scripts that run in command line
    √ for creating GUI client applications

    What can't PHP be used for?
    √ for creating operating systems
    to create scripts that run on the command line
    for creating client GUI applications

    It is known that PHP settings you can save/change not only in php.ini, but also in the PHP scripts, in the .htaccess files and in the httpd.conf server settings file. Where can I set the value of the register_globals option?
    in the user script
    in php.ini, in a user script or in a .htaccess file
    √ in php.ini, in the .htaccess file or in httpd.conf

    It is known that PHP settings can be saved/changed not only in php.ini, but also in the PHP scripts themselves, in .htaccess files and in the httpd.conf server settings file. Where can I set the value of the session.auto_start option?
    √ in the user script
    √ in php.ini or httpd.conf
    √ in the .htaccess file

    It is known that PHP settings can be saved/changed not only in php.ini, but also in the PHP scripts themselves, in .htaccess files and in the httpd.conf server settings file. Where can I set the value of the user_dir option?
    in the user script
    √ only in php.ini or httpd.conf
    in the .htaccess file

    How is PHP embedded in HTML code?
    √ using ‘’ tags
    √ using ‘’ tags
    using '' tags

    How are comments defined in PHP?
    √ // comment line
    √ /* several lines of comments */
    / a few lines of comments /
    ** comment line **

    How can you define an array in PHP?
    √ $arr = array("a","b","c");
    √ $arr = "a";
    $arr("0″=>"a");
    $arr["a","b","c"] = "q";

    How can you set a string in PHP?
    √ using double quotes: $var = "this is a string";
    using the opening character '':
    $var = >;
    √ using single quotes: $var = ‘this is a string’;
    √ heredoc syntax:
    $var =

    Internet