Add to Technorati Favorites Top100Inter.Net

Of PHP and Javascript

PHP, my choice?

PHP, my choice?

I was helping my friend doing his HTML assignment, when I saw what his lecturer asking his class to do a labwork, which, surprise surprise, includes also Javascript. Why am I surprised? Because during my time, we don’t have to learn it. Just basic HTML coding. Why on earth the lecturer wanted them to learn this I have no idea.

Javascript is good. I mean the shoutbox at the right is using Javascript to support AJAX. The zoom effect that the images on this site also uses Javascript. BUT, the difference between Javascript and PHP makes it not really a good option to use on the whole site. Javascript is Client Side Scripting, compared to PHP which is Server Side Scripting. This means that while PHP’s script is executed on the server side before being sent to your browser, Javascript is compiled by your own browser. Too many Javascript can lead to a crawldown for your PC. Some browsers *cough*firefox*cough* can also rendered nonresponsive when rouge script creates memory leaks.

Take this for example. I have to use Javascript to change the browser’s scrollbar colour. Thus in the html file, I have to do this

<head>
<script type=”text/javascript”>
function newScroll(){
document.body.style.scrollbarFaceColor=”#990100″;
document.body.style.scrollbarTrackColor=”#b51531″;
}
</script>
</head>

Then I had to tell the page to load the script.

<onload=”newScroll()”>

And yet, using CSS, I can do the same thing, and without even have to load the function in the Body tag:

body {
scrollbar-face-color: ##990100;
scrollbar-track-color: ##b51531;
}

We also have to create an output that shows the current date

Today is Tuesday, November 04 2008.

Using Javascript, this is how you do it

var now = new Date();
var days = new Array(
‘Sunday’,'Monday’,'Tuesday’,
‘Wednesday’,'Thursday’,'Friday’,'Saturday’);
var months = new Array(
‘January’,'February’,'March’,'April’,'May’,
‘June’,'July’,'August’,'September’,'October’,
‘November’,'December’);
var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
function fourdigits(number) {
return (number < 1000) ? number + 1900 : number;}
var weekDay = days[now.getDay()];
var monthDate = months[now.getMonth()];
var numDate = date;
var year = (fourdigits(now.getYear()));
document.write("<h3>Today is "+weekDay+", "+monthDate+" "+numDate+" "+year+".");

See how we need to declare the variable? In PHP, this would do the same output.

echo “<h3>Today is”.date(’l, F d Y’).”</h3>”;

What a massive difference! For one, the date() function in PHP is more powerful compared to the Javascript’s one. We have to manually assign to an array all the name of the days, and months in Javascript, while in PHP, they are already built in the function. Note that you also don’t have to declare any variable when you want to use the function, just use it as it is. If anything using the PHP function would save time and also page size. Less page size would mean faster loading. And these days, page load time is everything.

This is just the tip of the iceberg of how stupid the course have been. Yes, it’s good to learn Javascript. But why bother? Better wait until they have learn OOP (Java) and also PHP next semester instead of pushing this now down their throat. As of right now, this is just nuts and won’t do them any good at all. I also learn PHP first before dabbling with Javascript. Better walk first than run I say.

Post to Twitter Post to Facebook

1 comment to Of PHP and Javascript

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>