A brief discussion of PHP syntax (1)_PHP tutorial

WBOY
Release: 2016-07-21 16:07:25
Original
741 people have browsed it

Author: Hua Honglang
Text:
In the article "Talk about HTML syntax", I have introduced the basic HTML syntax. It is possible to compile a static Web page, but it is very important to dynamically interact information. For example, the membership system of some websites requires the operation of back-end programs for member registration and login. The CGI programs used by many websites are mainly written in Perl, ASP, Java, and PHP, and what we are going to use is PHP. It's completely free, thanks to those obscure programmers. The structure of PHP is similar to the C language, which is in line with the "Learn in one place, program anywhere" mentioned in the C language.I believe that people who have learned the C language can easily get started with PHP. Let’s introduce some PHP syntax first. This article is suitable for beginners to learn.
There are also some differences between PHP and C language, or it may be more flexible than C language to some extent. In C language, variables must be defined before they can be used. In PHP, variables do not need to be defined in advance and can be used directly. The type of the variable is automatically generated when assigning a value. The types of PHP variables are divided into: integer (int), double (double), string (string), array (array), and object (object).
When the integer size exceeds its range, it is automatically converted to double precision. The value range is as follows:
┌─────┬─────┬──────┬──── ────────┐
│ Declaration type │Length (bits) │Length (bytes) │ │ Value range │
├─────┼─────┼─── ───┼────────────┤
│ int │ 32 │ 4 │-2147483647~2147483647 │
├─────┼─────┼── ────┼────────────┤
│ double │ 32 │ 4 │ 1.7E-308~1.7E+308 │
└─────┴── ───┴──────┴────────────┘
String, usually represented by "" (double quotes). It can also be expressed by '' (single quotation marks), as follows:
$a="abc";
$b="abc$a";
$c='abc$a';
$ d=""cde"";
$e='"cde"';
Various variables in PHP are distinguished by adding "$" before the variable name.
Note that the content of $b is abcabc, the content of $c is abc$a, the content of $d is "cde", and the content of $e is also "cde". It can be seen that the variable names in the content in double quotes will be replaced, but those in single quotes will not. The content in double quotation marks needs to be escaped, such as $, which should be represented by $, but the content in single quotation marks should not be escaped.
The array syntax in PHP is:
Array name [index]
The index can be a number or a text. But it is not recommended to use text because it has little meaning. It is also more flexible than other languages ​​for arrays as follows:
$names[]=100;
$names[]=200;
$names[]="hi,how are you";
$names[]=98.5;
$names[]=1.7E+23;
$num=count($names);
for ($i=0;$i< ;=$num;$i++){
echo "$names[$i]
";
    }
?>
It can be seen that the elements in an array are not necessarily are of the same type, this is the "live" part of PHP arrays.
Using objects makes it easier for programmers to maintain and makes programs easier to read. Compared with other languages, PHP is much simpler. It only has categories, methods, attributes, extensions, etc.

The previous article only talked about the data types of PHP. The so-called "sharpening the knife does not waste time cutting firewood". Only by laying a good foundation in PHP can you learn PHP programming better.
The expressions and operators in PHP are not much different from those in C language. They are listed below:
┌─────┬────────┬──── ──────┐
│ Symbol │ Operator │ Example │
├─────┼─────────┼─────────────┤
│ + │ Addition │ $a+$b │
├─────┼─────────┼───────────┤
│ - │ Subtraction │ $a-$b │
├─────┼──────────┼──────────┤
│ * │ Multiplication │ $a*$ b │
├─────┼─────────┼──────────┤
│ / │ Division │ $a/$b │
├─────┼─────────┼──────────┤
│ % │ Take the remainder │ $a%$b │
├─── ──┼─────────┼──────────┤
│ ++ │ Increment │ $a++ or ++$a │
├───── ┼─────────┼──────────┤
│ -- │ Decreasing │ $a--or--$a │
├───── ┼─────────┼──────────┤
│ == │ Equivalent to │ $a==10 │
├─────┼─── ──────┼──────────┤
│ === │ Definitely equal to │ $a===10 │
├──────┼──── ─────┼──────────┤
│ != │ Not equal to │ $a!=10 │
├─────┼─────── ──┼──────────┤
│ < │ Less than │ $a<9 ────────┤
│                                                                                                                                                                                                                   > ───┤
│ <= │ Less than or equal to │ $a<=10 │
├─────┼─────────┼───────── ─┤
│ >= │ Greater than or equal to │ $a>=1 │
├─────┼──────────┼───────────┤
│ = │ Equality assignment operator│ $a=0  │
├─────┼─────────┼────────────┤
│ += │ Addition specified operator │ $a+=5 │
├─────┼────────┼───────────┤
│ - = │ Subtraction specified operator │ $a-=1 │
├─────┼─────────┼────────────┤
│ *= │ Specified operator for multiplication│ $a*=2                                                                                                                                                                               Division specified operator │ $a/=5 │
├─────┼─────────┼──────────┤
│ %= │ Remainder Specify operator │ $a%=7 │
├─────┼─────────┼───────────┤
│ .= │ String specification operator │ $a.="hello" │
├─────┼─────────┼─────────────┤
│ & │ and │ $a&$b │
├─────┼─────────┼───────────┤
│ | │ or │ $a|$b │
├─────┼──────────┼──────────┤
│ ^ │ Xor │ $a^$ b                                                                                  )│
├─────┼──────────┼───────────┤
│ <<     │ Shift to the left                                                                                  <$b                                                                                                                                                                                                        │ $a>>$b │
├─────┼─────────┼──────────┤
│and or && │ with │$ a and $b or $a&&$b │
├─────┼─────────┼──────────┤
│or|| │ or │$a or $b or $a||$b │
├─────┼─────────┼────────────┤
│ xor │ $a │
└─────┴────────┴─────────┘
┌───┬──────── ────┐
│Symbol│ Meaning │ │
├───┼───────────┤
│ $ │Variable │
├─── ┼────────────┤
│ & │Pointer to variable (added before the variable)│
├───┼──────────── ┤
│-> │ ──┘
Let’s compare it with C language. There is just one more "." operator. Its function is to connect two strings, as in the following example, the displayed result is hello, my baby.
$a="hello,";
$b="my baby. ";
echo $a.$b;
?>
There is another symbol that also makes PHP powerful. This is "$". It is used before a variable, indicating that this is a variable, such as $A, $b, etc. So what is its powerful function? This is the variable of variables.
Example as follows:
$a="go";
$$a="here";
echo $a;
echo $$a;
echo $go;
?>
The displayed result is:
go
here
here
In fact, adding a "$" before a variable means that the variable The content is used as a new variable name. This is unique to PHP and can sometimes simplify the program.
--(to be continued)--




http://www.bkjia.com/PHPjc/315131.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/315131.html

TechArticle

Author: Hua Honglang Text: In the article "Talk about HTML syntax", I have introduced the basic HTML syntax. It is possible to compile a static Web page, but it is very important to dynamically interact information. Such as some...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!