Jump to content

custom function


Matpatnik

Recommended Posts

I just created this function and I want to know if there is a better way of doing it

/* erase the / and .php from /apage.php */function page_name($_SERVER) {	global $name;	$maxcount = strlen($_SERVER['PHP_SELF']) - 5; // I have no idea why 5 there is only 4 character to me, 4 show the dot	$name = substr($_SERVER['PHP_SELF'], 1, $maxcount);	return $name;}

and then I just call the page_name function and echo the $name.Thank you

Link to comment
Share on other sites

* do you know why in my function I have to subtract 5 instead of 4?
Because there are 5 characters that you no longer want: /, ., p, h, and p and rather than starting at position 0 for the substring, you are starting at position 1.You can think of it like this:The length of ".php" = 4.The length of "/apage" = 6If you start at 1, then the length of the substring that you want is 1 less than it's current length (6). So, you subtract 4 for the ".php" and another 1 for the beginning "/".I hope that helped!
Link to comment
Share on other sites

Also, just to point out, you don't want to (and definately should not) have a function parameter named after a superglobal like $_SERVER. $_SERVER is a superglobal, it's automatically global in any function without needing to declare it. If you pass an array you make to your pagename function, it will be very confused over whether it should use the array that you passed by value, or the superglobal $_SESSION. So don't pass superglobals as parameters, just use them in the function.

Link to comment
Share on other sites

Because there are 5 characters that you no longer want: /, ., p, h, and p and rather than starting at position 0 for the substring, you are starting at position 1.You can think of it like this:The length of ".php" = 4.The length of "/apage" = 6If you start at 1, then the length of the substring that you want is 1 less than it's current length (6). So, you subtract 4 for the ".php" and another 1 for the beginning "/".I hope that helped!
Sorry I didn't explain my self correctlyWhat I meant by 5 is I had to delete 5 character at the end of $_SERVER['PHP_SELF'] to delete the .php but I only see 4 character when I echo it!I didn't count the first slash because it was not in the $maxcount variable.Justsomeguy, at first I didn't use the superblobal as parameter in that function and it didn't work, then I decide to try it anyway to see the result and it was working (but I probably change few things at the same time). :)
Link to comment
Share on other sites

Sorry I didn't explain my self correctlyWhat I meant by 5 is I had to delete 5 character at the end of $_SERVER['PHP_SELF'] to delete the .php but I only see 4 character when I echo it!I didn't count the first slash because it was not in the $maxcount variable.
I think I understood you correctly. You're asking why you have to subtract 5 rather than 4. Maybe another example will help. Think about this code instead:
$startpos = 1;$extension = ".php";$strlength = strlen($_SERVER['PHP_SELF']) - strlen($extension);$name = substr($_SERVER['PHP_SELF'], $startpos, ($strlength - $startpos));

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...