Introduction to comments, variables, arrays, constants, and Functions in PHP _php tutorial

Source: Internet
Author: User
Tags case statement switch case
What is the difference between single and double quotes in PHP?
1. As can be seen, the double-quote variable is parsed and output, and the single-quote variable is not resolved.
2. The resolution of single quotation marks is faster than double quotation marks
3. For single quotes, there are only two escapes \ ', \ \
4. In addition to the above two escape characters, the others are output as-is.
5. For double quotation marks, the escaped character is in addition to \\,\ ', \ ', and \ T, \ r, \ n, etc.
6. In addition to single and double quotation marks to declare string variables, there is a heredoc way
Program Code
$age = 22;
$str 1 = ' He $age year old '; ' Output as-is '
$str 2 = "He $age year old"; "" To parse the variables that are inside the output 22
echo $str 1, "
", $str 2;
Comments in PHP
(i) Start with//.
(b) Start with #.
#这是shell单行注释风格
(c) Multi-line annotations There is a one with/* start with */end
Considerations in PHP
I. In JS, a statement if a single row, the end of the line can not add;
Two. However, in PHP Plus, you must add a semicolon after each line
Although the last sentence of the entire PHP exception, it is strongly recommended to add
Three. For pure PHP page,?> can not write
And, for not directly run, but is included in other pages of the file, often also recommended to end without adding?>
This includes pages that execute faster faster
Variables in PHP
1. There are 8 types of PHP variables
2. Integer, floating-point, Boolean, string, NULL, array, object, resource
3. In JS, declare variable with var variable name [= value], declare variable in PHP, direct variable name = value;
4. Variable naming conventions in PHP
5. The name of the variable is composed of "letters", underscores, numbers, and combinations. And the number cannot begin
6. There is a ' $ ' tag in front of the variable in PHP
7. Echo is not a function, it is a grammatical structure.
8. You can print out the variables
9. To print multiple variables, use ', ' to separate
10. The value of the variable
11. Reference value and Assignment pass value
15. String type
variables and Constants
(i) Constants
Variables can be re-assigned at any time
$age = 22;
$age = 23;
echo "
", $age;
(b) What is the difference between a variable and a constant?
1. They are declared in different ways
2. When a constant is declared, its value cannot be changed
Define (' PI ', 3.14);
PI = 3.23; syntax error
echo "
", PI;
3. Variables can be destroyed, and constants cannot be destroyed once the doctrine is
Unset ($age);
Var_dump ($age);
4. Constants cannot be unregistered
Unset (PI); Syntax error, do not allow logoff
Echo PI;
5. The variable has its scope, and the function internally does not access the external variable by default.
and constants, once defined, either in the global definition, or in the function of the intrinsic doctrine.
can be accessed anywhere on the page.
(c) Naming conventions for variables and constants
1. The naming conventions for constants are the same from the syntax and variables.
Allow is a combination of letters, numbers, underscores, and numbers cannot begin
2. From a customary point of view: "Uppercase" for general constants
Define (' SF ', 342);
Echo SF; The latest version is case-sensitive for constant names
(d) What types of constants are allowed?
1. Only scalar type (single type) can be assigned to constants;
2. You cannot assign a compound, such as an array, an object to a constant.
3. If the resource type is assigned to a constant, it can lead to unexpected errors.
The code is as follows
Copy CodeThe code is as follows:
1 define (' age ', 22);
2 define (' HEI ', 343.234);
3 define (' ROOT ', ' d:/www ');
4 define (' LOCK ', true);
5 define (' NON ', NULL);
6 echo age,hei,root;
7 Var_dump (LOCK);
8 Var_dump (NON);

the control structure in PHP
(a) Any procedure, can not do without, variable, expression, control structure
(b) in PHP, else if can be attached to write, in JS do not allow, we recommend the specification of the writing, that is, esle if separated.
(c) in PHP, the scope of the variable does not look like JS, looking out of the scope
(d) in PHP, there is a class of special variables called Super global variables. Whether you're in a function or inside a class, no matter how deep the code wraps,
are able to access the variable.
PHP Arrays and JS arrays
(a) Two methods of creating an array in JS
(b) The index of the array in JS is always incremented from 0, with no gap in the middle.
1. Var arr=new Array (1,2,3,4);
2. var arr= [+]
(iii) Creating an array in PHP
1. But in PHP, the index of the array is very flexible
2. Can be a number, or it can be a string
3. It can even be a mix of numbers and strings
4. If the index part specifies a numeric index
5. Another cell does not have a specified index
6. Then take the largest numeric index value that has ever appeared before the unit and then +1, as its key value
PHP creates an array as follows:
Copy CodeThe code is as follows:
$arr =array (a);
Print_r ($arr);
//===========================================
$arr =array (10=> ' Zhao ', ' adfdssd ' = ' money ', ' Sun ', ' name ' = ' Zhang Sanfeng ');
Print_r ($arr);

7. In PHP, how to reference the cell value of an array, the key depends on the index
8. And the index is a numeric index
echo $arr [10];
9. If it is a string index, you must require a single quotation mark, if it is not enclosed in single quotation marks, it is first handled in the presence of constants
Define (' name ', ' ADFDSSD ');
echo $arr [name];
(iv) Associative arrays and indexed arrays
1. The index can be either pure or string, or it may be a string + number mix
2. If the index is a pure number, it is called an ' indexed array ';
3. Otherwise it is called "associative array";
(v) The difference between a function in PHP and a function in JS
1. In JS, you can declare a function with the same name multiple times
2. However, in a PHP page, you cannot multiple functions with the same name
3. In JS, the function name is case-sensitive
4. In PHP, function names are not case-sensitive (class methods are not distinguished)
5. In PHP, the number of arguments to call a function must be the same as the argument of the declared function
6. In PHP functions, function declarations, a parameter can have a "default value"
The code of all the above points shows
Copy CodeThe code is as follows:
================================================ return
2. Integer, floating-point, Boolean, String, NULL
$age = 22;
$weight = 75.23;
$name = ' Zhang San ';
$money = false;
$house = null; Equivalent to the undifined in JS
Echo $age, $weight, $money,
================================================ return
10. The value of the variable
$age = 22;
$nian = $age;//read out the value of $age and assign it to $nian
$nian = 24;
echo $nian, '----', $age;
//================================================
11. Reference value Assignment value
$money = 10000;
$credit = & $money; Declare the $credit variable and point the credit pointer to the storage space of money
$credit = 5000;
echo $credit, '--------', $money;
Unset ($credit);
Echo $credit;
//================================================
String type
$str 1 = ' Hello ';
$str 2 = "World";
echo $str 1, $str 2, "
";
//================================================
$age = 22;
function T () {
Var_dump ($age);
}
T ();
Define (' HEI ', 88.63);
function S () {
Var_dump (HEI);
}
S ();
Note: The functions in PHP cannot be declared repeatedly, the variables in the function are more tightly wrapped and not function within functions. Doesn't run out of action.
5. For the above situation, use variables, constants are OK, but we choose constants.
Reason: One is root, is often quoted
The second is: if the variable, $ROOT = ' a '; very likely to be changed in the multi-person development process value
There are also bad places with constants:
Constants are not destroyed once they are defined
Constants are always inside and cannot be destroyed.
//=====================================================
In PHP, variable names are also variable.
$talk = ' Hello ';
$heat = ' kill you ';
$love = ' love ';
Echo $love, "
";
$action = ' talk ';
$t = ' action ';
echo $$ $t;
//=====================================================
Advance notice: Not only the variable name, function name is also variable, the class name is also variable.
The constant name also needs to change
Define (' PI ', 3.14);
Define (' HEI ', 342);
$cons = ' PI ';
Echo $cons, "
";
ECHO constant ($cons); Constant is the value of the variable as the name of the constant, the reference constant
===================================================== return
The control structure in PHP
Any program, can not do without, variables, expressions, control structure
If, If/else,if/else If/esle
$num = 3;
if ($num >2) {
Echo ' in PHP, 3 is also greater than 2 ', "
";
}
if ($num >5) {
Echo ' 3 is greater than 5 ', "
";
}else{
Echo ' 3 not greater than 5 ';
}
//
if ($nun ==1) {
Echo ' Today is Monday ';
}else if ($num ==3) {
Echo ' Today is Wednesday ';
}else{
Echo ' Non-one non-two non-three ';
}
In PHP, else if can be attached to write, in JS do not allow
We recommend the specification of the write, i.e. Esle if separated.
//=====================================================
Switch Case statement
$num = 3;
Switch ($num) {
Case 1:
Echo ' Today Monday ', "
";
Break
Case 2:
Echo ' Today Tuesday ', "
";
Break
Case 3:
Echo ' Today is Wednesday ', "
";
Break
Default
Echo ' Don't know ';
Break
}
/*
Suppose someone has 100,000 in cash, and a fee is required for each intersection.
The tariff rule is that when he has more than 50000 cash, he will have to pay 5% cash per crossing, if the cash is less than or equal to
Equal to 50000 per 5000, please write a program to calculate how many times this person can pass this intersection
*/
For ($m =100000, $num =0; $m >=5000; $num + +) {
if ($m >50000) {
$m *=0.98;
}else{
$m-=5000;
}
}
//===========================================
While, Do/while
Print $1-9 with while;
$i = 1;
while ($i <10) {
echo $i + +, "
";
}
$i = 0;
while (+ + $i <10) {
Echo $i, "
";
}
//===========================================
while () {}
$i = 0;
while (+ + $i <10) {
if ($i ==5) {
Break
Continue
}
Echo $i, "
";
}
////===========================================
Super Global Variables in PHP
$num = 99;
function T () {
Echo $num;
//}
In this call procedure, the $num is null because there is no definition inside the function $num
And in PHP, and not like JS, along the scope of the look out.
T ();
///===========================================
In PHP, there is a class of special variables called Super global variables.
Whether you're in a function or inside a class, no matter how deep the code wraps,
are able to access the variable.
function A () {
echo $_get[' title ';
}
A ();
///===========================================
Want to use PHP to make a message book
About logical operators
var age= 2| | 3;
alert (age);
Var_dump ($age);
In PHP, the logical operation returns TRUE/FALSE
$age = 2| | 3;
Var_dump ($age);
$a = 3;
$b = 2;
if ($a =9 | | $b =1) {
$a +=1;
$b +=1;
}
echo $a, "
", $b;
The difference between a function in PHP and a function in JS
1. In JS, a function with the same name can be declared multiple times
However, in a PHP page, you cannot multiple functions with the same name
2. In JS, the function name is case-sensitive
In PHP, function names are not case-sensitive (class methods are not distinguished)
=========================================== return
3. In PHP, the number of arguments to call a function must be the same as the argument of the declared function
$a = 1;
$b = 2;
$c = 3;
function T ($a, $b, $c) {
Echo $a + $b + $c;
}
T (n/a);
T (n);
///===========================================
4. In PHP functions, when a function is declared, a parameter can have a "default value"
function T ($a, $b, $c =0) {
Echo $a + $b + $c;
}
T (n);
?>

http://www.bkjia.com/PHPjc/326288.html www.bkjia.com true http://www.bkjia.com/PHPjc/326288.html techarticle What is the difference between single and double quotes in PHP? 1. As can be seen, the double-quote variable is parsed and output, and the single-quote variable is not resolved. 2. The resolution of single quotation marks is faster than double quotation mark 3 ...

  • Related Article

    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.