Simple PHP syntax

Source: Internet
Author: User
Tags php foreach
PHP getting started simple syntax

1. the "echo" command tells the server to print a string of characters.

2. Explanation: The "var_dump" function can display the data type of our variables (which will be described later.

3. we can use "memory_get_usage" to obtain the memory consumed by the current PHP.

4. in PHP, eight primitive types are supported, including four scalar types, two composite types, and two special types. PHP is a loose language that does not need to declare the data type of a variable to PHP. PHP automatically converts the variable to an automatic data type, which lowers the threshold for learning PHP to a certain extent.

$ String = "that is ";

Var_dump ($ string );

Echo"
";

$ String = 9494;

Var_dump ($ string );

Echo"
";

?>

Note that when we use the "echo" command to output the Boolean type, if it is "true", the output is "1", and "false", nothing is output.

5. Scientific notation can use lower-case e or upper-case E.

6. the string type can be defined in three methods:Single quotes,Double quotation marksAndHeredoc structure.

When double quotation marks contain variables, the variables are connected with the content in double quotation marks;

When a single quotation mark contains a variable, the variable is output as a string.

7. we can useHeredoc structureTo solve this problem, first use the separator to represent the string (<), and then provide an identifier after "<GODAnd end the string with the provided identifier.

8. resource: resources are created and used by special functions, such as opening files, connecting data, and drawing canvases. You can create, use, and release resources ). Any resources should be released in time when they are not needed. If we forget to release the resources, the system will automatically enable the garbage collection mechanism to recycle the resources after the page is executed to avoid memory consumption.

 

// Use the "fopen" function to open the file. the returned value is the resource type.

$ File_handle = fopen ("/data/webroot/resource/php/f.txt", "r ");

If ($ file_handle ){

// Use the while loop (the loop structure in the subsequent language structure statements will be described in detail) to read the file row by row, and then output the text of each row

While (! Feof ($ file_handle) {// determines whether the last row is reached

$ Line = fgets ($ file_handle); // read a line of text

Echo $ line; // output a line of text

Echo"
"; // Line feed

}

}

Fclose ($ file_handle); // close the file

?>

9. NULL (NULL): NULL is a NULL type and is case insensitive. The NULL type has only one value, indicating that a variable has no value. if it is assigned NULL or has not been assigned a value, or is released by unset (). in these three cases, the variable is considered NULL.

10. constants in PHP are divided into custom constants and system constants.

11. the first parameter "constant_name" is a required parameter. it is a constant name, that is, a flag. the naming rules of constants are the same as those of variables, but note that they do not contain the dollar symbol. The second parameter "value" is a required parameter, which is the value of a constant. The third parameter "case_sensitive" is an optional parameter, which specifies whether it is case sensitive. if it is set to true, it indicates no sensitivity. generally, if the third parameter is not specified, the default value of the third parameter is false.

12. constants are mainly used to avoid repeated definitions and tamper with variable values. When we conduct team development, or when the amount of code is very large, for some of the amount that will not be changed after the first definition, if we use variables, without knowing it, when the same variable name is used, the variable value will be replaced, which will cause the server to execute the wrong task.

In addition, constants can improve the maintainability of the code. For some reason, when the constant value needs to be changed, we only need to modify one place.

13. The system constant is a defined constant in PHP. we can use it directly. common system constants include:

(1) _ FILE _: php program FILE name. It helps us to obtain the physical location of the current file on the server.

(2) _ LINE _: Number of PHP program files. It tells us the number of lines in the current code.

(3) PHP_VERSION: version number of the current parser. It tells us the version number of the current PHP parser. we can know in advance whether our PHP code can be parsed by the PHP parser.

(4) PHP_ OS: name of the operating system for executing the current PHP version. It tells us the name of the operating system used by the server, and we can optimize our code based on the operating system.

14. There are two methods to obtain a constant value. The first is to directly obtain the value using the constant name;

The second is to use the constant () function. It outputs the same effect as directly using a constant name, but functions can dynamically output different constants. it is flexible and convenient to use. the syntax format is as follows:

mixed constant(string constant_name)

The defined () function helps us determine whether a constant has been defined. Its syntax format is:

bool defined(string constants_name)

15. There are two assignment operators in PHP:

(1) "=": assigns the value of the right expression to the number of operations on the left. It copies the expression value on the right to the number of operations on the left. In other words, I first applied a piece of memory for the number of operations on the left, and then put the copied value in the memory.

(2) "&": reference value assignment, meaning that both variables point to the same data. It will make the two variables share a piece of memory. if the data stored in this memory changes, the values of the two variables will change.

$ A = "I learned PHP on MOOC! ";

$ B = $;

$ C = & $;

$ A = "I study PHP on MOOC every day! ";

Echo $ B ."
";

Echo $ c ."
";

?>

("? : ") The ternary operator is also a comparison operator. for an expression (expr1 )? (Expr2) :( expr3). If the value of expr1 is true, the value of this expression is expr2; otherwise, it is expr3.

$ A = TRUE; // A agrees

$ B = TRUE; // B agrees

$ C = FALSE; // C object

$ D = FALSE; // D objection

// By the way, review the ternary operators.

Echo ($ a and $ B )? "Pass": "fail ";

Echo"
";

Echo ($ a or $ c )? "Pass": "fail ";

Echo"
";

Echo ($ a xor $ c xor $ d )? "Pass": "fail ";

Echo"
";

Echo (! $ C? "Pass": "Fail ");

Echo"
";

Echo $ a & $ d? "Pass": "fail ";

Echo"
";

Echo $ B | $ c | $ d? "Pass": "fail ";

?>

16. the string concatenation operator is used to connect two strings. the following string concatenation operators are provided in PHP:

(1) concatenation operator ("."): it returns the string obtained after attaching the right parameter to the left parameter.

(2) join value assignment operator (". ="): It attaches the parameter on the right to the parameter on the left.

17. PHP provides an error control operator "@". for expressions that may encounter errors during running, we do not want to display error messages to customers when errors occur, this is unfriendly to users. Therefore, you can place @ before a PHP expression. any error information that may be generated by this expression is ignored;

If track_error is activated. set in ini). any error information generated by the expression is stored in the $ php_errormsg variable. this variable is overwritten every time an error occurs, therefore, if you want to use it, you must check it as soon as possible.

Note that the error control prefix "@" does not block parsing error information. it cannot be placed before the definition of a function or class, or be used for condition structures such as if and foreach.

18. in PHP, the foreach loop statement is often used to traverse arrays. There are two ways to use it: no subscript and no subscript.

(1) only values, not subscript

 

(2) obtain both the subscript and value.

 Value) {// the task to be executed}?>

$ Students = array (

'20180101' => 'Line-Hu chong ',

'20140901' => 'Lin pingzhi ',

'20140901' => 'quyang ',

'20140901' => 'Ren Yingying ',

'20140901' => 'Ask days ',

'000000' => 'let's reach ',

'20140901' => 'dashboard ',

'000000' => '正 ',

'20140901' => 'yuebu group ',

'20140901' => 'chinance ',

); // The student ID and name of 10 students, which are stored in arrays

// Use the loop structure to traverse the array and obtain the student ID and name

Foreach ($ students as $ v)

{

Echo $ v; // output (print) name

Echo"
";

}

?>

$ Students = array (

'20140901' => array ('line-Line-line-based boundary, "59 "),

'20140901' => array ('Lin pingzhi', "44 "),

'20140901' => array ('quyang', "89 "),

'20140901' => array ('Ren Yingying ', "92 "),

'20140901' => array ('Ask days', "93 "),

'20140901' => array ('Let's go ', "87 "),

'20140901' => array ('dashboard ', "58 "),

'20140901' => array ('founder ', "74 "),

'20140901' => array ('yueda group', "91 "),

'20140901' => array ('chinance', "90 "),

); // The student ID, name, and score of 10 students, which are stored in arrays.

Foreach ($ students as $ key => $ val)

{// Use the cyclic structure to traverse the array and obtain the student ID

Echo $ key; // output student ID

Echo ":";

// Name and score of cyclic output

Foreach ($ val as $ v)

{

Echo $ v;

}

Echo"
";

}

?>

15:38:03

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.