Title: PHP variable as css value
Last modified: August 20, 2016

---

# PHP variable as css value

 *  [dtek516](https://wordpress.org/support/users/dtek516/)
 * (@dtek516)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/php-variable-as-css-value/)
 * Hi,
    I’m trying to change the value of:
 *     ```
       .entry{
       transform:rotate(<?php echo $random_number; ?>);
       -ms-transform:rotate(<?php echo $random_number; ?>); /* IE 9 */
       -moz-transform:rotate(<?php echo $random_number; ?>); /* Firefox */
       -webkit-transform:rotate(<?php echo $random_number; ?>); /* Safari and Chrome */
       -o-transform:rotate(<?php echo $random_number; ?>); /* Opera */
       }
       ```
   
 * on each post in the loop, to turn the thumbnail at a slightly different angle
   for each post.
 * (The to mimic a polaroid being thrown down)
 * I’m using this php function:
 * `<?php $random_number = mt_rand(-20, 20);?>`
 * Any help would be greatly appreciated.

Viewing 9 replies - 1 through 9 (of 9 total)

 *  Thread Starter [dtek516](https://wordpress.org/support/users/dtek516/)
 * (@dtek516)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/php-variable-as-css-value/#post-3141523)
 * by the way:
    i tried the method here, to no avail.
 * [http://css-tricks.com/css-variables-with-php/](http://css-tricks.com/css-variables-with-php/)
 *  [mojowill](https://wordpress.org/support/users/mojowill/)
 * (@mojowill)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/php-variable-as-css-value/#post-3141545)
 * and is this css inline in your template? It obviously wouldn’t work in your style.
   css but could work if you make it inline.
 *  Thread Starter [dtek516](https://wordpress.org/support/users/dtek516/)
 * (@dtek516)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/php-variable-as-css-value/#post-3141550)
 * i did try, but i was having a very hard time entering/exiting the php and html.(
   im a graphics guy..lol)
 * would you happen to know how to do it?
 *  Thread Starter [dtek516](https://wordpress.org/support/users/dtek516/)
 * (@dtek516)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/php-variable-as-css-value/#post-3141554)
 * i figured i could use
 *     ```
       <?php $random_number = mt_rand(-20, 20);?>
       <div class="entry" style="
       transform:rotate(<?php echo $random_number; ?>);
       -ms-transform:rotate(<?php echo $random_number; ?>); /* IE 9 */
       -moz-transform:rotate(<?php echo $random_number; ?>); /* Firefox */
       -webkit-transform:rotate(<?php echo $random_number; ?>); /* Safari and Chrome */
       -o-transform:rotate(<?php echo $random_number; ?>); /* Opera */
       ```
   
 * _[Moderator Note: Please post code or markup snippets between backticks or use
   the code button. As it stands, your code may now have been permanently damaged/
   corrupted by the forum’s parser.]_
 * but i dont know how to format it for html/php
 * I also wanted it to pick a different random number for each post in the loop.
 * Thank you very very much for any help you can provide. 😀
 *  [Gagan Goraya](https://wordpress.org/support/users/gagan-goraya/)
 * (@gagan-goraya)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/php-variable-as-css-value/#post-3141565)
 * mt_rand is automatically seeded on each execution so u will get a diff number
   every time when it executes in a loop.
 * And take out those CSS comments, you do not need those here. The /* … */ things.
   And put closing quote at the end of the style definition I.e, “>
 *  [mojowill](https://wordpress.org/support/users/mojowill/)
 * (@mojowill)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/php-variable-as-css-value/#post-3141566)
 * Your pretty much there just make sure it’s inside your loop and you will get 
   a different number.
 *  Thread Starter [dtek516](https://wordpress.org/support/users/dtek516/)
 * (@dtek516)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/php-variable-as-css-value/#post-3141602)
 * Thanks guys. But still nothing. 🙁 Do I need to enclose anything in quotes or
   escape anything?
 *  Thread Starter [dtek516](https://wordpress.org/support/users/dtek516/)
 * (@dtek516)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/php-variable-as-css-value/#post-3141603)
 * sorry, i think i may be on to it.
 * the value i needed was:
 *     ```
       transform:rotate(<?php echo $random_number;?>deg);
       ```
   
 * with that added ‘deg’.. i tried to just add it,as you see, but its not showing
   up in the source code when rendered on the page. should i be using quotes?
 *  Thread Starter [dtek516](https://wordpress.org/support/users/dtek516/)
 * (@dtek516)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/php-variable-as-css-value/#post-3141604)
 * GOT IT! You guys were a big help!!! Thank you so much, have a great day. 😀

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘PHP variable as css value’ is closed to new replies.

 * 9 replies
 * 3 participants
 * Last reply from: [dtek516](https://wordpress.org/support/users/dtek516/)
 * Last activity: [13 years, 8 months ago](https://wordpress.org/support/topic/php-variable-as-css-value/#post-3141604)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
