Talking about PHP syntax (1)

Source: Internet
Author: User
Tags array learn php learn php programming variables php and php programming range
Author: Chinese Red Wolf
Body:
In the article "talking about HTML grammar," I've covered the basics of HTML syntax. It is important to create a static Web page that dynamically interacts with information. such as the membership of some Web site Queensland member registration, log in all need to run the back-end program. Many web sites use CGI programs mainly in Perl, ASP, Java, PHP writing, and we want to use is PHP. It is completely free of Queensland fees, thanks to those obscure programmers. PHP is similar to the structure of C language, this should be the C language mentioned in the "one learning, everywhere programming." I believe that people who have learned C Queensland language can easily get into PHP. Let's introduce some PHP syntax first. This article is suitable for beginners to learn.
PHP has some differences with C, or it may be more flexible to a certain extent than C language. In the C language, variables are defined before they can be used. and PHP variables do not need to Queensland prior definition, direct use can be. For the type of the variable, automatically generated when the value is assigned. The types of PHP variables are: integers (int), double (double), strings (string), Queensland Array (array), objects (object).
When the integer size exceeds its range, it is automatically converted to a double, whose value ranges from the following table:
┌─────┬─────┬──────┬────────────┐
│ Claim type │ length (bit) │ Length (bytes) │ Value range │
├─────┼─────┼──────┼────────────┤
│int│32│4│-2147483647~2147483647│
├─────┼─────┼──────┼────────────┤
│double│32│4│1.7e-308~1.7e+308│
└─────┴─────┴──────┴────────────┘
A string that is usually represented by "" (double quotes). Can also be expressed as "' (single quotes) as follows:
$a = "ABC";
$b = "abc$a";
$c = ' abc$a ';
$d = "\" Cde\ "";
$e = ' CDE ';
Various variables in PHP are preceded by a "$" in the variable name to differentiate.
Note that the content of the $b is ABCABC, $c content is abc$a, $d content is "CDE", $e content also is "CDE". As you can see, the variable names in the contents of the double quotes are substituted for the Queensland, while the single quotes do not. The contents of double quotes need to be escaped, such as $ application \$, and single quotes are not.
The array syntax in PHP is:
Array name [index]
The index can be a number or a literal. But it's not recommended to use text because it doesn't make much sense. An array is also more flexible than other languages:
<?php
$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]<br>";
}
?>
As you can see, the elements in an array are not necessarily the same type, which is the "Live" section of the PHP array.
Using objects makes it easier for programmers to maintain and make programs easier to read. PHP is much simpler than other languages, and it has only categories (Class), methods (method), attributes (attr Queensland ibute), and Extensions (extendsions).

The previous article is just the data type of PHP, the so-called "sharpening the wood work", playing good PHP to better learn PHP programming.
The expressions and operators in PHP do not differ much from the C language, which is now listed below:
┌─────┬─────────┬──────────┐
│ Symbol │ Operator │ Example │
├─────┼─────────┼──────────┤
│+│ Add │ $a + $b │
├─────┼─────────┼──────────┤
│-│ Subtraction │ $a-$b │
├─────┼─────────┼──────────┤
│*│ multiplication │ $a * $b │
├─────┼─────────┼──────────┤
│/│ Division │ $a/$b │
├─────┼─────────┼──────────┤
│%│ for remainder │ $a% $b │
├─────┼─────────┼──────────┤
│++│ │ $a + + or + + $a │
├─────┼─────────┼──────────┤
│--│ │ $a--or--$a │
├─────┼─────────┼──────────┤
│==│ equals │ $a ==10│
├─────┼─────────┼──────────┤
│===│ is no more than │ $a ===10│
├─────┼─────────┼──────────┤
│!=│ not equal to │ $a!=10│
├─────┼─────────┼──────────┤
│<│ less than │ $a <9│
├─────┼─────────┼──────────┤
│>│ is greater than │ $a >8│
├─────┼─────────┼──────────┤
│<=│ is less than or equal to │ $a <=10│
├─────┼─────────┼──────────┤
│>=│ is greater than or equal to │ $a >=1│
├─────┼─────────┼──────────┤
│=│ equality assignment operator │ $a =0│
├─────┼─────────┼──────────┤
│+=│ addition specified operator │ $a +=5│
├─────┼─────────┼──────────┤
│-=│ subtraction Specifies the operator │ $a-=1│
├─────┼─────────┼──────────┤
│*=│ multiplication specified operator │ $a *=2│
├─────┼─────────┼──────────┤
│/=│ division Specifies the operator │ $a/=5│
├─────┼─────────┼──────────┤
│%=│ remainder specified operator │ $a%=7│
├─────┼─────────┼──────────┤
│.=│ string Specifies the operator │ $a. = "Hello" │
├─────┼─────────┼──────────┤
│&│ and │ $a & $b │
├─────┼─────────┼──────────┤
│| │ or │ $a | $b │
├─────┼─────────┼──────────┤
│^│xor│ $a ^ $b │
├─────┼─────────┼──────────┤
│~│ Non-│~ $a (take 1 complement) │
├─────┼─────────┼──────────┤
│<<│ left shift │ $a << $b │
├─────┼─────────┼──────────┤
│>>│ shift to the right │ $a >> $b │
├─────┼─────────┼──────────┤
│and or &&│ with │ $a and $b or $a&& $b │
├─────┼─────────┼──────────┤
│or or | | │ or │ $a or $b or $a| | $b │
├─────┼─────────┼──────────┤
│xor│xor│ $a xor $b │
├─────┼─────────┼──────────┤
│! │ non-│! $a │
└─────┴─────────┴──────────┘
┌───┬────────────┐
│ Symbol │ Meaning │
├───┼────────────┤
│$│ variable │
├───┼────────────┤
Pointers to │&│ variables (plus in front of variables) │
├───┼────────────┤
│->│ object's methods or properties │
├───┼────────────┤
Element value of │=>│ array │
├───┼────────────┤
│? : │ ternary operator │
└───┴────────────┘
Compare it to the C language. There's just one more "." This one operator. Its function is to connect two strings, as shown in the following example, the result is hello,my baby.
<?php
$a = "Hello,";
$b = "My Baby."
echo $a. $b;
?>
There's also a symbol that makes PHP powerful. This is "$". It is used before the variable, which means that it is a variable, such as $a, $b, and so on. So where does it work? The Queensland is the variable of the variable.
The following example:
<?php
$a = "Go";
$ $a = "here";
echo $a;
echo $ $a;
Echo $go;
?>
Display results as:
Go
Here
Here
In fact, adding a "$" to a variable is to take the contents of the variable as a new variable name. This is unique to PHP and can sometimes simplify the program.
--(to be continued)--


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.