<?php
$var = "01-01-10";
function checkkkdate($n)
{
global $var;
$var = $n;
}
echo $var;
?>
The output should be what i send in function call
but it is giving output = 01-01-10
Please help
You didn't call your function. Call it before echoing value:
checkkkdate();
echo $var;
You forgot to call the function.
checkkkdate();
echo $var;
Call to the function that changes the global variable is missing.
You need:
checkkkdate();
echo $var;
Related
i need to assign a global variable value by passing it in a function, something like static variable i guess. Here is my code
<?php
//this is old value
$var = "Old Value";
//need to change the value of global variable
assignNewValue($var);
echo $var;
function assignNewValue($data) {
$data = "New value";
}
?>
After the execution the value of var need to be New Value. Thanks in advance.
<?php
//this is old value
$var = "Old Value";
//need to change the value of global variable
assignNewValue($var);
echo $var;
function assignNewValue(&$data) {
$data = "New value";
}
?>
I made the argument of assignNewValue a reference to the variable, instead of a copy, with the & syntax.
You can try it in 2 ways, the first:
// global scope
$var = "Old Value";
function assignNewValue($data) {
global $var;
$var = "New value";
}
function someOtherFunction(){
global $var;
assignNewValue("bla bla bla");
}
or using $GLOBALS: (oficial PHP's documentation: http://php.net/manual/pt_BR/reserved.variables.globals.php)
function foo(){
$GLOBALS['your_var'] = 'your_var';
}
function bar(){
echo $GLOBALS['your_var'];
}
foo();
bar();
Take a look: Declaring a global variable inside a function
Is there a way to return a variable variable from a function?
This is what I tried:
function varvar($num){
$var = "foo".$num;
return $$var;
}
varvar(3);
echo $foo3;
But nothing prints out. Any ideas?
You need to capture the return value in a variable:
function varvar($num){
$var = "foo".$num;
return $$var;
}
$foo3 = varvar(3);
echo $foo3;
so imagine I have this PHP code
<?php
$var = 10;
function foo($var)
{
global $var;
echo $var;
}
foo(2);
?>
The output here is 10. I want to know if there is a way to refer back to the function scope variable $var (which in my example has a value of 2).
Maybe not an exact answer but here is a way:
$var = 10;
function foo($var)
{
$array = get_defined_vars();
global $var;
echo $var;
echo $array['var'];
}
foo(2);
You could also use func_get_arg() or func_get_args(), but whatever you do would need to be before the global statement.
This is blowing my mind...
I've got a standalone PHP file, and a simple function with a global var.
<?php
$var = 4;
function echoVar()
{
echo $var;
}
echoVar();
?>
When I call echoVar() nothing is returned... However if I place the $var inside the function it will return 4.
What's going on here? Shouldn't $var be global in this case?
If a variable is set outside of a function, it's not visible inside that function. To access it, you must declare it global with the global keyword. This is called a scope.
<?php
$var = 4;
function echoVar() {
global $var;
echo $var;
}
echoVar();
Note: This is generally considered bad practice. Read this for more information.
A good alternative would be to pass in the variable as an argument:
<?php
$var = 4;
function echoVar($var) {
echo $var;
}
echoVar($var);
Lots of options here... like
<?php
$var = 4;
function echoVar($var)
{
echo $var;
}
echoVar($var);
?>
or
<?php
$var = 4;
function echoVar()
{
global $var;
echo $var;
}
echoVar();
?>
You can either have $var as an argument, like this:
$var = 4;
function echoVar($var)
{
echo $var;
}
echoVar($var);
or use global, like this:
$var = 4;
function echoVar()
{
global $var;
echo $var;
}
echoVar();
When you call any function it's create local variable so you have to pass argument in calling function part.
$var = 4;
function echoVar($var)
{
echo $var;
}
echoVar($var);
Just going to clarify since everyone seems to be posting mostly rubbish.
Do not use global $var;
Do not echo out inside of a function
Output from a function does not need to be assigned to a variable before being echo'd
This is how it "should" be done.
<?php
$var = 4; //set initial input var this is external to the function
function echoVar($internalvar) { /*notice were accepting $var as $internalvar I'm doing this to clarify the different variables so you don't end up getting confused with scope $internalvar is local to the function only and not accessible externally*/
return $internalvar; //now we pass the function internal var back out to the main code we do this with return you should never echo out your output inside the function
}
echo echoVar($var); //call function and pass $var in as an arguement
?>
I'm trying to make a function which will do what the following statements do....
<?php
if(isset($var)){
echo $var;
}
else {
echo "";
}
?>
I have done this so far....
<?php
function echo_ifset($dyn_var){
$var = $dyn_var;
if(isset($$var)){
global $$var;
echo $$var;
}
}
but its not displaying anything when I run..
echo_ifset('message');
// while message is a defined variable.
If you work with a reference, you won't have any problems with warnings (or errrors, my PHP is a little rusty!) if the variable isn't defined:
function echo_ifset(&$var) {
if (isset($var)) {
echo $var;
};
}
Note the & before the $var declaration, this is the reference operator.
Then, you can just call it using:
echo_ifset($message);
This method is also great if you want to define a method to set a default value:
<?php
function defaultValue(&$var, $default) {
if (!isset($var)) {
return $default;
}
return $var;
}
?>
Some extra reading material can be found at: http://www.php.net/manual/en/language.references.pass.php
You need to echo the returned value of your function:
function ifset($dyn_var) {
if (isset($dyn_var)) {
return $dyn_var;
}
else {
return "";
}
}
And then just use:
echo ifset($var);
You have pass message as a string so does not display it.
You have passed like echo_ifset($message). if message variable is already define.
delete one $ in the if statement:
function echo_ifset($dyn_var){
$var = $dyn_var;
if(isset($var)){
global $$var;
echo $$var;
};
}