Case 1: output Hello
// demo.php <!doctype html> <html lang="zh_cn"> <head> <title>Hello<title> <meta charset="UTF-8" /> </head> <body> <?php echo 'Hello'; ?> </body> </html>
It can also be written as:
<?php echo 'Hello'; ?>
Case 2: writing PHP code combined with HTML
// demo.php <!doctype html> <html lang="zh_cn"> <head> <title>Hello</title> <meta charset="UTF-8" /> </head> <body> <p>Hello HTML</p> <?php echo 'Hello'; ?> </body> </html>
In conclusion, it is proved that PHP can be used with HTML.
PHP variables and constants
Like other languages, variables and constants are containers for storing data. The definition of variables in PHP is to save variable data; The definition of constant is to save immutable data. According to the book, PHP is a weakly typed language, and variables can be directly assigned without prior declaration. There are two kinds of variable assignment in PHP, one is the default value transfer assignment (that is, another variable is calculated or directly assigned to the variable to be created), and the other is reference assignment.
Value transfer assignment
$demo = 1; // Define the variable age and assign a value of 1 $num = $demo; // Assign the value of num to demo $demo = 100; // Modify the value of demo to 100 echo $num; // The value of output num is still 1
When assigning or calculating a variable, "$" must be added before the variable.
It should be noted that num = demo is a typical error prone place, because many people will not notice that the demo variable will assign its value to num.
To reference assignment, you need to add an "&" before the assigned variable, that is:
$num = &$demo;
The biggest difference between value transfer assignment and reference assignment can be viewed through the following code: Value transfer assignment
$num = 1; $demo = $num; $num = 2; echo $num;
The final output of the above code is 1.
$num = 1; $demo = $num; $num = 2; echo $num;
The final output of the above code is 2.
In general, value transfer assignment is simply to transfer the value of the original variable to another variable, and then the change of the value transferred variable will not affect the value of the value transferred variable again. Reference assignment is like taking the assigned variable as the alias of the assigned variable, but when the assigned variable changes, the value of the assigned variable will also change.
In PHP, it is often necessary to define some symbols and mark some names in the program, such as variable name, function name, class name, method name, etc.
Naming rules for identifiers
- Identifiers can only consist of letters, numbers, and underscores
- An identifier can consist of one or more characters, but must consist of letters or underscores
- When identifiers are used as variable names, they must be case sensitive
- If the identifier consists of multiple words, it is recommended to use the underscore "" to separate it. Such as user_name
In order to dynamically change the name of variables during development, PHP provides a special variable usage: variable variables. Through variable variables, the value of another variable can be used as the name of the variable. The implementation of variable variables is very simple. You only need to add an "$" symbol in front of variables. Namely:
$a = 'a'; $b = 'b'; $c = 'c'; echo $a; // Output a echo $$a; // Output b echo $$$a; // Output c
It should be noted that when the value of a is a number, a variable is used
In PHP, constants can not only save unchangeable values, but also save a constant value in the script. Its characteristic is that it cannot be modified or redefined. For example, PI is a constant whose value is fixed and cannot be changed. The keywords defining constants in PHP are define() and const, but constants are case sensitive. For example:
define('DEMO','hello'); // Define demo constant, but it is not the same data as demo const DEMO = 'hello';
All the above methods are OK.
However, it is worth mentioning that in PHP, the define() function provides a third parameter, which is optional. If retained, the default value of this parameter is false. By modifying this parameter, you can make constants case insensitive. example:
For the third parameter, true means case insensitive, and false means case sensitive.
Constants can not only pass the echo constant name; Output can also be output through another attribute provided by echo: echo constant('constant name '). Through this function, we can also output the value of constants.
Some constants are predefined in PHP to obtain PHP information, which can be directly referenced if necessary.
Variable name Function description PHP_VERSION obtain PHP edition PHP_OS Get resolution PHP Version of operating system PHP_INT_MAX obtain PHP Maximum value of integer type in PHP_INT_SIZE obtain PHP Word length of integer type in E_ERROR Indicates a runtime fatal error E_WARNING Indicates a runtime warning error E_PARSE Represents a parsing error at compile time E_NOTICE Indicates a runtime prompt
The output of constants is very simple, such as outputting PHP version information: echo PHP_VERSION;.
PHP retains some words with special meaning, also known as keywords. Keywords must not be used as constants, function names, and class names. When naming constants, function names and class names, please note: try to avoid keywords.
Case 3: output server information
// demo.php <!doctype html> <html lang="zh_cn"> <head> <title>server information</title> <meta charset="UTF-8" /> </head> <body> <center><h1>server information</h1></center> <a>PHP edition:</a><p><?php echo PHP_VERSION; ?></p> <a>Server operating system:</a><p><?php echo PHP_OS; ?></p> </body> </html>
Since PHP is a weak language, you do not need to specify the type of variable when declaring variables. The type of variable can be changed in the code, such as:
$a = 123; // At this time, the variable a is integer $a = 'hello'; // At this time, the variable a is of string type
In PHP, Boolean data is used for logical judgment. It consists of true and false values, representing "true" and "false" respectively. It is case insensitive. You can assign values to variables directly.
$a = true; $b = false;
Integer data can be expressed in octal, decimal and hexadecimal, and "+" and "-" can be added before the data to represent the positive and negative of the data. Refer to the octal hexadecimal manual for details.
Floating point type can save floating-point numbers and integers. It is generally used to represent decimals. It has two writing methods: standard writing and scientific counting
$a = 1.1111; $b = -1.111; $c = 3.13E5; $d = 1.23E-3;
No matter how the above format is expressed, the significant digits of floating-point decimals are 16.
String type is a character composed of consecutive alphanumeric symbols. In PHP, it is usually identified by single quotation marks and double quotation marks.