Sunday, January 30, 2011

The syntax of PHP

Accessed from PHP.SU

PHP syntax

General concepts

PHP language designed specifically for web programming. PHP combines the advantages of C and Perl, and thus very easy to learn and have significant advantages over traditional programming languages.

PHP syntax is very similar to the syntax of C language and is largely borrowed from languages like Java and Perl.

Programmer with very quickly learn the language of PHP and can use it with maximum efficiency.
Basically, PHP has almost all the operators and functions available in the standard GNU C (or equivalent), such a cycle (while, for), the choice of operators (if, switch), the function of the file system and processes (fopen, * dir, stat, unlink, popen, exec), I / O functions (fgets, fputs, printf) and many others ...

The purpose of this section - a brief introduction to the basics of language syntax PHP. For more information on specific components of PHP syntax, see the relevant sections.

PHP and HTML

Cintaksis any programming language is much easier " pochuvstvovat", by example, rather than using some charts and diagrams. Therefore, we present a simple example script in PHP:

< html>
     < head>
         < title> Primer< / title>
     < / head>
     < body>

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

     < / body>
< / html>

You have probably noticed that this is a classic script, with which the start studying the language.

Note that the HTML-code correctly processed by the interpreter PHP.

Beginning of the script you may be confused: is this scenario? Where HTML-tags < html> and < body>? Here lies the main feature (by the way, very convenient) language PHP: PHP-script may not differ from the usual HTML-document.

Go ahead. You probably guessed that the code starts after the opening script tag <? and ends with the closing? >. So, between these two tags is interpreted as program text, and HTML-document does not fit. If the program needs something to take, it must use an operator echo.

Thus, PHP is designed so that any text that is located outside the program block, <? and? >, displayed in the browser itself. This is the main feature of PHP, as opposed to Perl and C, where the output is only using standard operators.

Instruction separation

Instructions are separated as in C or Perl - each expression ends with a semicolon.

The closing tag (? >) Also implies the end of the instructions, so the following two snippet equivalents:

<? php
     echo " This test";
? >

<? php echo " This test"? >

Comments in PHP scripts

Writing almost any script is not without comment.

PHP supports kommetarii in the style of 'C', 'C' and shell Unix. For example:

<? php
     echo " This test" / / This is a one-line comment style c
     / * This is multiline comment
        another line of comment * /
     echo " This is another test";
     echo " Last test"; # This is a comment in the style of the Unix shell
? >

Single-line comments only go to the end line or the current block of PHP-code, depending on what comes before them.

< h1> This <? php # echo " prostoy";? > primer.< / h1>
< p> header above will say 'This is an example. "

Be careful, watch for the lack of nested 'C'-comments, they may appear during commenting large blocks:

<? php
  / *
     echo " This test" / * This comment will cause a problem * /
  * /
? >

Single-line comments only go to the end line or the current block of PHP-code, depending on what comes before them. This means that the HTML-code after / /? > Will be printed:? > Displays of PHP mode and returns to HTML, but / / does not allow it.

Variables in PHP

Variable names are marked with $. Same " Hi, I'm - script PHP! " can be obtained as follows:

<? php
$ Message = " Hi, I'm - script PHP! ";
echo $ message;
? >

Details about the variables in PHP are here

Data Types in PHP

PHP supports eight primitive data types:

Four scalar types:

- Boolean (binary data)
- Integer (whole numbers)
- Float (floating point number or 'double')
- String (string)

Two compound types:

- Array (arrays)
- Object (objects)

And two special types:

resource (resource)
NULL (" pustye")

There are also several pseudotype:

- Mixed (mixed)
- Number (number)
- Callback (callback)

Details about the data types in PHP here

Expressions in PHP

The basic forms of expressions are constants and variables. For example, if you write " $ a = 100", you assign a '100 'to variable $ a:

$ A = 100;

In the example above $ a - it is a variable, = - is the assignment operator, and 100 - this is an expression. Its value is 100.

Expression may be variable, if it is associated a specific value:

$ X = 7;
$ Y = $ x;

The first line of this example is an expression of constant 7, the second line - the variable $ x, because before she was set to 7. $ Y = $ x is also an expression.

Details of the expressions in PHP can be found here

PHP Operators

An operator is something that consists of one or more values (or expressions, in programming jargon) which yields another value (so that the entire structure can be seen as an expression).

Examples of operators PHP:

Assignment operator:

<? php

$ A = ($ b = 4) 5 / / result: $ a is equal to 9, and $ b set to 4.

? >

Combined operators:

<? php

$ A = 3;
$ A = 5 / / sets $ a to 8, the same record: $ a = $ a 5;
$ B = " Hello ";
$ B .= " There! "; / / Sets $ b string " Hello There! ", Like $ b = $ b. " There! ";

? >

String operators:

<? php
$ A = " Hello ";
$ B = $ a. " World! "; / / $ b contains a line " Hello World! "

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

There are also logical and comparison operators, but they are usually seen in the context of language control structures.

Detailed information on the PHP operators can be found here.

Control constructs of the PHP language

The main structures of the PHP language are:
Conditionals (if, else);
Loops (while, do-while, for, foreach, break, continue);
Design selection (switch);
Ad design (declare);
Design return values (return);
Design inclusions (require, include).

Examples of structures of language PHP:

<? php
if ($ a > $ b) echo " a value greater than b";
? >

The above example illustrates the use of the if in conjunction with a comparison operator ($ a > $ b).

In the following example, if the variable $ a is not equal to zero, the string will be printed " the value of a true (true), that is, shows the interaction of the conditional operator (construction) if the logical operator:

<? php
if ($ a) echo " the value of a true (true) ";
? >

Here is an example of the cycle while:

<? php
$ X = 0;
while ($ x < 10) echo $ x;
/ / Displays 12345678910
? >

Information on all the control structures PHP you can get here

User-defined functions in PHP

In any programming language, there are routines. In the C language, they are called functions in assembly language - routines in Pascal, there are two kinds of subprograms: procedures and functions.

In PHP, these routines are user-defined functions.

Subroutine - a specially designed piece of software, which can be accessed from anywhere within the program. Routines simplify life for programmers, improving the readability of source code, as well as reducing it, because fragments of code does not need to write a few times.

Give an example of a custom function on the PHP:

<? php

function funct () {
$ A = 100;
 echo " < h4> $ a< / h4> ";
}
funct ();

? >

The script displays the 100:

100

User-defined functions in PHP you can pass arguments and get return value.

Detailed information on user functions PHP can be found here

Built-in (standard) PHP function

PHP contains a massive amount of built-in functions that can perform tasks at various levels of complexity.

PHP.SU portal contains a complete guide to the standard features PHP.

OOP and PHP

PHP has fairly good support for object-oriented programming (OOP).

In PHP you can create different levels of classes, objects, and is flexible enough to operate them.

Here's a sample PHP class and use it:

<? php
/ / Create a new class of Coor:
class Coor {
/ / Data (properties):
var $ name;

/ / Methods:
 function Getname () {
 echo " < h3> John< / h3> ";
 }

}

/ / Create object of class Coor:
$ Object = new Coor;
/ / Get access to class members:
$ Object-> name = " Alex";
echo $ object-> name;
/ / Displays 'Alex'
/ / Now we get access to a class method (in fact, to function within a class):
$ Object-> Getname ();
/ / Displays 'John' in big letters
? >

Details about classes and OOP in PHP can be found here

That is so, briefly describe the syntax, can be characterized by great language PHP. To obtain detelnoy information for each intended installation issue, use the appropriate sections and articles.

No comments:

Post a Comment

Confused? Feel free to ask

Your feedback is always appreciated. I will try to reply to your queries as soon as time allows.

Note:
Please do not spam Spam comments will be deleted immediately upon my review.