answersLogoWhite

0

A static variable is a variable that retains it's value over multiple calls to the function. When it is defined using the static keyword, that defining value is applied only on the first use. For example:

function foo(){

static $bar = 5;

$bar++;

echo $bar . "\n";

}

foo(); foo(); foo(); foo(); foo();

This code would output the following:

6

7

8

9

10

User Avatar

Wiki User

14y ago

What else can I help you with?

Related Questions

How do you create static variables in PHP?

Use the "static" keyword to declare a static variable within a function like shown below. <?php function fun() { static $variable; static $another_variable = ''; } ?>


How can a PHP static be initialized?

PHP static can only be initialized using a literal or constant. You can not use an expression. You can initialize it to an integer but you may not to another variable.


How do you clear a variable in PHP?

You can unset a variable in PHP by setting it to NULL. You can set a variable to NULL by declaring it normally, or using the function "unset()". Both methods are acceptable, and accomplish the same thing. Examples of both: ---- $variable = NULL; ---- unset($variable); ----


How do you pass a variable by value in php?

In the declaration of the receiving function, you add an ampersand. <?php function myWayCoolFunction( &$params) {.....} $x = array('1','2','3'); myWayCoolFunction($x) ?>


How to display a variable in PHP using echo or print statements?

displaying a variable in php using echo statement? <?php $name="ram"; //declaring and defining the variable echo "$name"; //printing the variable using echo command ?>


How do you do the pass the variable value to the query in same php page?

To pass PHP Variable value in Jquery simpley echo that variable : Eg <script> var jQueryVariable=<?php echo $anyVariable?> </script>


Why you say that no static binding in php?

PHP supports late static binding since version 5.3, which was officially released in June of 2009.


How do you write a function with a variable number of input elements in PHP?

When defining your function, do not put any arguments in the definition (e.g. function myFuntion()). Inside the function, you can use func_num_args() and func_get_arg($number) to get the function's arguments.


How can you use a JavaScript variable in the PHP isset function?

You can't actually do that. There is no direct way to make JavaScript code talk to PHP code, as the two languages are interpreted in different locations. The PHP is interpreted by the server, and the JavaScript is interpreted by the client. This means it's easy enough to transfer data from PHP to JavaScript (by generating the JavaScript with the PHP), but not the other way around. If you're simply looking for a way to see if a JavaScript variable is set (from within the JavaScript itself), that can be done with a line like this one: if(myVariable !== undefined){ /* do stuff */} If you actually want to handle it on the PHP side, one way to do so would be to use additional PHP code when that happens. For example: <?php $jsVars = array(); ?> <script type="text/javascript"> var foo = 'bar'; <?php $jsVars['foo'] = 'bar';?> var yub = 'nub'; <?php $jsVars['yub'] = 'nub';?> </script> ... You can then check to see whether a certain variable has been set by seeing if it's in that array: <?php function jsIsset($varname){ global $jsVars; return array_key_exists($varname, $jsVars); } ?> This however, only works when the JavaScript is generated, not when it's interpreted by the client system. For example, imagine you have a variable that is defined by a JavaScript function that is called from an onclick event. By the time that event happens, the page has already been served and the PHP is done executing. If you want the JavaScript to tell the PHP that a variable is defined, you would need to do it through an AJAX request, which I believe is beyond the scope of this question.


How do you represent a variable in PHP?

Variables in php begin with $ and are declared by the user. $str1 = "wiki"; would store "wiki" into the variable $str1


How can you declare the variable in PHP?

Variables in PHP do not need to be declared like some languages (e.g. JavaScript). This is all that needs to be done to assign a variable: $variable = "Value";


What is a valid variable declaration in PHP?

In PHP, you can declare a variable in several ways.In the global scope, or within a function, you can use:var $variable; // Creates an empty variable$variable = {something}; // declares, then assigns, the variableIn the object scope, a property operates like a variable:public $variable; // Creates an empty propertyprivate $variable; // Same thing, but can't be referenced outside the objectprotected $variable; // Similar, but can only be referenced in the object or any object extended by itpublic/private/protected $variable = {something}; // declares, then assigns a default value to, the property$this->variable = {something}; // if not declared, this will declare the property and assign the value to itYou can also create object properties from outside the object:$object->variable = {something}; // works just like $this->variableFinally, the arguments to a function are implicitly declared:function DoSomething ($variable) { ... } // declares and assigns $variableVariables in PHP are very flexible, mutable things. This is unlike some other languages that rely on strict type declarations for variables and other code constructs.