Keith Devens .com |
Saturday, July 4, 2009 | ![]() |
| Есть Два Цвета: Черный И Белый, А Есть Оттенки, Которых Больше. ('There are two colors: black and white. Others are just shades.') – unknown | ||
|
| ← Evaluation: moving from Java to Ruby on Rails for the CenterNet rewrite | Thanksgiving → |

Dennis Pallett (http://www.phpit.net) wrote:
Dennis Pallett (http://www.phpit.net) wrote:
Oh, and a real shame about named parameters. I love those, and usually use parameters now, i.e.
function foo ($params) {
echo $params['userid'];
echo $params['pass'];
}
instead of:
function foo ($user, $pass)
Keith (http://keithdevens.com/) wrote:
Here's the whole g function, for your entertainment (update, Dec 7: fixed bug in g):
<?php
function g(&$var, $key, $default = NULL){ #make it easy to develop with notices on
if(!is_array($key)){
return isset($var[$key]) ? $var[$key] : $default;
}else{
$result = array();
if(isset($key[0])) #numeric
foreach($key as $k)
$result[] = g($var, $k);
else #associative
foreach($key as $k=>$v)
$result[$v] = g($var, $k);
return $result;
}
}
?>
So, if $key is an array you get an array back composed only of the keys you specify in the array, where keys that don't exist get NULL. And if you specify an associative array you can transform the keys in an array from the keys in that array to the corresponding values.
I also use this function everywhere:
<?php
function e($str){ echo htmlspecialchars($str); }
?>
It's way easier to type e($foo) than echo htmlspecialchars($foo) all over the place. I have a lot of these little functions I use everywhere on my PHP page, if you're interested.
Keith Gaughan (http://talideon.com/) wrote:
I'll show you something moronic. I've a function similar to your e() function that I use called eecho(). The following code doesn't work:
<?php
function eecho() {
echo htmlentities(implode('', func_get_args()));
}
?>
But this does:
<?php
function eecho() {
$args = func_get_args();
echo htmlentities(implode('', $args));
}
?>
Now, that is utterly stupid.
Keith (http://keithdevens.com/) wrote:
That's weird. I'll have to try that later and see why it doesn't work. Another thing is, PHP's parser is too lame to allow things like:
<?php
$foo = "bar baz";
echo explode(' ', $foo)[0]; #should be 'bar', but gives parse error
?>
M. Bean wrote:
This post is why your blog software needs a <--more--> feature to paginate for you.
Keith (http://keithdevens.com/) wrote:
You mean, because this post was too long for the whole thing to show comfortably on the home page? You're saying you'd like something like what Sam Ruby does?
Feel free to post a comment below. Please see my comment policy.
Formatting Rules (No HTML):
Generated in about 0.884s.
(Used 8 db queries)
I hear ya, it's a pain in the ass to get notices for every unset GET or POST variable, and it's exactly the reason why I usually turn off notices, even during developing (which I shouldn't do).
I like your functions though, and I'll probably use them from now on.