KBD

Keith Devens .com

Friday, May 16, 2008 Flag waving
That is because you crazy! – Ikea
← RSS auto-discoveryAggregator →

Daily link icon Saturday, June 1, 2002

Wow, PHP really does suck

No offense, that title sounds harsher than I really mean it in a way. PHP's stated goal is to help solve the "web problem". Unfortunately, as a language it kind of sucks. I've been having major problems writing some code I'm trying to write, so I decided to write my code in a largely functional style. PHP can't do that!!

Well, to be fair, maybe it can, but I have to force it to Smiley I'll let you know how I make out.

Ok, some examples. I want to write a function to test if a "key" matches a certain "tag". A key matches a tag if, given a tag of "tag", the key is either "tag" or "tag<space>[anything else]". So, I'd want to write something like this functionally:

<?php
function isKeyEqual($key,$tag){
   return 
substr($key0strpos($fullkey' ')) == $tag;
}
?>

Unfortunately, substr won't work right if the space is not found, since strpos will return false if the space isn't found, which will be interpreted by substr as '0', which will make it always return the empty string.

substr isn't amenable to a functional style. If it took 0 to mean "the end of the string", or even NULL, I could do something like:

<?php
   
return substr($key0,
            ((
$pos strpos($fullkey' ')) !== false $pos NULL)
          ) == 
$tag;
?>

But that doesn't work either. So I have to go fully back to the imperative style as so:

<?php
function isKeyEqual($key,$tag){
 
$pos strpos($full_key" ");
 if(
$pos !== false){
  return 
substr($full_key0$pos) == $tagname;
 }else{
  return 
$full_key == $tagname;
 }
}

Boo.

Update: I finally got my algorithm right. I think I'm finally done!

← RSS auto-discoveryAggregator →

Comments XML gif

Johan Abildskov (http://Qig.dk) wrote:

Well, you could do it with a regexp pattern in the likes of:
/^tag.$/ which matches anything that starts with "tag".

∴ Johan Abildskov | 29-Jun-2005 6:23am est | http://Qig.dk | #7815

Marcel Kroetz wrote:

Your final code isn't imperative at all, the assignment

$pos = strpos($full_key, " ");
...

is funtional equivalent to a let in sml, like

let pos = strpos(full_key, " ") in
...

and the if's that you use, but the return stantment, is functional equivalent.

In my opinion, the final code still functional and is more readable!

∴ Marcel Kroetz | 29-Jul-2005 11:06pm est | #8018

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

May 2008
SunMonTueWedThuFriSat
 123
45678910
11121314151617
18192021222324
25262728293031



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 1 posts

Recent comments XML

"IMDB for music"

IMDB for Music? It looks to be a​couple of years old...​http://MusicTell.co...

Ken Empie: May 14, 9:57pm

Generated in about 0.689s.

(Used 8 db queries)