Operators and expressions are the most basic knowledge in learning PHP. This article explains some applications and techniques of operators and expressions in PHP through examples, as well as what needs to be paid attention to. Friends in need can refer to it. one time.
php Operators and expressions
1. Classification of operators
1. By operand Category
1.!true // Unary operator
2.$a $b // Binary operator
3.true ? 1:0 // Ternary operator
2. Classification by operation function
(1) Arithmetic operators
1. , -, x, /, % (remainder)
(2) String operator
1.. // For example: $a = 'abc'.'efg';
(3) Assignment operator
1.= // Simple assignment
2. =, -=, X=, /=, %=, .= // Compound assignment
3. ($a, $a), --( $a--, --$a) // Increment and decrement
4.&($a = 1;$b = &a) //Reference assignment
(4) Comparison operator
1.==, ===(constantly equal to), !=, !===, (not equal to), , =
(5) Logical operators
1.// The ones inside the brackets have higher priority than those outside the brackets
2.&&(and), ||(or), !(not), xor (XOR),
(6) bit operators
1.&(bitwise AND), |(bitwise OR), ~(bitwise NOT), ^(bitwise XOR), >(right shift)
2. Arithmetic operator
% remainder, common usage : 1) Divisibility operation 2) Controlling the value range
Example: Determine whether it is a leap year (one leap year every four years, no leap years, and another leap year after four hundred years)
// %会把两边的数转成整型后在整除 // %两边不能用小数或负数 if ((($year%4 == 0) && ($year%100 != 0)) || $year%400 == 0) echo "闰年"; else echo "平年";
3. Assignment operator
//先自身加10,然后赋值给自己,等价于 $a=$a+10 $a += 10; //前置递增递减,先递增递减再赋值 ++$a //先赋值,再递增递减 $a++ //实例 $a = 10; $b = $a++ $c = --$b 结果:a=11 b=9 c=9
4. Logical operator
xor XOR: The same is false (two true or two false = false), different is true (one true and one false = false)
Tips: Pay attention to the difference with or, two or True = true
Logical operator short circuit
1, && //If one is false, then no operation will be performed later, it must be false
2, || //If one is true, then no operation will be performed later, it must be false True
fopen ("test.php","r") or die("failure");
Tips: There is no short-circuit feature for operator & |
5. Bit operators
Bit operations: convert integers to 32-bit binary, and convert strings to ANSCA codes for processing
The above is the entire content of this article, I hope It will be helpful to everyone’s study.
Related recommendations:
php file_get_contents implements the method of getting the array elements in the file
How to implement append and line break in file_put_contents in PHP
How to implement mortgage calculator in PHP
The above is the detailed content of Detailed introduction to php operators and expressions. For more information, please follow other related articles on the PHP Chinese website!

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.


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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools
