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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
