PDA

View Full Version : Why PHP needs a lexical scope


Zok
03-04-2009, 01:39 AM
$chan_stats = $vbulletin->db->query_read("SELECT * FROM channel_cache WHERE loc='#totse' OR loc='#zok' OR loc='#halfbaked' LIMIT 0, 10");
$i = 0;
while ($chan = $db->fetch_array($chan_stats)) {
$url_chan = str_replace('#','%23',$chan['loc']);

if ($i%2==0) {$class='alt2';} else {$class='alt1';}
$chan[topic] = htmlspecialchars($chan[topic]);
$topic = '';
foreach(explode(' ',$chan['topic']) as $i=>$t) {
if($i == 0) {
$topic .= Truncate($t);
} else {
$topic .= ' ' . Truncate($t);
}
}
$topic = Truncate($topic,65);
$irc_bits .= '
<tr><td class="'.$class.'">
<div class="smallfont">
<div align="center"><b><a href="http://zb3.zoklet.net/stats2/?loc='.$url_chan.'" title="View more stats" >'.$chan['loc'].'</a></b></div>
<b>Users:</b> '.$chan['user_count'].'<br/>
<b>Messages/minute:</b> '.($chan['line_count']/5).'<br/>
<b>Topic:</b> "'.$topic.'"
<div align="right"><a href="http://widget.mibbit.com/?settings=a27f7346cca1d91393b774f48ad34408&server=irc.slashnet.org&channel='.$url_chan.'">Join the channel</a>
</div>
</td></tr>';
$i++;
}
$db->free_result($chan_stats);


Specifically lines 2,6, and 9.

On line 2 I set a counter ($i) that will get incremented on every iteration of the outermost while loop. It controls the background color for the table row - the <tr> tag.

Now on line 9 I have a foreach loop that I set up to iterate through the words of a string and truncate each of them. This counter happens to have the same name as the counter from line 2 to control background color. PHP is stupid and doesn't set up a new scope for the foreach loop - meaning that when I assign the NEW counter with the same name, it writes OVER the old counter.

Proper scoping would make it so that the $i declared inside the foreach loop would have no bearing on the $i outside of the loop.

Fucking php!

toraton
03-04-2009, 01:59 AM
Also needs namespaces.

For those who are interested, perl has this feature if you utilize the keyword 'my'.

Zok
03-04-2009, 02:07 AM
Also needs namespaces.

For those who are interested, perl has this feature if you utilize the keyword 'my'.

my $toraton; :)

MadVillain
03-04-2009, 02:42 AM
Jesus, my time with ANSI C has lead me to forget that loops can introduce a new layer of scope and my time with Scheme has thrown off my whole perception of scope totally. Time to revisit some of these languages made after 1980 (but abso-fucking-lutely not php or perl).

Zok
03-04-2009, 02:48 AM
Jesus, my time with ANSI C has lead me to forget that loops can introduce a new layer of scope and my time with Scheme has thrown off my whole perception of scope totally. Time to revisit some of these languages made after 1980 (but abso-fucking-lutely not php or perl).

Off topic: You're learning scheme? Is it for an academic course? I absolutely love CL and Scheme! I'm glad to know we've got another schemer around here :)

MadVillain
03-04-2009, 03:02 AM
Off topic: You're learning scheme? Is it for an academic course? I absolutely love CL and Scheme! I'm glad to know we've got another schemer around here :)

Well I learned scheme last semester in a programming languages class, but I'm going through SICP (http://mitpress.mit.edu/sicp/full-text/book/book.html) this semester on my own. Yeah lisps are awesome! Have you heard of Clojure? I've been messing around with it a little bit as of late, as it really has potential to be much better than either CL or Scheme.

Zok
03-04-2009, 03:28 AM
Well I learned scheme last semester in a programming languages class, but I'm going through SICP (http://mitpress.mit.edu/sicp/full-text/book/book.html) this semester on my own. Yeah lisps are awesome! Have you heard of Clojure? I've been messing around with it a little bit as of late, as it really has potential to be much better than either CL or Scheme.

Interesting... *reads up on it*

MadVillain
03-04-2009, 03:37 AM
The idea is to integrate a lisp with the jvm and modernize lisp by adding support for more first-class data structures (clojure has really lispy hash maps for instance, much better than the ones in CL). It also has the benefit of having mostly immutable data structures, which simplifies some aspects of concurrency. The real win though, in my opinion, is putting it on the jvm. It gives you access to all the java libraries, integration with any java code you write, and actually pretty decent performance.

It's still kind of a work in progress right now though. They just added mutual recursion (ability to recurse between two or more functions), which is kind of a basic feature in functional programming, and there's still no tail call optimization in the jvm, though clojure has a kind of circumvention to this problem.

Anyway I'm kind of hijacking this thread so maybe I'll start a thread on lisps.

deus
03-10-2009, 01:38 PM
Just name your iterators uniquely :p

You're right, I guess. Never seemed a big deal to me, though.

Zok
03-15-2009, 11:14 PM
Just name your iterators uniquely :p

But I shouldn't have to! I should never have to worry about what my iterator's name is for loops because the loop structure defines a new scope. The iterator should also be destroyed after the loop is terminated (because the it's out of scope).

Phinehas
03-16-2009, 02:20 AM
Why don't you just rename the second i variable? :rolleyes:

deus
03-16-2009, 10:36 AM
But I shouldn't have to! I should never have to worry about what my iterator's name is for loops because the loop structure defines a new scope. The iterator should also be destroyed after the loop is terminated (because the it's out of scope).

I know, just messing. :p

Why don't you just rename the second i variable? :rolleyes:

That's kinda like saying "why have function scope? Just make sure your global variables aren't named the same!", although to a lesser extent.

Zok
03-20-2009, 05:00 AM
Why have scope at all?!

Hell, we should also go ahead and define all functions on the global level! Fuck packages and modules. I want it all included.