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
Related
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 have an extension in Magento that is causing a bug in the system.log
The error message is below. It is at the return statement after the foreach statement
Notice: Undefined variable: struct
The method is
public function getFeeList($id)
{
//some code here but no statement that contains $struct
foreach($result2 as $kk=>$vv)
{
$struct[$vv['magikfees_id']]['tax_apply']=$vv['tax_apply'];
$struct[$vv['magikfees_id']]['tax_type']=$vv['tax_type'];
....
}
return $struct;
}
To my understanding is that you do not need to declare the PHP variables. So one question is
is the scope of the $struct variable in this case only in the foreach? OR
the code never gets in the foreach statement so $struct is not initialized?
is declaring it before the foreach going to resolve this issue? How can I declare it?
is the code actually working regardless of this notice?
You do not need to declare PHP variables, but you will get a notice when you don't. You can declare it as $struct = array();.
The code will work, but as $struct wasn't declared, it is likely you made a mistake (maybe a typo). PHP warns you for that by sending a notice. Although it does work without the declaration, it is recommended to declare it anyways.
No, the scope is the method getFeeList(). Once you've defined the variable you can access it later in your code in that particular method. If the code doesn't enter the foreach, the variable will be null.
Yes it will remove the notice error. You can do that by adding $struct = array(); before your foreach. Another workaround is to remove E_NOTICE from your error_reporting setting in php.ini or set ini_set() in the beginning of your code.
This particular piece of code should be working just fine even if you haven't defined the variable.
Although in PHP you are not required to define your variables it's a better coding practice to define them.
I'm building my first application with Kohana, and using a basic templating system within that. In my templates I want to echo variables for the various contents of the page, but only if each variable is set, and I want to keep the code in the templates as short as possible, so something like this:
<?=$foo?>
works fine if the variable is set, but if it's not I get a notice. So I thought a ternary operator would do the trick nicely:
<?=$foo?:''?>
according to the PHP manual, from 5.3 it's ok to leave out the middle part and the above should output nothing if the variable isn't set, but I still get an error notice."Notice: Undefined variable: foo in /"
I can get the desired result with a slight alteration to suppress the notice:
<?=#$foo?:''?>
but I know that's generally not beset practice and would like a better solution if possible, while still keeping the code to a minimum in the template files.
the following works, but it's not as concise (mainly because my actual variables can be quite long):
<?=isset($foo)?$foo:'';?>
am I missing something or doing something wrong?
The ternary operation is not meant to replace checking with isset() - it needs it's variable defined or else you get a notice.
Template engines usually offer a way to use a default value instead, but they also do not use pure PHP code. You you are out of luck here: Either suppress the notice, or use the longer code variant. Or ensure that every variable is set, which enables you to consider any notice an error.
To avoid notices for undefined variables, you can create custom function that takes first parameter by reference
function tplvar(&$value, $default = '') {
return ($value !== null) ? $value : $default;
}
<?=tplvar($foo, 'bar');?>
Uninitialized variables passed by reference will be seen as nulls.
I want to use such code:
$arr=array('a'=>$a);
but $a is not defined, so I get error. But if I write this code before
if (!isset($a))
$a=null;
all works. Why? In the beginning $a is not defined , so $a=null. or underfined!=NULL ?
When you write
array("a"=>$a)
it means that you want the key "a" refers to a variable reference named $a which does not exist in the first place thus you are getting an error; but when you add
$a=null;
before hand, although you are setting $a to null but actually you are creating a variable reference named $a that's known by PHP so there will be no errors.
Yes, in truth undefined != null, although the difference is only in the eyes of PHP when it decides whether to throw an error or not. Otherwise it's the same thing.
As you already discovered undefined is different from null.
Undefined means that the name $a in not yet into the scope of your function/code.
$a=null is a (no)value assigned to a variable.
By the way you should get a Notice, not an error as using undefined variables as right-values php warns you about a probable typo and proceed with the script execution.
As a rule of thumb, if you address an undefined variable on the left of the assignment (=) simbol (a left-value name) then php create a new variables name and bind it into the current scope, if the reference is on the right (are you using the value contained instead than the variable itself) php warns you and keep going. You can change this behaviour by the error_reporting function.
see the manual of isset
Returns TRUE if var exists and has
value other than NULL, FALSE
otherwise.
Hi I am new to opencart and currently facing one issue that not able to identify why it is coming
"Notice: Undefined variable: dailydeal in ~/template/common/column_right.tpl on line 3"
At line no 3 in this file the code written is as follows:
<?php echo ${$module['code']}; ?>
Can any one tell me the what is meaning of this syntax. or how I can solve this issue.
Thanks a lot in advance.
That's a variable variable, from your error I can tell that:
echo $module['code']; // dailydeal
The ${$module['code']} is the equivalent of $dailydeal (the variable content).
To avoid OpenCart throwing notices you can either lower your error reporting level, like this:
error_reporting(E_ALL & ~E_NOTICE);
Or just fix the notice altogether, by adding an isset() check:
if (isset(${$module['code']})) {
// do stuff
}
see this article variable variables there is written
In order to use variable variables
with arrays, you have to resolve an
ambiguity problem. That is, if you
write $$a1 then the parser needs to
know if you meant to use $a1 as a
variable, or if you wanted $$a as the
variable and then the 1 index from
that variable. The syntax for
resolving this ambiguity is: ${$a1}
for the first case and ${$a}1 for
the second.
also read
language variable