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 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

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.
