


Some common pitfalls when switching to PHP development_PHP Tutorial
Some common pitfalls when switching to PHP development
1. strrchr function
The note on the W3School site is as follows:
Thestrrchr() function finds the last occurrence of a string within another string and returns all characters from that position to the end of the string.
If successful, return false otherwise.
Actually, this function searches for a certain character, not a string. You should refer to the official documentation
Code example:
$a = 'abcdef.txt'; $b = '.php'; echo strrchr($a, $b);
In other words, if $b is a string, only the first character will be used, and other subsequent characters will be ignored
Note: PHP provides the strstr function, why not provide the strrstr function, although it is very simple to implement it yourself
2, null and empty, 0, comparison of three values
In PHP, == will first perform type conversion and then compare, while === will first compare types. If the types are different, unequal will be returned directly. Refer to the following example
$a = null; $b = ''; $c = 0; echo ($a == $b)?1:0; // 输出1 echo ($a === $b)?1:0; // 输出0 echo ($a == $c)?1:0; // 输出1 echo ($a === $c)?1:0; // 输出0 echo ($b == $c)?1:0; // 输出1 echo ($b === $c)?1:0; // 输出0
3. Reference assignment in foreach , please refer to the official documentation
This reference assignment is very good. For me who uses C#, it is impossible to modify the foreach element in C#, and an exception will occur. PHP makes this possible, but:
There is a warning in the official documentation: Warning The $value reference of the last element of the array will still be retained after the foreach loop. It is recommended to use unset() to destroy it.
Let’s look at a set of code:
$a = [1,2,3]; foreach($a as &$item){ echo $item . ','; } //unset($item); // 引用赋值后不销毁对象 foreach($a as $item){ echo $item . ','; }
1,2,3,1,2,2 The last output is 2 instead of 3. This is because $item is not destroyed in the code. The reason is as follows:
In the first loop, the reference of 3 is assigned to $item. In the second loop, 1 is assigned to $item. Because $item is a reference, element 3 of the array becomes 1. Do you understand?
4. The connection and difference between isset and empty, isset document empty document
empty returns true in the following 8 situations:
null, empty string "", string 0 "0", empty array, Boolean value false, number 0, floating point number 0.0, defined with var in the class but not assigned
isset detects whether the variable is set and is not NULL, but for the 8 cases of empty, only null returns false, and the other 7 cases return true
In summary, except for the 7 non-null situations described by empty, in other cases, if(empty(variable)) is equivalent to if(!isset(variable))

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

In recent years, the template engine in PHP programming has become an important part of PHP development, making it easier for programmers to develop and manage pages. This article will introduce common template engines in PHP programming. SmartySmarty is a commonly used PHP template engine. It supports a series of functions such as cached templates, plug-in modules and custom functions. Smarty's syntax is very flexible and can solve the problem of combining PHP variables with HTML tags, making the PHP language more suitable for templated design. Moreover, S

Common application scenarios of the position attribute in H5 development require specific code examples. In H5 development, the position attribute of CSS is very important. It controls the positioning of elements in the web page. By properly applying the position attribute, we can achieve flexibility and beauty in page layout. In this article, we will introduce common application scenarios of the position attribute and illustrate them with specific code examples. Static (static positioning): The default value of the position attribute is st

In-depth understanding of Ajax controls: What are the common ones? Introduction: In modern Web development, Ajax (Asynchronous JavaScript and XML) has become a popular technology, which can realize partial refresh of web pages and improve user experience. In development, we usually use Ajax controls to simplify and speed up our development process. This article will take an in-depth look at Ajax controls and introduce some common controls and their functions. 1. jQueryAjax: jQueryA

Super global variables are a very important concept in PHP. They can access variable values anywhere in the program without using functions or other methods to pass variables. In this article, we will discuss several super global variables commonly used in PHP programming. $_GET$_GET is one of the super global variables used to collect data submitted by HTML forms. Through $_GET, we can obtain the query string parameters in the specified URL. These parameters can be used for operations such as data filtering or data query on the page. For example, when

PHP is an open source, general-purpose scripting language that is widely used in the field of web development. In PHP programming, conditional statements are one of the indispensable basic syntaxes, used to implement various logical judgments and flow control in the program. This article will introduce common conditional statements in PHP programming. 1. If statement The most commonly used conditional statement in PHP is the if statement. The syntax of the if statement is as follows: if (conditional expression) {//statement to be executed when the condition is true} where the conditional expression can be anything

Common HTML block-level elements: A brief introduction to commonly used block-level elements in HTML, specific code examples required HTML (Hypertext Markup Language) is a markup language used to create the structure and content of web pages. It consists of various tags, of which block-level elements are a common type. Block-level elements refer to elements that occupy an entire line in an HTML document and are displayed in new lines. There are many common HTML block-level elements. Below we will introduce several commonly used block-level elements and provide corresponding code examples. <div>yuan

A preliminary study on built-in objects: Understanding common built-in objects and their application scenarios Introduction: In JavaScript, built-in objects refer to predefined objects built into JavaScript. These objects can be used directly in code without additional references or imports. Understanding common built-in objects and mastering their usage will greatly improve our efficiency in JavaScript development. This article will introduce some common built-in objects and provide specific code examples to help readers better understand the application scenarios of these objects.

REST (RepresentationalStateTransfer) is a design style that allows clients to perform CRUD operations (create, read, update, delete) on the server through HTTP (or HTTPS). In PHP programming, REST API is a common way to help developers provide external API interfaces for websites or applications so that other developers or users can access and use your data, services or functions.
