Using variables in CSS

I’m working on a little PHP script to allow for variables and inheritance in CSS. I know there’s a ruby script that does this somewhere, but I fancy seeing if we can’t get a pure PHP version up & running between us. If you’re somebody that writes their own CSS and are willing to have a play with it to see what problems occur then I’d really appreciate it. If you want to tighten up my regular expressions a bit as well then it probably won’t go amiss, as I’ve only spent 5 minutes on them so far 😉

It’s fairly straightforward, and at the moment just runs some simple find & replace on a stylesheet, nothing too fancy, and it seems to hold it’s own on my dev server, but I’d be interested to see if it works in the hands of others. I don’t have much spare time but will be adding attribute inheritance next – which could be very cool.

you can grab the PHP here, and implement it by saving it as PCSS.php (or whatever you choose) and then linking to your stylesheet as follows:

[html]
css" media="screen" rel="stylesheet" />
[/html]

In case that isn’t clear – instead of linking to your stylesheet within the tag, you link to my PHP file and then tag a ‘?’ and the filename of your stylesheet onto the end.

once running you can declare variables in your stylesheet ‘thusly’, ensuring each begins with a $ and ends with a  line-break:

[css]
/*
$pie_blue = #443300
$pie_border = 1px solid #CCC
$pie_padding = 1em
*/

.class {
color: $pie_blue;
border: $pie_border;
}
[/css]

and so on…

Again, if anybody does pick this up and play with it, I really appreciate the time and help.

Limiting the number of times a PHP loop will run using the break command

If you’re reading this page then you’re already likely familiar with the standard PHP for, while and foreach loops. If not, then may I suggest some background reading first – http://www.php.net/manual/en/language.control-structures.php.

That said, I added a new weapon to my PHP arsenal today in the form of a better understanding of the break command.

I’ve used the break command before of course – it sits inside switch statements to stop each case running into the next one as below:
[php]

switch ($i) {
case "apple":
echo "i is apple";
break;
case "bar":
echo "i is bar";
break;
case "cake":
echo "i is cake";
break;
}

[/php]
But I didn’t realise that it can be used within any loop to stop the cycle continuing there and then.

This came in handy today when needing to perform a particular loop 8 times only. Normally one would just use a for loop, as that is what they’re -um- ‘for’, but in this instance I wanted to iterate across an array 8 times only. In essence, I wanted a foreach loop to run 8 times and then stop, regardless of how many items it contained.

Usually I would have either used array_slice() to reduce the array to 8 elements only, or introduced a counter and an if statement to my foreach loop as follows:
[php]

$i = 0;
foreach($array as $value){
if($i < 8){
echo $value;
}
$i++;
}

[/php]
Whilst this would work, it’s not an ideal solution because the foreach statement needs to perform the logical test ‘if $i < 8' for every item in the array – of which there could potentially be thousands. Which is where break comes in. Changing my foreach loop to the following…
[php]

$i = 0;
foreach($array as $value){
if(++$i == 8) break; //edit suggested by CHris
echo $value;
}

[/php]
means that as soon as the loop hits the ninth iteration (when $i is equal to 8) the break command is executed and the loop stops. No unnecessary iterations, and in my opinion much tidier code – which is always a good thing in my book!