PHP: very dirty scoping...

So I'm still helping my friend out with some captcha stuff and we've hit a small problem with one of the image functions. I start talking to another friend of mine, scuzzy (Daine), about the problem and he suggests I use another function. Basically the line of code that wasn't really working was this one:

imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);

Daine suggested I just use the imageline() function, but make it reall small, so it looks like a dot (we settled on imageputpixel though, but thats got nothing to do about it). imageline() needs an extra two x/y params, but in our case, they have to be the same as the original to simply create a dot. But I'm lazy and I hate lots of lines of code, so I rekon I'll do it the dirty way.

imageline($image, $tmpx = mt_rand(0,$width), 
	$tempy = mt_rand(0,$height), $tmpx, $tempy, $noise_color);

The important part to not here is the declaration of a var(s) inside the function call and then using the vars again, inside the same function call: $tmpx = mt_rand(0,$width), $tempy = mt_rand(0,$height), $tmpx, $tempy. After writing this code, we got onto a casual discussion on how much of a "dirty" language PHP can be. Sometimes it's sexy dirty and sometimes it's gross dirty. Most lanuages wouldn't even allow you to declare a variable in the function call, never mind allow you to reuse it. That got me wondering though, if PHP will allow me to delcare in the function call and then reuse the variable in the same function call, I wonder if it will let me reuse the var outside the function call. This spawned the following piece of test PHP code:

<?php
 
	function foo($integerVar) {
		echo "The value is: ".$integerVar."<br>\n";
	}
 
	foo($var = 5);
 
	echo "The value still is: ".$var."<br>\n";
 
?>

Which of course outputted the following:

The value is: 5
The value still is: 5

I don't care who you are, but thats dirty. Whether you think its sexy drity or gross dirty is up to you, I personally think it's gross dirty.

I then wondered if I could do the same thing in C#, so I fired up Visual Studio to find out.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ScopeTest
{
    class test
    {
        static void foo(String integerStirng)
        {
            Console.WriteLine("The value is: " + integerStirng);
        }
 
        static void Main(string[] args)
        {
 
            foo(String newString = "5");
 
            Console.WriteLine("The value still is: " + newString);
 
            Console.ReadKey();
        }
    }
}

Well, I didn't even get to compile the code, because Visual Studio was quite happy to point out to me that I'm a complete idiot for trying to declare a String inside a function call by underlining that bit of code with a nice big red marker.

It really makes you wonder how PHP handles its scoping compared to other languages...

Syn

Every assignment returns a value

It's very easy to explain why your script does this: In PHP, every assignment returns a value (the one you chose).

echo $myvar = "A String";
 
echo "<br/>";
 
echo isset($myvar)? '$myvar is set' : '$myvar is not set';

So this code would print...

A String
$myvar is set

But you're right, it's strange...