The Importance of Scripting Languages – Sample PHP Tutorials |
Whether you spend your day working fulltime as a web server administrator, or you like to host a simple website on a spare computer in your basement, one of the best ways to increase your productivity and develop very useful tools is to write programs called scripts. Scripts are normally short, high-level computer programs that are easy to write and let you accomplish a wide range of objectives, from automating tedious tasks to increasing interaction between different programs on your computer.
Determining the best scripting language for you may depend on what languages are available on your given system. Some languages are very easy and let you accomplish many things with one line of code, whereas others are more verbose, yet more powerful. The language you choose is entirely up to you, and I recommend exploring several different languages before you start to dig in on one.
Over the next few weeks we’re going to cover many tutorials on different types of scripts you can write. We’ll provide the sample code in different languages like PHP and Ruby, so you can get a good idea on how each language works. Below are a few, short scripts written in PHP that let you easily add some cool functionality to your website. If your server or computer has PHP functionality, simply copy this code into a file with a “.php” extension to test it out!
Add a Random Quote to Your Website
This sample code will read in a random quote from iheartquotes.com, and print that quote on your website. To change the type of quote, go to http://www.iheartquotes.com and select a category name. Update the section of the code that reads $src = “albert_enstein”; by replacing albert_einstein with your new category, and then updating the line that reads “A random quote from Albert Einstein<br><br>”; with your category’s name.
<?php
$src = "albert_einstein";
echo "A random quote from Albert Einstein: <br><br>";
echo nl2br(file_get_contents("http://www.iheartquotes.com/api/v1/random?source=$src"));
?>
Quickly Count the Lines of a File
This sample code will read a file and count the number of lines. This code works with text files on your computer, or with web pages. Simply change the Web Hosting Blog address to point to either a text file on the internet or on your computer.
<?php
$file = "http://blog.readysetconnect.com";
echo "The number of lines in the file is: " . sizeof(explode("\n", file_get_contents($file)));
?>
Although short, these two scripts show how easy it can be to add a lot of functionality to your set of tools with relatively little code. We will post in-depth code samples in the coming weeks.

(-1 rating, 1 votes)







Leave a Reply