PHP variables and constants
This section describes
(1) FEATURES of PHP files
(2) PHP variables
(3) PHP constant
1. PHP file features
- The suffix for the PHP file is.php
- PHP scripts to
<? php
At the beginning,? >
Ending, in which? >
The ending character can be omitted - PHP statements start with semicolons
;
The end of the - PHP files usually contain HTML tags and some PHP script code; PHP can be placed anywhere in the document
- The server wax interface file is typically pure PHP script code
- ** Note: ** files containing PHP scripts must use PHP as the file name, otherwise the PHP script will not parse
- Example 1:
-
Create the file example.html
-
HTML, CSS, related grammar reference: www.w3school.com.cn/h.asp (there is no need to do)
-
<! DOCTYPE html> <html> <head> <meta charset ="UTF-8"> < title > my homepage < / title > < / head > < body > < h2 > this is my page < / h2 > < hr > <? phpecho "PHP code"; ? > </body> </html>Copy the code
-
Saved to run a web root directory, use the browser to visit: http://localhost:8888/example.html, you can see the results as follows, the content of the PHP does not show:
-
Change the file name to example. PHP and refresh the page.
-
2. PHP comments
(1) Single-line comments
// This is a one-line comment
# This is a one-line comment
(2) Multi-line comments
-
/* This is a multi-line comment that can span multiple lines */Copy the code
3. PHP echo statement
- PHP often uses echo to enter the value of a string or variable
- Echo is divided into no parentheses and with parentheses
- Echo prints a string that can contain HTML tags
- Example 2:
- Create the file echo.php
-
<? phpecho "Echo example < br >" ; $a= 2020;echo $a ; echo "<br>" ; echo ($a) ; ?> Copy the code
4. PHP variables
(1) PHP variable rules
- Variables begin with a $sign, followed by the name of the variable
- Variable names must begin with a letter or underscore
- Variable names cannot start with a number
- Variable names can contain only alphanumeric characters and underscores (A-z, 0-9, and _)
- Variable names are case-sensitive (Y is two different variables.)
(2) PHP variable types
- PHP has no command to create variables
- Variables are created the first time they are assigned a value
- PHP is a loosely typed language, so you don’t have to tell variables their data types
- PHP automatically converts a variable to the correct data type based on its value
(3) PHP prints variable types
- Generally used for checking and debugging
- To view the value and type of an expression
var_dump()
- To get the type of a variable or expression
gettype()
is_type
Function to determine whether a variable is of a type, such as is_int, is_double, is_string, etc
5. PHP constant
- Constants are like variables, but they cannot be changed or undone once defined
- PHP USES
define()
The function defines a constant, which takes three arguments:- (1) The first argument defines the name of the constant
- (2) The second argument defines the value of the constant
- (3) The optional third argument specifies whether the constant name is case-sensitive. The default is false(case sensitive). Such as:
define("LOGO","Welcome to Bernadette!" ,true);
echo LOGO;
echo logo;
- Print all of
Welcome to Bernadette!