Home php教程 PHP开发 Excerpted from the PHP Manual [1] – Basics to note

Excerpted from the PHP Manual [1] – Basics to note

Dec 22, 2016 am 10:08 AM

Tianya recently used his free time to read the manual completely, and will post some things on the blog that he thinks we can easily overlook. Not much to say, the first article.

Note: About line breaks Although the actual significance of line breaks in HTML is not great, proper use of line breaks can make HTML code readable and beautiful. PHP will automatically remove a newline after the end character ?> when outputting. This function is mainly designed for embedding multiple pieces of PHP code in a page or containing PHP files without substantial output. At the same time, it also caused some confusion. If a newline is output after the PHP terminator ?>, you can add a space after it, or add a newline in the last echo/print statement.

Note: The end tag of the PHP code segment at the end of the file is not required. In some cases, it is better to omit it when using include() or require(), so that unexpected white spaces will not appear at the end of the file, and it will still be there afterwards. Response headers can be output. It's also convenient when using output buffering, so you don't see the unwanted white spaces generated by include files.

Note: Unlike the other two syntaxes, variables and escape sequences appearing in single-quoted strings will not be replaced by the value of the variable.
【Tianya Note】In other words, variables inside single quotes will not be parsed and will be output as strings.

Characters in a string can be accessed and modified by specifying the zero-based offset of the desired character using curly braces after the string.

$str = 'Hello World!';

echo $str{1}; // Output e

?>

Note: The unset() function allows you to unset a key in an array. Be aware that the array will not be reindexed.

You should always put quotes around array indices expressed as strings. For example, use $foo['bar'] instead of $foo[bar]. But why is $foo[bar] wrong? You may have seen the following syntax in old scripts:




$foo[bar] = 'enemy';
echo $foo[bar];
?>


This is how it works Wrong, but works fine. So why is it wrong? The reason is that there is an undefined constant (bar) in this code instead of a string ('bar' - note the quotes), and PHP may define this constant later, unfortunately you have the same name in your code. It works because PHP automatically converts a bare string (a string without quotes and not corresponding to any known symbol) into a normal string whose value is that bare string. For example, if there is no constant defined as bar, PHP will replace it with ‘bar’ and use that.
Note: Again, in a double-quoted string, it is legal to unquote the index so "$foo[bar]" is legal.

The allowed casts are:




(int), (integer) - converted to integer type
(bool), (boolean) - converted to Boolean type
(float), (double), (real) - Convert to floating point type
(string) - Convert to string
(array) - Convert to array
(object) - Convert to object


Note that spaces and tabs are allowed within brackets

Note : HTML forms do not pass integers, floats or booleans, they only pass strings. To check whether a string is a number, you can use the is_numeric() function.
Note: When the variable $x is not defined, usage such as if ($x) will result in an E_NOTICE level error. Therefore, you can consider using the empty() or isset() function to initialize variables.

Note: Although ! has higher precedence than =, PHP still allows expressions like the following: if (!$a = foo()), in this case the output of foo() is assigned to $a.

The above is excerpted from the PHP manual [1] - the basic knowledge that needs to be paid attention to. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Learn from scratch: Master the basics of the Go language Learn from scratch: Master the basics of the Go language Feb 01, 2024 am 08:45 AM

Starting from Scratch: Introduction to the Basics of Learning Go Language Go language, also known as Golang, is an open source programming language developed by Google. It was released in 2009 and quickly became a popular language, especially in fields such as web development, distributed systems, and cloud computing. The Go language is famous for its simplicity, efficiency, and strong concurrency. Basic syntax 1. Variables and constants In the Go language, variables and constants are typed. Variables can store data, while constants cannot be changed. The declaration format of variables is: v

A must-read for learning MySQL! Detailed explanation of the basic knowledge of SQL statements A must-read for learning MySQL! Detailed explanation of the basic knowledge of SQL statements Jun 15, 2023 pm 09:00 PM

MySQL is an open source relational database management system that is widely used in web application development and data storage. Learning MySQL's SQL language is very necessary for data managers and developers. SQL language is the core part of MySQL, so before learning MySQL, you need to have a full understanding of SQL language. This article aims to explain the basic knowledge of SQL statements to you in detail, so that you can understand SQL statements step by step. SQL is the abbreviation of Structured Query Language, which is used in relational data

What basic concepts do you need to understand to learn canvas? What basic concepts do you need to understand to learn canvas? Jan 17, 2024 am 10:37 AM

What basic knowledge do you need to learn canvas? With the development of modern web technology, using the <canvas> tag in HTML5 for drawing has become a common way. Canvas is an HTML element used to draw graphics, animations and other images, which can be manipulated and controlled using JavaScript. If you want to learn canvas and master its basic knowledge, the following will introduce it to you in detail. HTML and CSS basics: before learning canvas

Beginner's Guide to Go Programming: Basic Knowledge and Practical Applications Beginner's Guide to Go Programming: Basic Knowledge and Practical Applications Jan 23, 2024 am 09:31 AM

Quick Start Go Language Programming: Basic Knowledge and Practice Guide Go language, as an emerging programming language, is favored by developers for its simplicity, efficiency and concurrency. Whether you are a beginner or a developer with some programming experience, this article will take you quickly into Go programming and provide some practical guidelines and specific code examples. 1. Install the Go language environment. To start programming in the Go language, you first need to install the Go language environment on your computer. You can download it from Go official website (https://golang

Some basic knowledge of Yii framework Some basic knowledge of Yii framework Jun 21, 2023 pm 07:07 PM

Yii is a popular object-oriented PHP framework. Its full name is "YesItIs", which means "Yes, it is like this". It is designed to be efficient, fast, secure and easy to use, so it is widely used in the development of large-scale web applications. In this article, we will introduce some basic knowledge of Yii framework to help novices better understand this framework. MVC architecture Yii framework adopts a design pattern based on MVC (Model-View-Controller), which

An in-depth exploration of the basics of Go language programming An in-depth exploration of the basics of Go language programming Mar 05, 2024 am 08:15 AM

"In-depth Discussion on the Basics of Go Language Programming: Analysis of Specific Code Examples" As a fast and efficient programming language, Go language is increasingly favored by programmers and developers. In the process of learning and mastering the Go language, it is crucial to have a deep understanding of its basics. This article will conduct an in-depth discussion on variables, data types, process control, functions, etc., and combine it with specific code examples to help readers better understand and master the basic knowledge of the Go language. Variables and data types In Go language, the declaration and initialization of variables are very

Object-oriented basics of JavaScript Object-oriented basics of JavaScript Sep 02, 2023 am 11:21 AM

JavaScript has grown in popularity in recent years, in part due to the development of libraries that make it easier to create JavaScript applications/effects for those who have not yet fully mastered the core language. While in the past, people generally thought of JavaScript as a basic language and very "sloppy" with no real foundation; this is no longer the case, especially with large-scale web applications and JSON (JavaScript Object Notation) Waiting for the introduction of "adaptation". JavaScript can have all the features offered by an object-oriented language, albeit with some additional work that is beyond the scope of this article. Let's create an object functionmyObjec

Getting Started with Go Language: Basic Concepts of Database Connections Getting Started with Go Language: Basic Concepts of Database Connections Jan 23, 2024 am 08:17 AM

Learn Go language: basic knowledge of connecting to databases, specific code examples are required. Go language is an open source programming language. Its simple and efficient features make it loved and used by more and more developers. During the development process, it is often necessary to establish a connection with the database to perform operations such as reading, writing, updating, and deleting data. Therefore, learning how to connect to a database in Go language is a very important skill. Database driver In the Go language, a database driver is required to connect to the database. At present, the main database drivers of Go language are:

See all articles