[Vtigercrm-developers] A question on efficiency/semantics...
Alan Lord
alanslists at gmail.com
Thu May 6 06:54:24 GMT 2021
That's helpful info,
So it seems using empty() _is_ actually a good solution. And that
count() is probably unnecessary in the initial example I gave?
Thanks.
Al
On 06/05/2021 06:17, Mariusz Krzaczkowski | YetiForce Sp. z o.o. wrote:
> Notice: Undefined variable / Undefined index / Undefined offset
>
> This is a bug that causes a lot of problems,
> no one knows if it's on purpose or if someone forgot to change something
> or made a typo.
>
> https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined#answer-4261200
> <https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined#answer-4261200>
>
> From the vast wisdom of thePHP Manual
> <http://php.net/manual/en/language.variables.basics.php#example-112>:
>
>> Relying on the default value of an uninitialized variable is
>> problematic in the case of including one file into another which uses
>> the same variable name. It is also a majorsecurity risk
>> <http://www.php.net/manual/en/security.globals.php>withregister_globals <http://www.php.net/manual/en/ini.core.php#ini.register-globals>turned
>> on.E_NOTICE
>> <http://www.php.net/manual/en/errorfunc.constants.php#errorfunc.constants.errorlevels.e-notice>level
>> error is issued in case of working with uninitialized variables,
>> however not in the case of appending elements to the uninitialized
>> array.isset()
>> <http://www.php.net/manual/en/function.isset.php>language construct
>> can be used to detect if a variable has been already initialized.
>> Additionally and more ideal is the solution ofempty()
>> <http://php.net/manual/en/function.empty.php>since it does not
>> generate a warning or error message if the variable is not initialized.
>>
> --
> Z poważaniem / Kind regards
> Mariusz Krzaczkowski
>
> W dniu 2021-05-05 17:36, Alan Lord napisał(a):
>
>> Interesting.
>>
>> That seems like good/valid Warning - it should encourage the
>> declaration of variables at the start of each routine.
>>
>> Maybe for a variable variable it might not make sense but those aren't
>> that common it seems to me?
>>
>> Nice to hear from you Joe. Hope all's well.
>>
>> Cheers
>>
>> Al
>>
>>
>> On 05/05/2021 16:24, Joe Bordes wrote:
>>> Sadly, PHP warning system doesn't work that way:
>>>
>>> ----
>>>
>>> <?php
>>> error_reporting(-1);
>>> ini_set('display_errors', 1);
>>>
>>> if ($nothere) {
>>> echo "it is here";
>>> } else {
>>> echo "it isn't here";
>>> }
>>> -----
>>>
>>> result
>>>
>>> -----
>>>
>>> joe at linuxmint:/var/www/coreBOSTest$ php k.php
>>> PHP Notice: Undefined variable: nothere in
>>> /var/www/coreBOSTest/k.php on line 5
>>> PHP Stack trace:
>>> PHP 1. {main}() /var/www/coreBOSTest/k.php:0
>>>
>>> Notice: Undefined variable: nothere in /var/www/coreBOSTest/k.php on line 5
>>>
>>> Call Stack:
>>> 0.0001 393600 1. {main}() /var/www/coreBOSTest/k.php:0
>>>
>>> it isn't here
>>>
>>> ------
>>>
>>>
>>> so you get the warning if the variable is not defined which is what
>>> you are trying to avoid
>>>
>>
>>
>>
>> _______________________________________________
>> http://www.vtiger.com/ <http://www.vtiger.com/>
>
> _______________________________________________
> http://www.vtiger.com/
>
More information about the vtigercrm-developers
mailing list