This article will introduce in detail the comments, variables, arrays, constants, and function applications in php. For more information, see
What is the difference between single quotes and double quotes in php? 1. the variables in double quotation marks are parsed and output, while those in single quotation marks are not parsed.
2. the resolution speed of single quotes is faster than that of double quotes.
3. for single quotes, there are only two escape characters \',\\
4. except for the above two escape characters, all other escape characters are output as they are.
5. for double quotes, escape characters include \, \ ', \ ", \ t, \ r, and \ n.
6. apart from the single quotes and double quotes used to declare string variables, there is also the heredoc method
Program code:
// $ Age = 22;
// $ Str1 = 'he is $ age year'; // ''output as is
// $ Str2 = "He is $ age years old this year"; // "" The variable to be parsed is output 22.
// Echo $ str1 ,"
", $ Str2;
Comments in php (1) start.
(2) start.
# This is the shell single line comment style
(3) Multiline comments start with/* and end */.
Notes in php
1. in js, if a statement occupies a single row, it can be left blank at the end of the row;
2. However, in php, you must add points after each line.
Although the last sentence of php is an exception, we strongly recommend that you add
3. for pure php pages,?> Yes.
What's more, for files not directly run but are contained on other pages, it is often recommended not to end?>
This allows faster page execution.
Variables in php 1. there are 8 php variables
2. integer, floating point, Boolean, string, NULL, array, object, and resource type
3. in js, declare the variable with the var variable name [= value], declare the variable in php, and the direct variable name = value;
4. variable naming rules in php
5. the variable name is composed of letters, underscores, numbers, and combinations. And the number cannot start
6. there is a '$' mark in front of the variable in php.
7. echo is not a function, but a syntax structure.
8. you can print out the variables.
9. to print multiple variables, separate them with commas (,).
10. variable value transfer
11. reference and value assignment
15. string type
Variables and constants (1) constants
Variable value can be re-assigned at any time
// $ Age = 22;
// $ Age = 23;
// Echo"
", $ Age;
(2) What are the differences between variables and constants?
1. Their declaration methods are different
2. once 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 they are fixed.
Unset ($ age );
Var_dump ($ age );
4. constants cannot be deregistered.
Unset (PI); // syntax error. logout not allowed
Echo PI;
5. variables have their own Scopes. by default, external variables cannot be accessed within the function,
Constants, once defined, are defined globally or internally in functions.
You can access any part of the page.
(3) naming rules for variables and constants
1. the naming rules of constants are the same as those of variables in terms of syntax.
It can be a combination of letters, numbers, and underscores and cannot start with a number.
2. from the perspective of habits: constants are generally capitalized"
// Define ('sf', 342 );
// Echo SF; // The latest version is case sensitive to the constant name
(4) What types of constant values are allowed?
1. only scalar type (single type) can be assigned to constants;
2. compound elements, such as arrays, cannot be assigned to a constant.
3. if a resource type is assigned to a constant, unexpected errors may occur.
The code is as follows::
The 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 );
Control structure in php (1) any program is inseparable. variables, expressions, and control structures
(2) in php, else if can be written in connection and cannot be written in js. we recommend standard writing, that is, separating esle if.
(3) in php, the scope of variables will not look out of scope like js.
(4) in php, there is a special type of variable called Super global variable. No matter whether you are in a function or inside the class, no matter how deep the code is packaged
You can access this variable.
Php array and js array
(1) two methods for creating arrays in js
(2) in js, the array index will always increase one by one from 0, with no blank files in the middle.
1. var arr = new Array (1, 2, 3, 4 );
2. var arr = [1, 2, 3]
(3) Create an array in php
1. but in php, the array index is very flexible.
2. it can be a number or a string
3. it can even be a combination of numbers and strings.
4. if the index part specifies a digital index
5. another unit does not specify an index.
6. take the largest numeric index value that appeared before the unit and then add 1 as its key value.
Php creates an array as follows:
The code is as follows:
$ Arr = array (1, 2, 3 );
Print_r ($ arr );
// ================================================ =====
$ Arr = array (10 => 'Zhao ', 'adfdssd' => 'qian', 'sun', 'name' => 'Zhang Sanfeng ');
Print_r ($ arr );
7. in php, how to reference the unit value of an array depends on the index
8. index is a digital index.
// Echo $ arr [10];
9. if it is a string index, you must add single quotation marks. if it is not added with single quotation marks, it must be processed in front of a constant.
// Define ('name', 'adfdssd ');
// Echo $ arr [name];
(4) associating arrays and indexed arrays
1. The index can be either a pure number, a string, or a string + digit combination.
2. if the index is a pure number, it is called 'index array ';
3. Otherwise, it is called "join array ";
(5) differences between functions in php and those in js
1. in js, you can declare functions with the same name multiple times.
2. However, in a php page, functions with the same name cannot be performed multiple times.
3. in js, function names are case sensitive.
4. in php, function names are case-insensitive (class methods are also case-insensitive)
5. in php, the number of parameters used to call a function must be the same as that used to declare a function.
6. when a function is declared in a php function, a parameter can have a "default value"
Code display of all the above knowledge points
The code is as follows:
// ================================================ =========== Return the original location
// 2. integer, floating point, Boolean, string, and NULL
$ Age = 22;
$ Weight = 75.23;
$ Name = 'Zhang San ';
$ Money = false;
$ House = null; // equivalent to undifined in js
Echo $ age, $ weight, $ money,
// ================================================ =========== Return the original location
// 10. pass the variable value
$ Age = 22;
$ Nian = $ age; // read the value of $ age and assign it to $ nian.
$ Nian = 24;
Echo $ nian, '----', $ age;
// ================================================ ============
// 11. assign values for reference
$ Money = 10000;
$ Credit = & $ money; // declare the $ credit variable and point the credit pointer to the bucket of the money.
$ Credit = 5000;
Echo $ credit, '--------', $ money;
Unset ($ credit );
Echo $ credit;
// ================================================ ============
// String type
$ Str1 = 'hello ';
$ Str2 = "world ";
Echo $ str1, $ str2 ,"
";
// ================================================ ============
$ Age = 22;
Function t (){
Var_dump ($ age );
}
T ();
Define ('hei', 88.63 );
Function s (){
Var_dump (HEI );
}
S ();
// Note: functions in php cannot be declared repeatedly, and variable packages in functions are strict, so they only take effect in functions. Not going outside
// 5. for the above cases, variables and constants can be used, but we select constants.
// Reason: ROOt is often referenced.
// Second, if the variable is used, $ ROOT = 'a'; it is very likely that the value will be changed during multi-person development.
// It is also difficult to use constants:
// Once defined, constants will not be destroyed.
// Constants are always within the point and cannot be destroyed.
// ================================================ ====================
// In php, the variable name is also variable.
$ Talk = 'hello ';
$ Heat = 'kill you ';
$ Love = 'love ';
Echo $ love ,"
";
$ Action = 'tal ';
$ T = 'action ';
Echo $ t;
// ================================================ ====================
// Advance notice: the variable name, function name, and class name are also variable.
/// The constant name must be changed.
Define ('pi ', 3.14 );
Define ('hei', 342 );
$ Cons = 'pi ';
Echo $ cons ,"
";
Echo constant ($ cons); // constant refers to a constant whose value is treated as the constant name.
// ================================================ ================== Return the original location
// Control structure in php
// Any program is inseparable. variables, expressions, and control structures
If, if/else, if/else if/esle
$ Num = 3;
If ($ num> 2 ){
Echo 'in php, 3 is greater than 2 ',"
";
}
If ($ num> 5 ){
Echo '3 greater than 5 ',"
";
} Else {
Echo '3 not greater than 5 ';
}
//
If ($ nun = 1 ){
Echo 'Today is Monday ';
} Else if ($ num = 3 ){
Echo 'Today is weday ';
} Else {
Echo 'non-two-not-Three ';
}
//// In php, else if can be written together and cannot be written in js
// We recommend that you write the rule, that is, esle if is separated.
// ================================================ ====================
Switch case Statement
$ Num = 3;
Switch ($ num ){
Case 1:
Echo 'Today Monday ',"
";
Break;
Case 2:
Echo 'Tuesday today ',"
";
Break;
Case 3:
Echo 'Today is weday ',"
";
Break;
Default:
Echo 'unknown ';
Break;
}
/*
Assume that a person has 100,000 yuan in cash, and a charge is required every time he passes through the intersection.
The billing rule is that when his cash is greater than 50000, he must pay 5% in cash every time he passes through an intersection. if his cash is less than or equal
If it is equal to 50000, 5000 will be handed in each time. please write a program to calculate the number of times this person can pass through this intersection.
*/
For ($ m = 100000, $ num = 0; $ m> = 5000; $ num ++ ){
If ($ m & gt; 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 variable in php
$ Num = 99;
Function t (){
Echo $ num;
/
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.