Why does my code report an error when I want to create an image counter?-php Tutorial

Source: Internet
Author: User
I want to create an image Counter. Why does my code report an error and follow the video learning process?
The following describes the preparation of the image counter and the text counter.
If (! @ $ F = fopen ("num.txt", "r ")){
Echo "the file does not exist ";
$ Num = 0;
} Else {
Fgets ($ f, 10 );
Fclose ($ f );
}
$ Num ++;
$ Ff = fopen ("num.txt", "w ");
Fwrite ($ ff, $ num );
Fclose ($ ff );
Echo $ num;
?>
------------------------------------------------
The first normal operation, you can create a new num.txt file, and the counter jumps to 1
However, if you run it again, the number 2 should be displayed.
But it is shown as follows:
(! ) SCREAM: Error suppression ignored
(! ) Notice: Undefined variable: num in C: \ wamp \ www \ PHP100 \ PHP07-GraphicalCounter.php on line 42
Call Stack
# Time Memory Function Location
1 0.0006 253472 {main} () .. \ PHP07-GraphicalCounter.php: 0
1


Reply to discussion (solution)

Use the file_exists function to determine whether a file exists.
Does not exist: create a file and write 1
Yes: open the file, read the file content, add 1, and write

If (! @ $ F = fopen ("num.txt", "r ")){
Echo "the file does not exist ";
$ Num = 0;
} Else {
$ Num = fgets ($ f, 10 );
Fclose ($ f );
}
$ Num ++;
$ Ff = fopen ("num.txt", "w ");
Fwrite ($ ff, $ num );
Fclose ($ ff );
Echo $ num;

If (! @ $ F = fopen ("num.txt", "r ")){
Echo "the file does not exist ";
$ Num = 0;
} Else {
$ Num = fgets ($ f, 10 );
Fclose ($ f );
}
$ Num ++;
$ Ff = fopen ("num.txt", "w ");
Fwrite ($ ff, $ num );
Fclose ($ ff );
Echo $ num ......
Thank you! Problem changed
However, it is normal to be 1, 2, 3, 4, 5, 6
Mine is 1, 3, 5, 7, 9,

My test is normal and I don't know how you test it. Have you refreshed twice?

  

I am here to learn

My test is normal and I don't know how you test it. Have you refreshed twice?
No. I can't double-click it again if I click it once.
Is the increase of an even or odd number.
Another question is how to enable EclipsePHP Studio 3 to support Chinese naming.
I use a Chinese version, but an error is reported when the folder or file name contains Chinese characters. it is easy to use an English name.

Why should we name it in Chinese? Find yourself in trouble ..

Why should we name it in Chinese? Find yourself in trouble ..
Files are not needed, but folders generally prefer Chinese characters...
Please Jordan step: http://bbs.csdn.net/topics/390432008

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.