Home  >  Article  >  Backend Development  >  PHP operator and process analysis

PHP operator and process analysis

小云云
小云云Original
2018-03-15 13:16:351960browse

This article mainly shares with you the analysis of PHP operators and processes, hoping to help everyone have a clearer understanding of PHP operators and processes.

Comparison Operator


Operator 1 Type Operator 2 Type Result
null or string string Convert NULL to "" for numerical or lexical comparison
bool or null Any other type Convert to bool, FALSE
object object Built-in classes can define their own comparisons. Different classes cannot be compared. Properties of the same class and arrays are compared in the same way (in PHP 4). PHP 5 has its own instructions
string,resource or number string,resource or number Convert strings and resources to numbers, comparing by ordinary math
array array has less The array of members is small. If the key in operand 1 does not exist in operand 2, the array cannot be compared, otherwise the values ​​are compared one by one (see the example below)
object Any other type object is always larger
array Any other type array is always larger

Ternary operator

Expression (expr1) ? (expr2) : (expr3) The value when expr1 evaluates to TRUE is expr2, The value when expr1 evaluates to FALSE is expr3.

Since PHP 5.3, the middle part of the ternary operator can be omitted. The expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE and expr3 otherwise.

Error control operator

PHP supports an error control operator: @. When placed before a PHP expression, any error message that expression may produce is ignored.

If you set a custom error handling function with set_error_handler(), it will still be called, but this error handling function can (and should) call error_reporting(), and this function has @ will return 0.

If activated track_errors feature, any error information generated by the expression is stored in a variable. This variable is overwritten on every error, so check it as early as possible if you want to use it.

<?php
/* Intentional file error */
$my_file = @file (&#39;non_existent_file&#39;) or
    die ("Failed opening file: error was &#39;$php_errormsg&#39;");
// this works for any expression, not just functions:
$value = @$cache[$key];
// will not issue a notice if the index $key doesn&#39;t exist.
?>

Note: The @ operator is only valid for expressions. A simple rule for beginners is: if you can get a value from somewhere, prepend it with the @ operator. For example, you can put it in variables, functions and include before calls, constants, etc. It cannot be placed before the definition of a function or class, nor can it be used in conditional structures such as if and foreach etc.

include

The included file is first searched according to the path given by the parameter. If no directory (only file name) is given, it is searched according to Search in the directory specified by include_path. If the file is not found under include_path, include will finally search in the directory where the calling script file is located and the current working directory. The include construct emits a warning if the file is not found at the end; this is different from require, which emits a fatal error.

When a file is included, the code contained in it inherits the variable scope of the line where the include is located. From that point on, any variables available in the calling file at that line are also available in the called file. However, all functions and classes defined in include files have global scope.

Related recommendations:

Detailed introduction to PHP operators and operators

PHP operator collection

Summary of operators in php

The above is the detailed content of PHP operator and process analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn