php数组与fscanf的一些注意事项
前面的一篇博文提到php中数组是没有下标越界这一说的,如下的程序可以正常运行:
<?phpprint count($w) . "\n";$w[3] = "rt";$w[100] = "tt";print $w[0] . $w[1] . $w[2] . $w[3] . $w[100] . "\n";print_r( $w );?>
结果为:
0rtttArray( [3] => rt [100] => tt)
由上print_r这一句的输入结果中可以看出,php中的数组其实实际上是“映射的集合”,是类似于python中字典和java中Map的一种数据结构。因此它的下标不需要是连续的,可以是离散的,甚至下标值可以是任意数据类型,不一定要是整数。这里的下标值其实就相当于键值对中的键名而已。而当下标(键名)在数组中不存在时,则php默认返回的值是空串""。
另外要注意php中的fscanf函数,看下面一段c++的代码:
#include<iostream>using namespace std;int main(){ int a[3]; for( int i = 0; i <p></p> <p>若从键盘输入一行:1 2 3 </p> 则程序能输出正确结果为:1 2 3 <p>而单纯地将这段程序改写成php,如下:</p> <p></p> <pre name="code" class="sycode"><?phpfor ( $i = 0; $i < 3; $i++ ) fscanf( STDIN, "%d", $a[$i] );print $a[0]. " ". $a[1]. " ". $a[2] . "\n";?>
即scanf改为fscanf后,则不能得到期望的结果。原因是,php中fscanf是把输入中的”一行“作为一个输入单位进行解析的,这样在输入”1 2 3“后,php从该字符串中解析到了第一个整数值1把它赋给a[0]后,字符串中剩余字符就会被忽略,程序等待下一行的输入。这就是c++中scanf和php中fscanf间要注意的区别。值得一提的是,php中开头的读文件函数基本都是以”行“为单位的。
若要让php能顺利进行输入,一般有两种方法,1 用格式化字符串,比如上面将fscanf的第二个参数改为 "%d%d%d"。2 用fgets函数读一行,然后自己利用split,int等函数进行字符串的解析,得到自己想要的数据。

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

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

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.
