blog icon

PHP variables inside external CSS

There’s a way of using php variables inside your css external file.
First, you have to rename your css file to “mycssfile.php”.
Then, insert the following line of code on top of the file:

  1. <?php header("Content-type: text/css"); ?>

Finally, don’t forget that the reference to the css file in your HTML is “mycssfile.php”.

Inside “mycssfile.php” you can define variables like colors, typography, measures, etc…

  1.  
  2. <?php
  3. $color1 = "#CCCCCC";
  4. $color2 = "#FFFFFF";
  5. $type1 = "Helvetica";
  6. etc…
  7. ?>
  8.  
Filed under: Blog, Tutorials

Random Image using PHP

Ok… this is really going to be simple.
You want to display a random image inside, lets say, a header div.

For the sake of simplicity, put all the images you want to display inside a folder and name them something1.jpg (or png, or gif, or whatever), something2.jpg, something3.jpg, etc…

Now you only have to generate a random number using the php rand() function. This function may receive two parameters: a minimum value and a maximum.
Example: rand(1,30) will return a number between 1 and 30 (inclusive).

In this example I have only 5 images inside a “images” folder. They are named “img1.jpg”, “img2.jpg”, etc…

Filed under: Blog, Tutorials

CSS variables with PHP

Want to have css variables in your css files?
Here’s a simple solution to have variables in your css.
Well, in fact this are PHP variables inside your css files.

Define all the variables that you want in your HTML document like this:

  1.  
  2. <?php
  3. $color1 = ‘#BB180E’;
  4. $color2 = ‘#666666′;
  5. ?>
  6.  

Use them in your CSS file like this:

  1.  
  2. h1 { color:<?php echo $color1 ?>; }
  3. p { color:<?php echo $color2 ?>; }
  4.  

Here you can see an example.

Filed under: Tutorials