Auto-increment/auto-Increment Variable

Source: Internet
Author: User

I saw an interview question today, which involves a very elementary question, as shown below:

 

Write the output result of the php segment:

<? Php

$ Count = 5;
Function get_count (){
Static $ count = 0;
Return $ count ++;
}
Echo $ count;
+ + $ Count;
Echo get_count ();
Echo get_count ();

?>

 

The answer is:

501

 

 

 

It involves two knowledge points:

1. php variable scope;

2. Auto-increment/auto-increment variables;

 

The interviewer understands the scope of "php variables" well, but he is not sure about "auto-increment/auto-increment variables. The following is a review for your reference:

Auto-increment/Subtraction is divided into front and back, which involves a sequence:

<? Php
$ A = 3;
Echo ++ $ a; // The frontend type. The result is 4 after auto-increment. In this case, $ a is 4.

$ B = 3;
Echo $ B ++; // post-type: Output 3 first, and then auto-increment to 4. At this time, $ B is 4.
?>

 



The principle of auto-Subtraction is the same as that of auto-increment.

 

 

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.