[Vtigercrm-developers] A question on efficiency/semantics...

Joe Bordes joe at tsolucio.com
Wed May 5 15:29:57 GMT 2021


what is true, and can be optimized, is that PHP is smart enoughto know that

empty(array()) == true

so you could avoid the count() in this particular case


On 5/5/21 17: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
>
>
>
>
> On 5/5/21 17:04, Sukhdev Mohan wrote:
>>>> if ($variableList) {
>>>
>>> would provide the same result? To me that simple if($var) statement 
>>> says, "if $var is not equal to a boolean false, or 0, or null then 
>>> continue"...
>>
>> Yup, $var = [] is still 0 so it’s a falsy value (love the C based 
>> programming languages).
>>
>> I’d like to propose, let’s anticipate the rejection instead of nested 
>> ifs for the expected values (normally ifs should be used to handle 
>> exceptions not for the basic functionalities), like this:
>>
>> If(!$var) { // check what will break the code
>>     // reject statement and exit routine
>> }
>>
>> // continue the normal flow
>>
>> This would also improve readability as it’s short and you immediately 
>> see what can cause a break.
>>
>> Best Regards,
>> *Sukhdev Mohan* | *Software Developer*
>>
>>
>>
>>
>>
>>
>>
>>> Il giorno 5 mag 2021, alle ore 16:14, Alan Lord 
>>> <alanslists at gmail.com <mailto:alanslists at gmail.com>> ha scritto:
>>>
>>> In the fairly recent past there have been a number of minor commits 
>>> made which, I guess, are to help prevent php WARNING and/or NOTICE 
>>> type messages, e.g.
>>>
>>> Something like this:
>>>
>>>> if (count($variableList) > 0) {
>>>
>>> became this:
>>>
>>>> if (!empty($variableList) && (count($variableList) > 0)) {
>>>
>>> But this seem to be overkill...
>>>
>>> As I understand things simply doing this:
>>>
>>>> if ($variableList) {
>>>
>>> would provide the same result? To me that simple if($var) statement 
>>> says, "if $var is not equal to a boolean false, or 0, or null then 
>>> continue"...
>>>
>>> count($var) would return an integer if $var is a string, so it is 
>>> not a useful explicit test for an array.
>>>
>>> And from the php manual, empty($var) says "a variable is considered 
>>> empty if it does not exist or if its value equals false"
>>>
>>>
>>> Thoughts / Comments?
>>>
>>>
>>> Al
>>>
>>> _______________________________________________
>>> http://www.vtiger.com/ <http://www.vtiger.com/>
>>
>>
>> _______________________________________________
>> http://www.vtiger.com/
> -- 
> Un saludo
> Joe
> TSolucio
>
> JPL TSolució, S.L. garantiza la confidencialidad de los datos que obran en su poder y según lo estipulado por la LOPD
> (Ley Orgánica 15/1999 de 13 de diciembre de Protección de Datos de Carácter Personal). Este mensaje se dirige
> exclusivamente a su destinatario y puede contener información privilegiada o confidencial. Si no es vd. el destinatario
> indicado, queda notificado de que la lectura, utilización, divulgación y/o copia sin autorización está prohibida en
> virtud de la legislación vigente. Si ha recibido este mensaje por error, le rogamos que nos lo comunique inmediatamente
> por esta misma vía y proceda a su destrucción. El correo electrónico vía Internet no permite asegurar la confidencialidad
> de los mensajes que se transmiten ni su integridad o correcta recepción. JPL TSolució, S.L. no asume ninguna
> responsabilidad por estas circunstancias.
>
> _______________________________________________
> http://www.vtiger.com/

-- 
Un saludo
Joe
TSolucio

JPL TSolució, S.L. garantiza la confidencialidad de los datos que obran en su poder y según lo estipulado por la LOPD
(Ley Orgánica 15/1999 de 13 de diciembre de Protección de Datos de Carácter Personal). Este mensaje se dirige
exclusivamente a su destinatario y puede contener información privilegiada o confidencial. Si no es vd. el destinatario
indicado, queda notificado de que la lectura, utilización, divulgación y/o copia sin autorización está prohibida en
virtud de la legislación vigente. Si ha recibido este mensaje por error, le rogamos que nos lo comunique inmediatamente
por esta misma vía y proceda a su destrucción. El correo electrónico vía Internet no permite asegurar la confidencialidad
de los mensajes que se transmiten ni su integridad o correcta recepción. JPL TSolució, S.L. no asume ninguna
responsabilidad por estas circunstancias.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.vtigercrm.com/pipermail/vtigercrm-developers/attachments/20210505/cdbfcdbc/attachment.html>


More information about the vtigercrm-developers mailing list