Some considerations for PHP arrays and fscanf

Source: Internet
Author: User

One of the previous posts mentioned that the array in PHP is not subscript out of bounds, as the following program can be run correctly:

<?phpprint count ($w). "\ n"; $w [3] = "RT"; $w [] = "tt";p rint $w [0]. $w [1]. $w [2]. $w [3]. $w [100]. "\ n";p rint_r ($w);? >

The result is:

0rtttArray (    [3] = RT    [+] = TT)

As can be seen from the input result of Print_r, the array in PHP is actually a "set of mappings", a data structure similar to that of a dictionary in Python and a map in Java. Therefore its subscript does not need to be continuous, can be discrete, even the subscript value can be any data type, not necessarily an integer. The subscript value here is actually equivalent to the key name in the key-value pair. When the current label (key name) does not exist in the array, the default value returned by PHP is the empty string "".

Also pay attention to the FSCANF function in PHP, see the following section of C + + code:

#include <iostream>using namespace Std;int main () {int a[3];for (int i = 0; i < 3; i++) scanf ("%d", a+i); cout < ;< a[0] << "<< a[1] <<" "<< a[2] << endl;return 0;}

If you enter a line from the keyboard: 1 2 3

The program can output the correct result as: 1 2 3

Instead, simply rewrite the program into PHP, as follows:

<?phpfor ($i = 0; $i < 3; $i + +) fscanf (  STDIN, "%d", $a [$i]);p rint $a [0]. " ". $a [1]. " ". $a [2]. "\ n";? >

If the scanf is changed to fscanf, the desired result cannot be obtained. The reason is that PHP fscanf is the input in the "line" as an input unit to parse, so after the input "1 2 3", PHP from the string resolved to the first integer value 1 to assign it to a[0], the string remaining characters will be ignored, the program waits for the next line of input. This is the difference between the fscanf in scanf and PHP in C + +. It is worth mentioning that in PHP, the beginning of the read file function is basically in the "line" unit.

For PHP to be able to enter, there are generally two methods, 1 with the format of the string, such as the above fscanf the second parameter to "%d%d%d". 2 Use the Fgets function to read a line, and then use the Split,int and other functions to parse the string, to get their own data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.