The difference between a static variable and a Java static variable in PHP static

Source: Internet
Author: User
Tags tomcat server
Static statics Properties for Java:

<%!static class Count {    private static int count = 0;    public static int GetCount () {        count++;                return count;    }} %><%out.print (Count.getcount ()); Out.print ("
"); Out.print (Count.getcount ());%>


First time output 1, 2

After the refresh output 3, 4, the refresh will always be incremented.

If you restart the Tomcat server and turn it back on again, Output 1, 2


Static statically property of PHP

 
  "; Echo Count::getcount ();

The first output 1, 2, the refresh will still output 1, 2


Comparison Summary:

1. The static property of Java dies with the exit of the Java Virtual machine, and the static property persists during the Java Virtual machine run, and is shared by all users.

2. The static property of PHP is only for one request (once PHP file execution), the PHP file executes, the static property is immediately extinct, again request (Execute the PHP file again), the static property is recreated, Therefore, PHP has no concept of shared static properties, only valid for a single request.


Add:

In addition, PHP has a static variable concept. A static property is something in a class, and a static variable is something in the method:

function count () {static    $count = 0;//method defines static variable    $count + +;    return $count;} echo count (); echo "
"; echo count (); echo"
"; echo count ();

The first output of 1, 2, 3, after the refresh still output 1, 2, 3, indicating that PHP static variables and static properties, but also only for a single request valid.


Reference:


http://www.zhihu.com/question/35472851

  • Related Article

    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.