Jamroom Logo Jamroom 5 Core
is now Open Source!
User Support Forum Archive (Read Only)
Jamroom Developers:
strlen trick
smith.kyle
CodeSmith


Joined: 27 Apr 2006
Posts: 22009
Location: Southern California

Posted: 05/24/09 13:26 
I found a nifty little trick I thought was kind of cool. Usually if I need to strlen something, I do it like so:


Code
if (isset($foo) && strlen($foo) > 0) {
    //do something
}


I found a nifty trick though that will do the same thing, but make things slightly faster:


Code
if (isset($foo[0])) {
    //do something
}


If you use a string variable as an array, each character from the string becomes an element of the array, i.e.

$foo = 'string';
$foo[0] = 's';
$foo[1] = 't';
$foo[2] = 'r';
$foo[3] = 'i';
etc.

so by doing isset($foo[0]), you are effectively checking to see if a character exists in the first position of $foo. This is slightly faster than strlen() as isset() is a language construct, whereas strlen() is a function. It's not gonna make that much of a difference, I just thought it was cool!

Kyle


_________________
kyle[at]jamroom.net

Yes...that's a soda machine...

I get bored when no one's posting...
Back to top
Brian
Jamroom Team


Joined: 09 Jul 2003
Posts: 37583
Location: Seattle, WA

Posted: 05/24/09 15:49 
Jamroom uses this in a few places internally, and it is a good trick - the only issue is that it can be a bit of a "gotcha" if $foo is an array... I believe you can also use "squigly" brackets for this as well, which is clearer - i.e.


Code

if (isset($foo{1})) {


- Brian


_________________
Make sure and check out:
* The Jamroom FAQ
* The Jamroom Documentation
Back to top
djmerlyn
Jamroom Ustad


Joined: 18 Dec 2003
Posts: 13497
Location: Behind You

Posted: 05/24/09 20:06 
I don't understand...

isset($foo)
isset($foo[1])

Are both the same as saying "is there anything here", is it not? Why do you need the key?


_________________
Pro JR Hosting, now 50% off!
-100% Guaranteed

"more server and network power than any host, dedicated to your jamroom site"
Back to top
djmerlyn
Jamroom Ustad


Joined: 18 Dec 2003
Posts: 13497
Location: Behind You

Posted: 05/24/09 20:18 
oh, because the var may be set but may be =0 not necessarily >0 correct?


_________________
Pro JR Hosting, now 50% off!
-100% Guaranteed

"more server and network power than any host, dedicated to your jamroom site"
Back to top
smith.kyle
CodeSmith


Joined: 27 Apr 2006
Posts: 22009
Location: Southern California

Posted: 05/24/09 23:27 
Right DJ. Cool with the curly braces, I'll have to use it more Wink


_________________
kyle[at]jamroom.net

Yes...that's a soda machine...

I get bored when no one's posting...
Back to top
Paul
Jamroom Team


Joined: 20 Aug 2003
Posts: 5341
Location: Nottingham, UK

Posted: 05/25/09 02:02 
Can this be used inside smarty ifs?

{if isset($foo{1})}
Do Something
{/if}

Might the curly braces confuse things?

Pa


_________________
Paul Asher
Jamroom Network Team Member: http://www.jamroom.net
Priority Support: http://www.jamroom.net/Support_Center
Back to top
smith.kyle
CodeSmith


Joined: 27 Apr 2006
Posts: 22009
Location: Southern California

Posted: 05/25/09 16:10 
I think you'd have to do it with brackets instead of braces if you tried it in smarty - I'm not sure if it works.


_________________
kyle[at]jamroom.net

Yes...that's a soda machine...

I get bored when no one's posting...
Back to top
Display posts from previous:   
User Support Forum Archive (Read Only)
Jamroom Developers

 
Solutions
• Social Media Platform
• Social Networking Software
• Musician Website Manager
• Community Builder
Products
• Jamroom Core
• Jamroom Addons
• Jamroom Modules
• Jamroom Marketplace
Support
• Support Forum
• Documentation
• Support Center
• Contact Support
Community
• Community Forum
• Member Sites
• Developers
Company
• About Us
• Contact Us
• Privacy Policy
©2003 - 2010 Talldude Networks, LLC.