Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Is array empty?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
blaz
Smarty n00b


Joined: 13 May 2003
Posts: 2

PostPosted: Tue May 13, 2003 11:28 am    Post subject: Is array empty? Reply with quote

How to use a smarty IF statement depending on if an array is empty?

I have an array called $errors. This doesn't seem to work...

Code:

{if $errors ne ''}
...some code here...
{/if}


Thanx,
Blaz
Back to top
View user's profile Send private message
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 235
Location: Purdue University

PostPosted: Tue May 13, 2003 2:47 pm    Post subject: Reply with quote

Humm good point I just tried and I dont know how you would do that without changing Smarty
_________________
"Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Tue May 13, 2003 3:59 pm    Post subject: Reply with quote

hmm

{if $array ne ''} ... tests if the array is *not* empty.

possible tests for empty array:
{if $array eq ''}
{if !$array}
{if not $array}

these all are not type-specific (they also match for empty strings, empty objects, false, null, 0 and 0.0). but types are something for php, not for smarty Smile


edit: correction, {if $array eq ''} does not work, but the other two do.


Last edited by messju on Tue May 13, 2003 4:16 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website
sweatje
Smarty Regular


Joined: 17 Apr 2003
Posts: 70
Location: Bettendorf, Iowa, USA

PostPosted: Tue May 13, 2003 4:04 pm    Post subject: Reply with quote

I use
Code:
{if  $errors|@count gt 0}
     ... some stuff here ...
{/if}


HTH
_________________
Jason
jsweat_php AT yahoo DOT com
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue May 13, 2003 5:04 pm    Post subject: Reply with quote

I tended to use sweatje's method (and sometimes the ne "" method--I think that worked in previous versions??) but I decided to see just what the difference was performance wise (since modifiers don't come for free).

Using my handy-dandy timeblock plugin and the following template code:

{timeblock}
{if $array|@count == 0}EMPTY{/if}
{/timeblock}

{timeblock}
{if !$array}EMPTY{/if}
{/timeblock}

{timeblock}
{if not $array}EMPTY{/if}
{/timeblock}

I ran tests both with empty arrays and arrays populated with 1000 elements. Cutting to the chase:

$array|@count == 0 is about 10 times slower than the other two methods.

!$array and not $array run about neck-and-neck.

For fairness, you must run each timeblock separately. Otherwise, PHP seems to cache (?) the lookup result on the array causing subsequent checks to be faster.

The moral is:

If you get advice from more than one person, pay particular attention to what messju is saying!!
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Tue May 13, 2003 10:44 pm    Post subject: Reply with quote

ah, no boots. i know of bugs i committed to smarty-cvs. of course i fixed 'em asap when i noticed, but my choice wasn't the best before then.

all in all: "good" is a vector and "fast" is only a component.

but your post makes me ponder, if modifier-calls to php-functions should be resolved at compile-time instead of run-time. this would bring the 'count' into the compiled template literally. i think this will have an impact of native-php modifier's execution time.

just some thoughts, that are totally off-topic here. Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed May 14, 2003 12:25 am    Post subject: Reply with quote

messju--I didn't mean to put undue pressure on you--I was just having fun there Wink Apologies to sweatje as well: as I said, I made the same choice as you did -- it seems natural to test strings against "" and arrays against @count. I was even using $var|count == 0 to test strings for consistency with $array|@count == 0.

Quote:
all in all: "good" is a vector and "fast" is only a component.


absolutely! In this case, given three otherwise equal choices, I'll take the fastest.

On the topic of your off-topic comments, I would love to see compiled modifiers -- recall my query to add compile-time modifier plugins. I think it might also lead to ubiquitous support for modifiers whereas right now, they don't operate on builtins or compiler functions.

One thing I like about benchmarking is that it forces comparisons and therefore brings up things I might not otherwise consider.
Back to top
View user's profile Send private message
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Wed May 14, 2003 4:32 pm    Post subject: Reply with quote

yeah... boots... but you know, often a |@count can't be avoided...
I _always_ initialize arrays ($foo = array();) before I populate and assign them, so I can't do a !$array in an IF... ;/
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed May 14, 2003 4:55 pm    Post subject: Reply with quote

Wom.bat:

Quote:
yeah... boots... but you know, often a |@count can't be avoided...
I _always_ initialize arrays ($foo = array()Wink before I populate and assign them, so I can't do a !$array in an IF... ;/


did you try testing that?

---
$smarty->assign('b', array());
---
{if !$a}Empty{/if}
{if !$b}Empty{/if}
---
EmptyEmpty

So, initialized (but empty) arrays and uninitialed vars both eval to true for !$var.
Back to top
View user's profile Send private message
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Thu May 15, 2003 2:17 pm    Post subject: Reply with quote

w000t
well... in php, it would be false
Smile
sorry for that mistake
Smile
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu May 15, 2003 5:27 pm    Post subject: Reply with quote

Wom.Bat:

Yeah, I see why you thought that and my feeling is that the behaviour that Smarty is exhibiting is not by policy but instead an artifact of the implementation. I too would have expected your result (ie. consistent with PHP) but it was in timing the different checking styles that I noticed this behaviour. So I wasn't trying to "correct" you--only showing what the actual behaviour appears to be Wink


Last edited by boots on Thu May 15, 2003 7:50 pm; edited 1 time in total
Back to top
View user's profile Send private message
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Thu May 15, 2003 7:21 pm    Post subject: Reply with quote

hehe Smile not a problem, thank you very much Wink

P.S: have you tested the math patch yet? Smile
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP