ghacks Technology News

How echo works in PHP

The most basic command, and probably the first you’ll learn when taking a look at PHP is “echo”. The first example in many books and online tutorials is the following. Create a file, give it an extension of “.php”, upload it to your server, and edit it like so:

<?php
echo ‘Hello World’;
?>

I hate Hello World examples, but this shows what we are doing pretty well. Basically the echo command will write out that phrase “Hello World” (the quotes will not be shown, the single quotes above are part of the code), so if you open that file using Firefox for example you should simply see the phrase.

This seems straightforward, but to really understand what’s happening, and to be able to work with PHP efficiently we need to dig a bit deeper to see what really happens when we echo something.

(more…)

Tags: , , , ,
Categories: Web Development

Set a Cooktimer on your laptop

As someone who spends most of the day in front of his laptop I frequently forget things like “Wake me in an hour please”, or “Can you tell me to come out of the bath in 20 minutes?”. Not that I forget, just if I’m enjoying my work time flies by. Cooktimer is a small application I found that comes to my rescue in these cases!

You can of course use it so you don’t forget your dinner in the over, or remember to switch the stove off, but for me there are many other uses, including the ones I mentioned. You can also use it to remember to take a rest every now and again, or if you hate distractions, to remember to check your email every 30 minutes.

The great thing about this app is that it is very lightweight, no installation required and will reside in memory while the timer is active. Many of you already have billions of applications running, and you don’t need to log timings, add multiple timers and so on, so this could be perfect, just set and forget. If any of you have any other timer suggestions, let us know!

(more…)

Tags: ,
Categories: Entertainment, Tools

Using javascript to hide and unhide elements dynamically

My favorite aspect of javascript is that it enables you to add great features to your site like showing/hiding parts of it when a user clicks a link without reloading, get some data from a database and displaying it in a new div, again without reloading. In fact, it would be possible (although not very comfortable and easily codeable) to create a big website, like a Wordpress blog without any reloading at all, no matter where you click.

This is usually referred to as AJAX, although most of it is javascript and HTML, and it is actually easier than it seems. In my opinion calling the hiding and showing of elements is not yet AJAX, because in my eyes ture AJAX communicates with the server in the background. We will only be changing CSS properties and element contents with javascript in this post to achieve our goal.

Let’s say you’re creating a form, and you want a way to explain what the fields do or what data they need to contain, but you don’t want to fill up too much space on screen, and you want to make it as unobtrusive as possible. In this case it would be cool to have a link with the anchortext “Explain”, which would expand a section which explains what the user needs to do.

(more…)

Tags: ,
Categories: Web Development

Basics of looping in PHP

Loops are an integral part of PHP, and many other programming languages for that matter, the basics covered here would apply to javascript as well, even the code is very similar. A loop is simply a block of code that executes multiple times, controlled either directly, by explicitly telling the script to execute “X” times, or by using a variable, telling the script to execute “as many times as the exact hour at the time of viewing” for example.

So why do we use loops? There are hundreds and hundreds of reasons, the widest usage is probably to cycle through values of some sort of data. For example, you might be writing a messaging system in PHP, which would allow users of your website to send private messages to each other once they register. You would write a MySQL query which would pull all the user’s letter from the database. To show all the letters you would use a loop to show all the rows of the query (all the separate messages) on a page.

To create a loop you need to add some rules which will dictate how the loop should behave. Usually we need to give three values, the starting value of the counter (which tells the loop how many times it has executed), an ending value (which tells the script to stop looping if it is reached) and an increment, which deals with changing the starting counter value in some way (so that it eventually reaches the end value, hence ending the loop).

(more…)

Tags:
Categories: Web Development

Web Development: How does PHP work?

When someone starts learning web development they usually start with HTML and CSS, and many people get stuck there without ever experiencing the wonderful workd of server side scripting like PHP or ASP. I’ll be talking about PHP here, but the basic rules for ASP and others are the same though.

The most important thing to understand is the difference between HTML and PHP. In HTML you write your code, upload it, and the user’s will subsequently download that page along with all the code. The user’s browser interprets this code and shows the user the page as you intended it (hopefully). In other words HTML is sort of what you see is what you get, in the sense that all the code goes to the user and is interpreted by the browser.

With PHP it works a bit differently because you don’t actually download the code the author wrote. What happens is that if you want to download a php page the code in that file is first processed by the server, and you download the output of the code, as opposed to the whole code as is. This in turn will be HTML just as before, this is why you never see PHP code in the source of a webpage. So what happens in processing? Turn the page to find out!

(more…)

Tags: , ,
Categories: Web Development

Work in style with the Enigma desktop package

While I’m a huge fan of desktop customization I always actually work with a solution which is the simplest possible. I love elaborite desktop configs, but I find most of the stuff gets in my way. I have found a setup which looks extremely good and easy on the eyes and while still a bit too much for me, it might be just what some of you are looking for. The Enigma Desktop is actually a package containing almost everything you’ll need for your desktop too look this good. You will need to download the apps the creator uses, like Rainmeter and Samurize, but the package contains the config files you need to make things happen.

What I like about the setup is the simple lines and design despite the complexity and elaborate nature of the configuration. Despite my distaste of over-complex designs, I am impressed, because it functions as a “dashboard” should, it gives you a lot of at-a-glance information. RSS feeds from your favorite blog, the time and date with weather, and a whole bunch of other stuff, like the handy Gmail notifier.

While this may not appeal to the hardcore “clean desktop” people, I urge you all to give these setups a try, some of them might work. the reason I stray from them is that when I am working on something I tend to generate a lot of icons on my desktop which ruin the nice looks. I could of course use a different directory, but this is the most readily accessable place. if you know of any other cool desktop configurations like this be sure to drop us a note in the comments!

(more…)

Tags: , ,
Categories: Desktop Manager

Web Development: A brief history of time()

Part of the beauty of PHP to me is the number of really useful variables that are built in. Some of these might seem very odd at first, but once you start creating pages you will run into some problems which you’ll find can be solved by a function which seemed totally useless when you first heard of it. One of these functions for me was time().

Echoing the time() function will give you the amount of time passed since the Unix Epoch in seconds. Say what? Epoch means a point in time chosen as the start of a period or an era and thus the Unix Epoch is January 1 1970 00:00:00 GMT. So echoing the time() function will give us “1230978041″ at the time I’m writing this, meaning that 1,230,978,041 seconds have passed since then. So why is this useful to us?

Mathematically it gives us a much easier way of keeping track of time. Sure, 2008, Jan 15th might seem all nice and organized to you, but to calculate the days passed from the 15th of Januaray to the 17th we’s have to strip the numbers of “th”, and in more complicated cases perform a bunch of other string changes. Using time you can essentially assign a number value to any given second, making it much easier to use, especially as a counter.

(more…)

Tags: , ,
Categories: Web Development



© 2005-2009 Ghacks.net. All Rights Reserved. Privacy Policy - About Us