Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What’s the right way to embed PHP code in my CSS and JavaScript files?

Like everyone else I'm storing my site`s display information in style sheet files. And I want to create back-end cms so users will be able to, for example, change < h1 > color, size etc.

So, what's the right way to embed PHP code in my CSS file(s)?

Is adding this header:

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

And changing file extension in link:

<link rel="stylesheet" type="text/css" media="screen" href="style.php">

Valid?

And what about JavaScript? At this moment I'm echoing <script type="text/javascript"> tags, but maybe there's also a way to embed everything in the .js files?

Thanks!

like image 468
fomicz Avatar asked Nov 24 '10 12:11

fomicz


People also ask

Can you embed PHP in JavaScript?

We can't use "PHP in between JavaScript", because PHP runs on the server and JavaScript - on the client. However we can generate JavaScript code as well as HTML, using all PHP features, including the escaping from HTML one.

How will you embed the PHP code in the HTML?

We can use PHP in HTML code by simply adding a PHP tag without doing any extra work. Example 1: First open the file in any editor and then write HTML code according to requirement. If we have to add PHP code then we can add by simply adding <?

How can PHP and JavaScript interact?

JavaScript is used as client side to check and verify client details and PHP is server side used to interact with database. In PHP, HTML is used as a string in the code. In order to render it to the browser, we produce JavaScript code as a string in the PHP code.

What is the procedure for accessing PHP using Web browser?

If you installed a web server in your computer, usually the root of its web folder can be accessed by typing http://localhost in the web browser. So, if you placed a file called hello. php inside its web folder, you can run that file by calling http://localhost/hello.php.


2 Answers

Yes, this is perfectly valid.

The same can be done for Javascript too by sending

<?php header("Content-type: application/javascript"); ?>

However, this is not optimal from a performance point of view because a PHP process has to be started for serving those resources.

If you have only very few dynamically changing CSS properties or JS variables, I would consider putting them into the document's head and continuing to serve the external files statically.

Remember that usually, there are no caching headers sent for PHP files. You'll have to take care of sending the correct headers inside your PHP script! Cheers @oracle certified professional for the reminder.

like image 78
Pekka Avatar answered Oct 03 '22 14:10

Pekka


What you're doing is absolutely valid.

However if you're running a bigger site with many visitors, you should concern to just let PHP "build" a "real" CSS file when your user updates his or her design to save your servers performance to better needed things:

<?php

header("Content-type: text/css");

// Your database magic here to fetch the user-specific designs 

// open the cached css
$cachefile = "cachedCSS/mycss.css";

if (file_exists($cachefile)) {
    // the page has been cached from an earlier request
    // output the contents of the cache file
    include($cachefile); 
    // exit the script, so that the rest isnt executed
    exit;
}

$fp = fopen($cachefile, 'w');
// save the contents of output buffer to the file
fwrite($fp, ob_get_contents());
// close the file
fclose($fp);
// Send the output to the browser
ob_end_flush(); 

Read more about it here: http://www.theukwebdesigncompany.com/articles/php-caching.php

like image 27
Industrial Avatar answered Oct 03 '22 15:10

Industrial