How can I check if a variable is set in PHP? If I am using isset it still throws an error Warning: Undefined variable
if (isset($_POST['newContext'])) {
$newContext = $_POST['newContext'];
}
Background:
My JavaScript file passing const = newContext to my PHP. This variable is in use like 80% of each executons. I want to get rid of the PHP warning message for the remaining 20% and only set the passed variable if this is not empty. How can I do it?
Had an old loop included, definetely as Eriksson mentioned, it wasn´t the method
Related
I am struggling to debug a PHP error I am getting when uploading photos to my website. It is not WordPress btw. The error message says
"A PHP Error was encountered, Severity: Notice
Message: Only variables should be passed by reference
Filename: admin/vehicles.php
Line Number: 322"
Below is the line of text from that file and line 322. I'm not sure if this is enough info to go off of, but if it's a simple syntax error, I'm sure it is.
$ext = array_pop(explode('.', $_FILES['slider_image']['name']));
Thanks in advance! I can provide more of the code if needed.
array_pop expects an actual variable, not a returned array, because it works with a pointer.
Try this:
$array = explode('.', $_FILES['slider_image']['name']);
$ext = array_pop($array);
Just to expand a bit on that:
If you use explode('.', $_FILES['slider_image']['name']);, you get an array. However, this array doesn't really exist. Its basically homeless. When you assign it to a variable, it gets an actual "address" in the memory.
array_pop only accepts references, not values (this is known as "pass by reference" vs "pass by value"). So you don't give array_pop a value, but the address to the value. These functions usually have a & sign in front of the variable name in the function definition.
https://www.php.net/manual/en/function.array-pop.php
I am getting this message
While trying to load a page. And its indicating to this line of code
return view('admin/vehicle-categories/index',compact('categories','level','parent_categories','type','parent_id'));
}
How can I solve this?
This was a breaking change introduced in PHP 7.2. Previously you could compact a named variable that was not yet defined. After 7.2 you must define the variable previous to adding to the compact method.
Broke a lot of code for us as well.
The fix is to simply define the variable before the compact statement, even if null. Be careful if going through a branch/if-check that you define it for certain:
$type == null // or value
I have an array say $packages. I have the code like this to prevent the undefined variable error.
$hospital_charge=0;
if(!empty($packages)){
foreach($packages as $package){
$hospital_charge=$package['hospital_charge'];
Then in subsequent code I use
$health_card_discount = ((($a*5)/100)+($hospital_charge*.05));
That is if $hospital_charge have a null value, I get the error
undefined variable hospital_charge,
so to prevent that I defined $hospital_charge=0
is this the proper way of doing it or there is better way of achieving this?
Note:I am using PHP 5.4.16
You can always use isset (it will check that the variable is defined and not null:
$health_card_discount = isset($hospital_charge) ? ((($a*5)/100)+($hospital_charge*.05)) : (($a*5)/100);
But the way you are doing it is also valid.
I am doing project in php.I used ajax code to upload the image.
It done nicely, everything is working but display notice and i want 2 remove this notice...
Notice is as follows:
Notice: Undefined variable: errorList in C:\xampp\htdocs\ajax\scripts\ajaxupload.php on line 18 Notice:
Undefined variable: errorList in
C:\xampp\htdocs\ajax\scripts\ajaxupload.php on line 132
i am not understanding what is the meaning of message...
How to remove this Notice?
To remove the notice you must either check is this variable $errorList defined and if it is, then check ajaxupload.php on line 18 and ajaxupload.php on line 132 does it see it there or is it out of the scope.
The other thing you could do is place at the beginning of PHP file ini_set( 'display_errors', 0 );, but this really bad idea since Notice is made to show what you are doing wrong.
Simply define the variables as null before using them, so that by default they will be null until given a value from elsewhere. Variables defined inside an if block or by user input sometimes don't get defined, right? So for them, a default is needed sso that they at least exist even if they aren't given a value.
$errorList = null;
When I try to access info that is not presented in xml like so: $someInfo = $element->blabla->cats[0]->src;
PHP shows notice like this: Notice: Trying to get property of non-object
How would I settle the matter?
Either $element, blabla, or cats[0] is not an object, and thus can't contain any elements.
Use isset():
if (isset($element->blabla->cats[0]->src))
echo $element->blabla->cats[0]->src;
one isset() should do, no need to check every part consecutively.
This should do the job even if cats exists but is not an array.
you can use isset to verify if object property exists, like this:
if (isset ($element->blabla) && isset ($element->blabla->cats) && etc..)
if you just don't want to see the notice, use error_reporting(E_ALL & ~E_NOTICE)
Or alternative (but wrong way) you can suppress log in your php script by:
error_reporting(E_ERROR);
Which will force php to report only fatal errors.
Anyway, use Pekka solution.