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
Use the "static" keyword to declare a static variable within a function like shown below. <?php function fun() { static $variable; static $another_variable = ''; } ?>
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.
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); ----
In the declaration of the receiving function, you add an ampersand. <?php function myWayCoolFunction( &$params) {.....} $x = array('1','2','3'); myWayCoolFunction($x) ?>
displaying a variable in php using echo statement? <?php $name="ram"; //declaring and defining the variable echo "$name"; //printing the variable using echo command ?>
To pass PHP Variable value in Jquery simpley echo that variable : Eg <script> var jQueryVariable=<?php echo $anyVariable?> </script>
PHP supports late static binding since version 5.3, which was officially released in June of 2009.
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.
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.
Variables in php begin with $ and are declared by the user. $str1 = "wiki"; would store "wiki" into the variable $str1
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";
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.