Recently in PHP Category

PHP Basic Questions

| No Comments

One great thing about PHP is it is great for people that are new to web programming, it's very easy to start with and also can get really technical for the veterans. PHP started as Personal Home Page tools, it was started to assist new users with programming. The syntax is very easy to use and is very inituitive. PHP is a good language to start on and learn the basics of programming. With the tutorials on this site you should be able to get started using PHP to create dynamic web sites.

How does PHP work?

PHP is a server side scripting language. So PHP does the work on the 'back-end', preprocessing web sites and inserting dynamic data before they are displayed. Unlike JavaScript which is a programming language that works while the page is being viewed by the browser on the users computer. PHP is very similar to Macromedia's Coldfusion and Microsoft's ASP.NET.

All you do is just embed the code into an HTML page and give it a .php file name extension and you are good to go.

What do I need to get started?

Two things; a web server running PHP on it and a HTML editor. Most web hosting companies (including ours 'shamless self advertisement') offer PHP for no additional cost. This is a major advantage over Macromedia's Coldfusion which is an extra charge for most hosting companies (except ours, we offer free Coldfusion hosting - 'sorry I couldn't resisit).

What are some uses for PHP?

PHP can handle the output from HTML forms, collect the data, then email it to someone or enter it into a database. You can even write your own online store, message forums, pretty much anything you can think of that uses data and writes to a database. In short to give the basics you can :

  • Access databases
  • Access files on your server
  • Access system commands
    • This is especially interesting - many system administrators could use this to run common applications on their server. For example I had an old Perl script that ran and cleaned up a lot of un-needed files on our server. That Perl script can still be used and call upon from PHP, very powerful stuff.

How much does PHP cost?

As state above PHP is free. How is that? Three words; open source software. Open source software is very different from non-open source software companies. The term means that the source code is available for all to see, anyone has the ability to view the code to see how PHP operates, the same can not be said about Microsoft's Windows code which is guarded heavily away. PHP and open source software as whole operates on a very large (and very wonderful) community, there is no giant corporate office that runs it. There are many resources out there to learn this stuff as you can see by reading this site :)

Format data with PHP

| No Comments

Now we can learn how to change the output of certain variables. For example if you have variable containing the number 3, what if you want to output it as $3.00, or $0.03 ? How can we do that easily? There are some other cool things we can do but as usual; lets actually view the code and then explain how to do it. There are two commands we start to use here, printf() and sprintf().

printf("format",$firstvariable, $secondvariable);
$variable = sprintf("format", $variable1, variable2);

printf() outputs formatted strings and the sprintf() does the same thing but instead outputs into a variable to be stored and used for later.

$variable1 = 'Tom';
printf("% says hello",$variable1);

Here we have the printf() function you will notice the % tells PHP to output the variable supplied as the second argument to the function. We could stop there but printf() has so many more neat functions, let see. What other things we can add to that lonely %. The syntax is as follows:

%padding-width.decimaltype

  • The padding is a padding character that we can use to fill in the blanks so to speak with the value we use is smaller than the formatting width specified. (We haven't done that yet). If you don't specify a padding character, no big deal; a space is used.
  • The - symbol makes the text left justified, if this is not present it will be right justified.
  • The width is the number of characters to pad using the padding character.
  • The decimal is the decimal places to use.
  • The type is the type of value, the command values are s for string or f for float (or numbers with decimal places)

For example if the padding character is 0 and the with is 3 and the value is 5 then output would be.

001

Ok enough of all that, let's see it in action!

$paycheck = 250;
$newvariable = sprintf("My paycheck is $%03.2f");
printf($newvariable);

The output would be :

$250.00

The $%03.2f is interpreted as follows; the % says to start outputting the variable. The 0 says to pad using variables. The 3 says to put 3 places to the left of the decimal point and the 2 says to go 2 places to the right of the decimal point.

Other Output Functions

Let's take a look at some other output functions, these are really quick simple examples, starting with Strtoupper().

strtoupper() - Converting to upper case

$newvariable = strtoupper($variable);
printf($newvariable);

Takes the $variable and outputs in all uppercase. You can use unfirst() to capitalize only the first letter in the string to uppercase, likewise ucwords() will upper case each word in the string to uppercase.

strtr() - Replacing values

$variable1 = "1 6 3";
$newvariable = strtr($variable1, "6","2");
printf($newvariable);

This takes $variable1 and reads the string, replacing 6 with 2, then outputs 1 2 3

substr() - Returns value inside of string at a specific point

$newvariable = substr("tommy", 2,4);

Reads "hello" and returns everything from the 2nd character to the 4th character. Outputting:

mmy

substr_count() - Counts number of times a given item is in a variable

$variable1 = "tom 123 tom 123 tom 123";
$find = "tom";
$numberoftimesfound = substr_count($variable1, $find);

In this case the variable $numberoftimesfound would have a value of 3 because it found tom 3 times in $variable1.

Using Date Format

Ok now we did some cool stuff, let's see how simple it is to use the date since that is used very heavily in most PHP applications. Like almost everything else in PHP, accessing the current date is very easy, let's take a look.

$today = date("d/m/y");

Would give the $today variable a value of 04/01/2006. Very easy! :)

PHP Hello World

| No Comments

This will be a really easy starter tutorial and will go over how to create a Hello World PHP application. I'll be doing a few of these easy starter tutorials then grow from there. We'll be doing PHP, Python & Ruby on Rails. So let's get started with PHP. I'm assuming you already have PHP installed.

Whatever text editor you use; type this data into the file.

<html>
<head>
<title> PHP Tutorial</title>
</head>
<body>
<p>This is only a test</p>
<?php
echo "<p>...from PHP</p>";
?>
</body>
</html>

Save it as test.php, then upload it to your web server.

Access the web site in your web browser and you should see the following output:

This is only a test

...from PHP

And there you have it! Easy wasn't it?

Line by Line Explanation

Basically the only line that has to do with PHP is the one that says :

<?php
echo "<p>...from PHP</p>";
?>

The <?php part means to start interpreting the following as PHP not HTML. The echo "<P>...from PHP</p>"; means to echo (display) the code <p>...from PHP</a> which of course is standard HTML. The quotes are necessary as the trailing ; to show the end of the line.

Take a look at Marylandlawyersearch.net. It's a web site I created so I could help my law firm clients. The idea is simple, its a directory of all my clients. The site could be considered a mash-up. It takes my client data and uses Google's Map API to display them. I also use jQuery because its just so damn fast and helpful.

My first concern was how to get my data dynamically into the site. I didn't want to have to update it every time I added a new client. Well I'm kinda starting in the middle here. I've already created a program to hold all my clients. I did this extremely quick using Ruby on Rails. This tutorial assumes you already have your client data in a database and you can access it via XML. I'll come back later and show how I made my client data program. With Ruby on Rails it was extremely easy to put it all together.

Twitter Feed