Introduction to comments, variables, arrays, constants, and functions in php

Source: Internet
Author: User
Tags switch case

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, "<br/>", $ 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 "<br/>", $ 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 "<br/>", 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::
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 );

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:
Copy codeThe 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
Copy codeThe Code is as follows:
<? Php
// ================================================ =========== 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, "<br/> ";
// ================================================ ============
$ 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, "<br/> ";
$ 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, "<br/> ";
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', "<br/> ";
}
If ($ num> 5 ){
Echo '3 greater than 5', "<br/> ";
} 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', "<br/> ";
Break;
Case 2:
Echo 'tuesday ', "<br/> ";
Break;
Case 3:
Echo 'Today is weday', "<br/> ";
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 ++, "<br/> ";
}
$ I = 0;
While (++ $ I <10 ){
Echo $ I, "<br/> ";
}
// ================================================ =====
While (){}
$ I = 0;
While (++ $ I <10 ){
If ($ I = 5 ){
Break;
Continue;
}
Echo $ I, "<br/> ";
}
/// =================================================== ========
// Super global variable in php
$ Num = 99;
Function t (){
Echo $ num;
//}
//// During this call, $ num is null because $ num is not defined in the function.
/// In php, it does not look out of scope like js.
T ();
/// =================================================== ======
// In php, a special variable is called a super global variable.
// No matter whether you are in the function or inside the class, no matter how deep the code is packaged
// All variables can be accessed.
Function (){
Echo $ _ GET ['title'];
}
A ();
/// =================================================== ======
// Want to use php as a message book
// 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, "<br/>", $ B;
// Differences between functions in php and those in js
// 1. In js, you can declare functions with the same name multiple times.
// However, In a php page, functions with the same name cannot be performed multiple times.
// 2. In js, the function name is case sensitive.
// In php, function names are case-insensitive (class methods are also case-insensitive)
/// =================================================== ===== Return
// 3. in php, the number of parameters used to call a function must be the same as that used to declare a function.
$ A = 1;
$ B = 2;
$ C = 3;
Function t ($ a, $ B, $ c ){
Echo $ a + $ B + $ c;
}
T (1, 2, 3 );
T (1, 2 );
/// =================================================== ======
// 4. When declaring a function in a php function, a parameter can have a "Default Value"
Function t ($ a, $ B, $ c = 0 ){
Echo $ a + $ B + $ c;
}
T (1, 2 );
?>

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.