Home >Backend Development >PHP Tutorial >Summary of PHP Basics (1)_PHP Tutorial
PHP supports a total of 4 markup styles:
1)
echo "This is an xml style tag"; //Recommended to use
?>
2)
3)
echo "short style tag"; ?>
4)
<%
echo "This is ASP style markup";
%>
/*
If you want to use short style and asp style, you need to configure it in php.ini, and then set short_open_tag and asp_tags to ON
*/
PHP supports three types of comments:
// c++ style single line comments
/* */ c-style multi-line comments
# Shell style comments
/*
Do not appear the ?> sign in a single-line comment, because the interpreter will think that the php script has ended and execute the content after it
*/
Data types in php:
PHP supports a total of 8 primitive types, including 4 scalar types (boolean, integer, float, string)
Two composite types: array, object
Two special types: resource,null
About boolean
The Boolean type has two values: true/false
*In php, not only false is false, 0 0.0 "0" "" arrays that only declare no values are false
About string
There are three ways to define strings: single quotes (recommended), double quotes, and delimiter
The difference between single quotes and double quotes:
1) When using single quotes, you only need to escape the single quotes. When using double quotes, you need to escape characters such as "$
2) If you use single quotes, the content inside will be output as is. If you use double quotes, PHP will spend some time processing string escaping and variable parsing
Delimiter:
$s = <<
str; //No spaces in front, no different from double quotes
About integer
Integer numbers can be expressed in decimal, octal, or hexadecimal. If it is octal, add leading 0. If it is hexadecimal, add 0x
If illegal numbers (8,9) appear in octal, the following numbers will be ignored
If the given value exceeds the maximum range that the int type can represent, it will be treated as a float type. This situation is called integer overflow
About float
The value of floating point numbers is only an approximation. Try to avoid comparisons of floating point numbers because the results are often inaccurate
About null
Not case sensitive,
There are three situations when a null value is assigned: no value has been assigned, a value is assigned to null, and a variable has been processed by the unset() function?
The function to determine whether it is null is is_null(), and the return value is boolean type
Starting from php4, unset() no longer has a return value
Casting:
(boolean) Convert to boolean
(string) ;
(integer) ;
(float) ;
(array) ;
(object) ;
*When converted to boolean, null, 0 and unassigned variables or arrays will be converted to false, and others will be true
When converting to an integer, false is 0 for Boolean type and 1 is true for true. The decimal point of the floating point number will be rounded off. If the character type starts with a number, non-numeric digits will be intercepted. Otherwise, 0
Type conversion can also be completed through the settype() function,
bool settype(mixed var,string type)
The parameter var is the specified variable, the parameter type is the specified type, and there are 7 optional values (boolean, float, integer, array, null, object, string)
The settype function will convert the type of the original variable, while forced type conversion only generates a temporary variable, and the type of the original variable will not change
Functions for monitoring data types:
is_bool() is_string() is_float()/is_double() is_integer()/is_int() is_null() is_array() is_object() is_numeric()
is_numeric() checks whether the variable is a number or a string composed of numbers
php constants:
Use the define() function to define constants:
define(string constant_name,mixed value,case_insensitive)
constant_name constant name
value constant value
case_sensitive is optional, specifies whether it is case sensitive, true means insensitive
There are two ways to get the value of a constant. One is to use the constant name to get the value directly, and the other is to use the constant() function
mixed constant(string const_name) //Return the value of the constant
To determine whether a constant has been defined, you can use the defined() function
bool defined(string constant_name)
PHP predefined constants:
__FILE__ php program file path
__LINE__ The line where this constant is located
PHP_VERSION program version
PHP_OS The name of the operating system that executes the php parser
php variable:
There is reference assignment in PHP. Use different names to access the contents of the same variable. When the value of one variable is changed, the other one will also change. Use the & symbol to represent
$i = "Hello";
$s = &$i;
Variable scope
Local variables are variables defined inside a function, and their scope is the function
Global variables are variables defined outside of all functions. Their scope is the entire php file, but cannot be accessed within user-defined functions. If you want to use global variables within user-defined functions, you must use the global statement
Static variables can retain variable values after the function call is completed
Such as:
$i = 'hello';
function fun(){
global $i;
echo $i; //output hello
}
?>
Mutable variables:
A variable variable is a unique variable that allows the name of a variable to be changed dynamically. The working principle is that the name of the variable is determined by the value of another variable. The implementation process is to add an extra dollar sign in front of the variable
$i = 'abc';
$abc = 'hello';
echo $$i; //hello
www.2cto.com
PHP predefined variables:
$_SERVER['SERVER_ADDR'] The IP address of the server where the script is currently running
$_SERVER['SERVER_NAME'] The name of the server host where the script is currently running
$_SERVER['REQUEST_METHOD'] Request method when accessing the page
$_SERVER['REMOTE_ADDR'] The user ip that is browsing the current page
$_SERVER['REMOTE_HOST'] The host name of the user who is browsing the current page
$_SERVER['REMOTE_PORT'] The port used by users to connect to the server
Excerpted from Youth China Airlines’ column