FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Why this code should be needed?





shamil
Excuse me the code below found in phpbb source code really interests me. Don't understand why this should be needed? Have you any idea?
Code:
      // Strip the trailing period.
      $namespace = substr($namespace, 0, strlen($namespace) - 1);

It is equalent to
Code:
$namespace = $namespace;
isint't it? Rolling Eyes
n0obie4life
The comment tells you. It strips the trailing period. Where as in $namespace = $namespace, it doesn't.

It's probably for security, making sure it cannot be abused Smile.
shamil
n0obie4life wrote:
The comment tells you. It strips the trailing period. Where as in $namespace = $namespace, it doesn't.

It's probably for security, making sure it cannot be abused Smile.

What can be stripped here actually. Can you give any example? Still uncler. It just says $namespace = $namespace.
Kaneda
It always tends to be confusing to use a substring() variant in a new language - some languages have the second parameter be the length, others have it be the index of the last character to extract.

Javascript, for example, has both. substr(index,length) and substring(firstIndex,lastIndex)...

In PHP substr()'s case, the second parameter is the length of the string to extract (like in JS), so:

Code:
$namespace = substr($namespace, 0, strlen($namespace) - 1);


... extracts [the length of the string minus 1] characters starting at the beginning of the string. So yes, it strips the last character. Had the second parameter been the index of the last char to extract, then you'd be right Smile

Example: If we had the string "hello." - 6 characters, then the function call would be equivalent to:

Code:
$namespace = substr($namespace, 0, 6 - 1);
i.e.
$namespace = substr($namespace, 0, 5);


i.e., "extract 5 characters beginning at the first". Which returns "hello" without the period.
shamil
Kaneda wrote:
It always tends to be confusing to use a substring() variant in a new language - some languages have the second parameter be the length, others have it be the index of the last character to extract.

You are right. It confused me. Thanks both guys. Wink
Related topics
questions :P
What - I had lost at Karen Mok's concert
Perl Script - checking if feilds are complete
'members only' section of website
Simple PHP forum code needed!
OMG! Pres Clinton and the NSA Eavesdropped on US Calls!
Ok... My website
C++ assignment help
Explaination about this C++ code needed
Privitize Your Downloads.
Clicksor
SAT- Longer Time Given for Wealthy Kids
Why does this code make undeletable files?
A code needed !!
To use or not to use a CMS - I need careful advice
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.