Folks,
Check this why I get undefined variable $z error when I write this: return $z.
[code]
<?php
//Php holds all ‘Global Variables’ in an array. Format: $GLOBALS[index]; The ‘index’ holds the name of the variable.
$x = 1; //’Global Variable’ as outside function. It now exists in “Super Global” Variable array as: $GLOBALS[‘x’].
$y = 2; //’Global Variable’ as outside function. It now exists in “Super Global” Variable array as: $GLOBALS[‘y’].
function addition_4()
{
$GLOBALS[‘z’] = $GLOBALS[‘x’] + $GLOBALS[‘y’]; //A new “Global Variable” ‘$z’ is created inside the function.
return $z; //This shows ‘Undefined variable’ error. Have to write like this to not see error: return $GLOBALS[‘z’].
}
echo addition_4();
echo $z; //This gets echoed. Doesn’t show error. So, why return $z above shows error ?
?>
Did not $z get defined here:
**$GLOBALS[‘z’] = $GLOBALS[‘x’] + $GLOBALS[‘y’];
This following code is ok as I write:
**return $GLOBALS[‘z’];
And not write:
**return $z.
[code]
<?php
//Php holds all ‘Global Variables’ in an array. Format: $GLOBALS[index]; The ‘index’ holds the name of the variable.
$x = 1; //’Global Variable’ as outside function. It now exists in “Super Global” Variable array as: $GLOBALS[‘x’].
$y = 2; //’Global Variable’ as outside function. It now exists in “Super Global” Variable array as: $GLOBALS[‘y’].
function addition_3()
{
$GLOBALS[‘z’] = $GLOBALS[‘x’] + $GLOBALS[‘y’]; //A new “Global Variable” ‘$z’ is created inside the function.
return $GLOBALS[‘z’];
}
echo addition_3();
?>
> **Description**
> An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array.
$GLOBALS
$GLOBALS
[code=php]
$x = 4;
$y = 5;
function test() {
global $x, $y;
return $x * $y;
}
echo test();
// 20
[/code]
<i>
</i>$x = 4;
$y = 5;
function test2($a, $b) {
return $a * $b;
}
echo test2($x, $y);
// 20
<i>
</i>global $x.
<i>
</i>$GLOBALS['x'].
<i>
</i>function custom_function()
{
code
}
<i>
</i>function custom_function($new)
{
code
}
<i>
</i>function custom_function($new=NULL)
{
code
}
<i>
</i>custom_function()
<i>
</i>custom_function($var)
<i>
</i>custom_function(1)
<i>
</i>custom_function($var)
<i>
</i>custom_function(&$var).
<i>
</i><?php
$x = 1;
function function_7(&$new=NULL)
{
global $x;
echo $y = $new+=$new;
}
function_7($x);
echo $x;
<i>
</i>
$var_2 = '2'; //This is global variable as it is declared outside functions.
function new_function_1() //Since no global variable is passed inside the function, on this line through the function definition braces, will have to mention the global variable inside the function. Like format: $GLOBALS['var_name'].
{
echo 'Value is: ' .$GLOBALS['var_2'] .'!'; //This line wouldn't have been necessary had the global variable $var_2 been passed as a parameter on the function definition's braces. Like so: function function_name($var);
}
new_function_1(); //Echoing the function name (function call) is NOT necessary since AN echo is mentioned inside the function.
<i>
</i>$var_2 = '2'; //This is global variable as it is declared outside functions.
function new_function_2() //Since no global variable is passed inside the function, on this line through the function definition braces, will have to mention the global variable inside the function by adding 'global' prior to it. Like so: global $var.
{
$string = 'Value is: ' .$GLOBALS['var_2'] .'!'; //This line wouldn't have been necessary had the global variable $var_2 been passed as a parameter on the function definition's braces. Like so: function function_name($var);
return $string;
}
echo new_function_2(); //Echoing the function name (function call) is necessary since no echo is mentioned inside the function and the returned value has to be echoed on this line here.
<i>
</i><?php
//This is template. All codes done from memory. No copying or copy & pasting from tutorials or anywhere like forums. No borrowed codes. Can use these codes 'AS IS' by copying into my business php scripts.
?>
<?php
//Tutorials from: https://www.tutorialspoint.com/php/php_functions.htm
?>
<?php
//STEP 1: No passing of global variables to inside a function as it's function parameter. No function function_name($var) but function function_name().
//Eg 1a.
//NOTE: Difference between Eg1 & Eg2 is that former does not pass global variable as function parameter for variable to work inside the function and so it is needed to delcare the global variable as 'global' from inside the function. Whereas with eg2 the global variable is passed into the function, as a function parameter, to work inside the function and so no need to declare the global variable as "global' from inside the function.
$var_1 = '1'; //This is global variable as it is declared outside functions.
function new_function_1() //Since no global variable is passed inside the function, on this line through the function definition braces, will have to mention the global variable inside the function by adding 'global' prior to it. Like so: global $var.
{
global $var_1; //This line wouldn't have been necessary had the global variable $var_1 been passed as a parameter on the function definition's braces. Like so: function function_name($var);
echo "Value is $var_1!"; //Echoing the function name (function call) is NOT necessary since AN echo is mentioned inside the function.
}
new_function_1();
//Eg 1b.
$var_2 = '2'; //This is global variable as it is declared outside functions.
function new_function_1() //Since no global variable is passed inside the function, on this line through the function definition braces, will have to mention the global variable inside the function. Like format: $GLOBALS['var_name'].
{
echo 'Value is: ' .$GLOBALS['var_2'] .'!'; //This line wouldn't have been necessary had the global variable $var_2 been passed as a parameter on the function definition's braces. Like so: function function_name($var);
}
new_function_1(); //Echoing the function name (function call) is NOT necessary since AN echo is mentioned inside the function.
//Eg 1c.
$var_2 = '2'; //This is global variable as it is declared outside functions.
function new_function_2() //Since no global variable is passed inside the function, on this line through the function definition braces, will have to mention the global variable inside the function by adding 'global' prior to it. Like so: global $var.
{
$string = 'Value is: ' .$GLOBALS['var_2'] .'!'; //This line wouldn't have been necessary had the global variable $var_2 been passed as a parameter on the function definition's braces. Like so: function function_name($var);
return $string;
}
echo new_function_2(); //Echoing the function name (function call) is necessary since no echo is mentioned inside the function and the returned value has to be echoed on this line here.
//Eg2.
//NOTE: Difference between Eg1 & Eg2 is that former does not pass global variable as function parameter for variable to work inside the function and so it is needed to delcare the global variable as 'global' from inside the function. Whereas with eg2 the global variable is passed into the function, as a function parameter, to work inside the function and so no need to declare the global variable as "global' from inside the function.
$var_2 = '2'; //This is global variable as it is declared outside functions.
function new_function_2($parameter)
{
echo "Value is $parameter!";
}
new_function_2($var_2); //Echoing the function name (function call) is NOT necessary since AN echo is mentioned inside the function.
//Eg3.
function new_function_3($parameter=NULL) //Best to add '$param=NULL' ('$var=NULL') so if by mistake no parameters are passed then get no errors on this line for missing parameter.
{
echo "Value is $parameter!";
}
new_function_3(); //Echoing the function name (function call) is NOT necessary since AN echo is mentioned inside the function.
?>
<?php
//STEP 2: Can use strings as function names when calling the function.
function new_function()
{
echo 'Hello!';
}
$function_name = 'new_function'; //Function name is a string.
echo $function_name(); //Function is getting called by identifying the function name from a string.
?>
<?php
//STEP 3: Passing Variables from outside function to inside function as function parameters. Passing via function braces: function function_name($param_1,$param_2);
//Eg1: Difference between eg1 & eg2 is latter uses 'return'.
function combine_values_1($value_1,$value_2) //The 2 values, mentioned inside the function definition braces on the function call line, get added to these two variables mentioned inside the function definition braces.
{
$sum = $value_1+$value_2;
echo 'The Sum of two Values is: '.$sum; //Due to this echo, no need to echo the function call, like so: echo
}
//NOTE: No need to echo the function call since an echo has been made from inside the function definition. No need to write like this: echo combine_values_1(1,2). Only need to echo the function call if the final result, inside the function definition, is not echoed from inside the function defition.
combine_values_1(1,2); //The 2 values, mentioned inside the braces, get added to the two variables mentioned inside the function definition braces.
//Eg2.
//Difference between eg1 & eg2 is latter uses 'return'.
//Difference between eg2 & eg3 is latter echoes the function call to get the function's return value. While former adds the function call to a variable and echoes that variable. Note on eg2 & eg 3 how the functions have been called.
function combine_values_2($value_1,$value_2) //The 2 values, mentioned inside the function definition braces on the function call line, get added to these two variables mentioned inside the function definition braces.
{
$sum = $value_1+$value_2;
return $sum;
}
$new_value = combine_values_2(1,2); //The 2 values, mentioned inside the braces, get added to the two variables mentioned inside the function definition braces.
echo 'The Sum of two Values is: '.$new_value; //Echoing the variable name, that holds the function name (function call) as it's value, is necessary since no echo is mentioned inside the function and the returned value has to be echoed on this line here.
//Eg3.
//Difference between eg2 & eg3 is latter echoes the function call to get the function's return value. While former adds the function call to a variable and echoes that variable. Note on eg2 & eg 3 how the functions have been called.
function combine_values_3($value_1,$value_2) //The 2 values, mentioned inside the function definition braces on the function call line, get added to these two variables mentioned inside the function definition braces.
{
$sum = $value_1+$value_2;
return $sum;
}
//Echoing the function name (function call) is necessary since no echo is mentioned inside the function and the returned value has to be echoed on this line here.
echo 'The Sum of two Values is: '.combine_values_3(1,2); //The 2 values, mentioned inside the braces, get added to the two variables mentioned inside the function definition braces.
?>
<?php
//STEP 4A
//'&' Examples. Original Variable value changes when adding '&' prior to variable name inside function braces.
//Eg 1.
$original = 2;
echo '$original= ' .$original; echo '<br>'; //echoes: 2.
function change_var_1(&$new) //Added '&' prior to variable name inside function braces. Now original value of variable will change.
{
$new = 1; //$new value is forced to '1'. And not added by '1' unlike Eg2.
}
change_var_1($original); //The value of the variable, mentioned inside the braces, get added to the variable mentioned inside the function definition braces.
echo '$original= ' .$original; echo '<br>'; //echoes: 1. Note: Variable value ($original) has changed.
//Eg2.
$original = 2;
echo '$original= ' .$original; echo '<br>'; //echoes: 2.
function change_var_2(&$new)
{
$new += 1; //$new value is NOT forced to '1'. But is added by '1' unlike Eg1.
}
change_var_2($original); //The value of the variable, mentioned inside the braces, get added to the variable mentioned inside the function definition braces.
echo '$original= ' .$original; echo '<br>'; //echoes: 3. Note: Variable value ($original) has changed.
?>
<?php
//STEP 4B
//nO '&' Examples. Original Variable value not changes due to not adding '&' prior to variable name inside function braces.
//On following 2 examples, original value of variable ($original) will NOT change.
//Eg 1.
$original = 2;
echo '$original= ' .$original; echo '<br>'; //echoes: 2.
function change_var_3($new) //Not added '&' prior to variable name inside function braces. Now original value of variable will NOT change.
{
$new = 1; //$new value is forced to '1'. And not added by '1' unlike Eg2.
}
change_var_3($original); //The value of the variable, mentioned inside the braces, get added to the variable mentioned inside the function definition braces.
echo '$original= ' .$original; echo '<br>'; //echoes: 2. Note: Variable value ($original) has NOT changed.
//Eg2.
$original = 2;
echo '$original= ' .$original; echo '<br>'; //echoes: 2.
function change_var_4($new)
{
$new += 1; //$new value is NOT forced to '1'. But is added by '1' unlike Eg1.
}
change_var_4($original); //The value of the variable, mentioned inside the braces, get added to the variable mentioned inside the function definition braces.
echo '$original= ' .$original; echo '<br>'; //echoes: 2. Note: Variable value ($original) has NOT changed.
?>
<?php
//STEP 5: Checks if a function() exists or not. Checks both built-in functions and user defined functions.
$functions = array("function_exists","in_array");
$i = 0;
foreach($functions as $function)
{
if(function_exists($functions[$i]))
{
echo "$function " .'exists!';
}
}
?>
<i>
</i><?php
$x = 1;
$y = 2;
function addition()
{
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y']; //A new "Global Variable" '$z' is created inside the function.
}
addition();
echo $z; //Since '$z' is present within the $GLOBALS[index] array. It is accessible outside the function.
?>
<i>
</i><?php
$developer_web = 1;
$nogdog = 2;
function addition()
{
$GLOBALS['sempervivum'] = $GLOBALS['developer_web'] + $GLOBALS['nogdog']; //A new "Global Variable" '$sempervivum' is created inside the function.
}
addition();
echo $sempervivum; //Since '$sempervivum' is present within the $GLOBALS[index] array. It is accessible outside the function.
?>
<i>
</i><?php
$developer_web = 1;
$nogdog = 2;
function addition()
{
$GLOBALS['sempervivum'] = $GLOBALS['developer_web'] + $GLOBALS['nogdog']; //A new "Global Variable" '$sempervivum' is created inside the function.
}
addition();
echo $sempervivum; //Since '$sempervivum' is present within the $GLOBALS[index] array. It is accessible outside the function.
?>
<i>
</i><?php
//Php holds all 'Global Variables' in an array. Format: $GLOBALS[index]; The 'index' holds the name of the variable.
$developer_web = 4; //'Global Variable' as outside function. It now exists in "Super Global" Variable array as: $GLOBALS['x4'].
$nogdog = 4; //'Global Variable' as outside function. It now exists in "Super Global" Variable array as: $GLOBALS['y4'].
function addition_4()
{
$GLOBALS['sempervivum'] = $GLOBALS['developer_web'] + $GLOBALS['nogdog']; //A new "Global Variable" '$sempervivum' is created inside the function.
return $sempervivum; //WHY I GET ERROR: "Notice: Undefined variable: z4 in ...." ? Is it because $sempervivum is a global variable that I am trying to call from within this function ? I should only call local variables inside this function ? If so, then how to make this local variable or how to create another variable, a local one, based on this global variable ?"
}
echo addition_4();
?>
<i>
</i><?php
$developer_web = 1;
$nogdog = 2;
function addition()
{
return $GLOBALS['sempervivum'] = $GLOBALS['developer_web'] + $GLOBALS['nogdog']; //A new "Global Variable" '**$sempervivum**' is created inside the function.
}
echo addition();
echo $sempervivum; **//Since '$sempervivum' is present within the $GLOBALS[index] array. It should be RETURNED outside the function.**
?>
global
$GLOBALS
<i>
</i>$x = 'vitsusa';
$y = nogdog;
function test2($a, $b) {
return $a * $b;
}
echo test2($x, $y);
<i>
</i><?php
$developer_web = 1;
$nogdog = 2;
function addition()
{
$GLOBALS['sempervivum'] = $GLOBALS['developer_web'] + $GLOBALS['nogdog']; //A new "Global Variable" '$sempervivum' is created inside the function.
}
addition();
echo $sempervivum; //Since '$sempervivum' is present within the $GLOBALS[index] array. It is accessible outside the function.
?>
<i>
</i><?php
//Php holds all 'Global Variables' in an array. Format: $GLOBALS[index]; The 'index' holds the name of the variable.
$developer_web = 4; //'Global Variable' as outside function. It now exists in "Super Global" Variable array as: $GLOBALS['x4'].
$nogdog = 4; //'Global Variable' as outside function. It now exists in "Super Global" Variable array as: $GLOBALS['y4'].
function addition_4()
{
$GLOBALS['sempervivum'] = $GLOBALS['developer_web'] + $GLOBALS['nogdog']; //A new "Global Variable" '$sempervivum' is created inside the function.
return $sempervivum; //LINE 11: WHY I GET ERROR: Notice: Undefined variable: sempervivum in C:xampphtdocsTemplatestest2.php on line 11
}
echo addition_4();
echo $sempervivum; //THIS SHOWS NO ERROR. ECHOES: 8.
?>
``<i>
</i>function addition_4()
{
$GLOBALS['sempervivum'] = $GLOBALS['developer_web'] + $GLOBALS['nogdog']; //A new "Global Variable" '$sempervivum' is created inside the function.
// Although the line of code that defines the global variable $sempervivum
// is located inside the function addition_4
// this variable is global and therefore cannot be accessed from inside the function
// return $sempervivum; //LINE 11: WHY I GET ERROR: Notice: Undefined variable: sempervivum in C:xampphtdocsTemplatestest2.php on line 11
// This would work although it would not make sense:
return $GLOBALS['sempervivum'];
// This would work either: Define a local variable and return it:
$sempervivum = $GLOBALS['developer_web'] + $GLOBALS['nogdog'];
return $sempervivum;
}<i>
</i>
``
>@developer_web#1630133 difficult to debug errors if the code that uses the function changes
[code=php]
error_reporting(E_ALL);
ini_set('display_errors', true);
function yuck() {
return $GLOBALS['x'] + $GLOBALS['y'];
}
$x = 2;
$y = 4;
echo yuck();
// 6
// but if for any reason you change the main code so that those
// global variables are not what the function expects....
unset($y);
echo yuck();
// Notice: Undefined index: y in php shell code on line 2
// 2
$y = array(1,2,3);
php > echo yuck();
// Warning: Uncaught Error: Unsupported operand types in php shell code:2
// Stack trace:
// #0 php shell code(1): yuck()
// #1 {main}
// thrown in php shell code on line 2
[/code]
<i>
</i>function add($a, $b) {
if(!is_numeric($a) or !is_numeric($b)) {
throw new Exception("Non-numeric value passed to add()");
}
return $a + $b;
}
echo add($x, $y);
add()
<i>
</i>function add($a, $b) {
if(!is_numeric($a) or !is_numeric($b)) {
throw new Exception("Non-numeric value passed to add()");
}
return $a + $b;
}
echo add($x, $y);
<i>
</i>
<?php
$a = 1; /* global scope */
function test()
{
echo $a; /* reference to local scope variable */
}
test();
?>
0.1.9 — BETA 4.23