If we miss something it's the nature that compiler producing a warning .Anyway I wanted to neglect the warning or rid from showing the warnings in php programs.
Example Warning is :
Error(s), warning(s): PHP Notice: Undefined variable: z in
source_file.php on line 11
Below is an example which shows an warning : How we can neglect or suppress the warning?
<?php
$x=4;
$y=3;
function func($x=3 ,$y=4)
{
$z=$x+$y/$y+$x;
echo '$z';
}
echo $x;
echo $y;
echo $z;
func($x,$y);
?>
You are printing $z variable at line 11, which is not defined.
You've defined $x and $y, but no $z.
The $z in your "func" function exists in a scope of the function only and not the "main" script.
So, I guess is what you trying to do:
<?php
function func($x=3 ,$y=4)
{
$z=$x+$y/$y+$x;
return $z;
}
$x=4;
$y=3;
//Now $z is defined!
$z = func($x,$y);
echo $z;
?>
becouse you have not defined $z. correct code is given below.
<?php
$x=4;
$y=3;
$z="";
function func($x=3 ,$y=4){
$z=$x+$y/$y+$x;
echo $z;
}
echo $x;
echo $y;
echo $z;
func($x,$y);
?>
Variable $z has a function level scope as you declare it in a function Therefore, you are getting that warning.
Also, there is no need of defining your function as
function func($x=3 ,$y=4) {} // here you are limiting the function to perform calculation for only 3 and 4
As you are passing values to a function therefore define your function as
function func($x ,$y){}
and use that function in your code as many times as you want.
To achieve your desired result try the following code
$x=4;
$y=3;
function func($x=3 ,$y=4)
{
return $x+$y/$y+$x;
}
echo $x;
echo $y;
$z = func($x,$y);
echo $z;
It is trying to tell you that z is not defined from where you are trying to use it. Actually, it is only defined within the function func. Review the scope of variables in PHP.
Edit:
Q: How to clear the warning?
A: You need to either define z or suppress the warning from the compiler. I would strongly recommend that you define it. An answer below already gives a possible alternative to your code where the problem is solved.
Related
I saw this on W3Schools.
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
The output is 0, 1 and 2.
I wonder why it is not 0, 0 and 0.
Since each time the function is called, the variable x becomes 0 again.
I am a PHP beginner. Thanks!
If you declare a var static inside a function, the var keep it value over multiple calls. You could compare it to a static var inside of classes.
The code you post is a good example to see the actual effect. However I would only carefull use static inside functions, because most of the time, you need the static value somewhere else, reset the value, or something else which requires to much logic and makes the code really bad.
A good example would be a function, which returns a unique identifier for a given identifier. This could be simply achieved by using this code.
function unique_id($id) {
static $count = 0;
return $id . ($id++);
}
This example may clarify. static has a scope, thus is not a global variable. So I can define static $x outside the function and it will be defined there. Since static has scope, it wouldn't make any sense to keep on executing and resetting $x = 0. So, php will only acknowledge it the first time that line is called.
<?php
static $x = 1000;
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
<?php
function sum($y) {
$y = $y + 5;
}
$x = 5;
sum($x);
echo $x;
?>
So I have this code. The questions are: What does it output? The answer: 5. How do I make it to output 10? The answer: sum(&$x).
The problem is that I don't understand why the answer to the first question is 5. When you make sum($x), shouldn't it replace the function with $x, so $x= 5+5=10? Why the answer is 5? I really don't understand. Someone explaind me something related to pointers and the adress, but I didn't understand. I never understood the concept of pointers, and I googled it and apparently there are no pointers in php, so I'm super confused. My friend said that a variable is formed of a value, and the memory adress of that value. Can someone explain me like I'm 5 years old why the answer is 5 and not 10? Please
Let's pretend $x is a piece of paper with 5 written on it.
function sum($y) {
$y = $y + 5;
}
Here $y is the value of what you have written. You add 5 to such value in your mind, but the note is left untouched.
function sum(&$y) {
$y = $y + 5;
}
With the reference operator (&$y), you pass the very paper to the function, and it overwrites what's written on it.
For primitive values like numbers, I wouldn't bother and always return the value you want:
function valuePlusFive($x) {
return $x + 5;
}
$x = 5;
$x = valuePlusFive($x);
This is not a very good explanation from theory point of view, but this should help you understand the concept:
when you declare function like this and then call it:
function ($argument) {...}
The argument you pass there is passed by value. This means that inside the scope of a function you will be working with a copy of an argument you passed. You can imagine that before the function is called the copy of argument has been made and inside the function you're working with the copy, while original remains untouched. Once the function is finished the copy is no more
However when you declare it like this:
function (&$argument) {...}
You are passing argument by reference, meaning that you are working directly with a variable you've passed. So in this case no copies are made, you took the argument from one place, put it inside the function and changed it.
In PHP, "The scope of a variable is the context within which it is defined." (according to the docs). So inside your function, $y (a copy of the value you passed in) is being operated on, but it is not returned by the function. So when the function ends, the value is no longer accessible outside the function.
If you want to pass the variable in by reference (similar to a pointer in C) then you can add a & like so:
function sum(&$y) {
$y = $y + 5;
}
Now when you call this code:
$x = 5;
sum($x);
echo $x;
it will output 10. Maybe a better way to do this would be to return a value from your function, and output that value:
function sum($y) {
return $y + 5;
}
$x = 5;
echo sum($x);
Just started learning PHP, and while experimenting with variable scopes, I created this code:
<?php
$x = 5;
function scopeTest($x) {
global $x;
echo $x;
}
scopeTest(4);
?>
In the given function I pass value 4, in the function that value is stored in variable $x (local to the function). The output of this code is 5 and not 4.
I don't know where the variable with value 4 gone? I know I can do this by changing the local variable name in the function but I want to know flow of this program, how it is outputting 5.
Is the local variable $x overridden with the global variable $x?
Is there any way to access the local variable $x value 4 within the function?
The local variable is being overwritten with the statement global and since they are sharing the same variable name, you lost reference to it.
But by doing this, you can use both:
$x = 5;
function scopeTest($x) {
echo $GLOBALS['x'], $x; // 54
}
scopeTest(4);
Or.. just rename the local variable
function scopeTest($y) {
global $x;
echo $x, $y;
}
Yes You can use the value 4 of the $x by echoing the $x before the global $x;
global $x; //replace the value of $x to it's global value.
Is there any differences between call_user_func() and its syntactic sugar version...
// Global function
$a = 'max';
echo call_user_func($a, 1, 2); // 2
echo $a(1, 2); // 2
// Class method
class A {
public function b() {
return __CLASS__;
}
static function c() {
return 'I am static!';
}
}
$a = new A;
$b = 'b';
echo call_user_func(array($a, $b)); // A
echo $a->$b(); // A
// Static class method
$c = 'c';
echo call_user_func(array('A', $c)); // I am static!
echo a::$c(); // I am static!
codepad.
Both output the same, but I was recently hinted (10k+ rep only) that they are not equivalent.
So, what, if any, are the differences?
First difference I can think of is that call_user_func() runs method as a callback.
This means you can use a closure, eg
echo call_user_func(function($a, $b) {
return max($a, $b);
}, 1, 2);
This would be more of an implementation difference versus a usage or performance (execution) one though.
I honestly can't find much of a difference between the two. They basically do the same thing, but the only differences I can find is that call_user_func takes over 2× longer to complete than variable functions (calling an empty function).
Another thing is that the error handlers are different, if you use a non-existent callback function, the variable function would output a fatal error and halt the script while call_user_func would output a warning but continue the script.
Also when passing parameters through the function, using variable functions provides a little more detail in the error in relation to line numbers:
function asdf($a, $b) {
return(1);
}
call_user_func('asdf', 1):
Warning: Missing argument 2 for asdf()
in G:\test.php on line 3
-
$a='asdf'; $a($a, 1):
Warning: Missing argument 2 for
asdf(), called in G:\test.php on line
10 and defined in G:\test.php on line 3
These errors are collected from Command-Line Interface (CLI) tests, the display of errors obviously depends on your configuration.
The situation:
index.php:
<?php
include("foo.php");
include("baz.php");
foo("bar.php");
?>
baz.php:
<?php
$x = 42;
?>
foo.php:
<?php
function foo($p) {
include_once($p); // please dont mind the inclusion hole
}
?>
bar.php:
<?php
echo $x;
?>
Zend notice: Undefined variable: x
Placing global $x; in bar.php removes the notice, but I understand why there is a notice about this in the first place.. Doesn't include pretty much work like including C headers? It would mean that the interpreted code would look like this:
<?php
function foo($p) {
include_once($p); // please dont mind the inclusion hole
}
$x = 42;
// this however, is included by a function...
// does the function's scope influence the stuff it includes?
echo $x; // undefined variable
?>
My editor is the Eclipse/Zend package.
I'm no expert, so please don't flame me if I'm wrong, but I think the file called by include_once or require_once is called in the context of the caller. Since function foo() won't know about $x then neither will any of its called includes. You could experiment by 'declaring' $x inside function foo() with the same setup as above.
I get a bunch of those notices since I'm almost allways goes with "$o .= 'foo'" without any definition. I'm just hiding them with error_reporting(E_ALL ^ E_NOTICE), but I don't know if it's the optimal way in this case.
It doesn't work even if the variable and the function are in the same file.
1 <?php
2
3 $x = 3;
4
5 function t()
6 {
7 echo $x;
8 }
9
10 t();
Prints nothing.
But add a global
1 <?php
2
3 $x = 3;
4
5 function t()
6 {
7 global $x;
8 echo $x;
9 }
10
11 t();
and you can see "3".
In a function you can't see global variables unless you declare it.
yes its the function scope that is causing your issues
if you replace
foo("bar.php");
with
include("bar.php");
you'll see that everything works fine because it puts it into the current scope and not functions scope