An Introduction to PHP

Posted By: Jas Singh

PHP is a widely used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. This blog post is an attempt to give an overview of the PHP language and it’s various features.  This blog post assumes that you already have a web server (eg: apache) and PHP installed on your box. You can also use some easy to install Apache distributions containing MySQL and PHP such as XAMPP  and MAMP.
So let’s get started!

The default delimiter syntax (recommended) opens with <?php and concludes with ?>, like this:

<?php
    echo "Hello World!!";
?>

The other alternative syntax requires enabling PHP’s short_open_tag directive, like this:

<?
    print "Hello World!!";
?>

When short-tags syntax is enabled and you want to quickly escape to and from PHP to output a bit of dynamic text, you can omit these statements using an output variation known as short-circuit syntax:

<?="Hello World!!";?>

You can also use the <script> tags to delimit PHP code

<script language="php">
   print "Hello World!!";
</script>

The last syntax uses ASP style delimiters, however this style syntax has been removed as of PHP 6. See the example below

<%
    print "Hello World!!.";
%>

Data Types in PHP

The following are the different data types available in PHP…

Boolean

In PHP, you can use zero to represent FALSE, and any nonzero value to represent TRUE. A few examples follow:

$isTrue = false;      	//  false.
$isTrue = 1;      	//  true.
$isTrue = -1;          	//  true.
$isTrue = 5;          	// true.
$isTrue = 0;           	// false.

Integer

An integer is quite simply a whole number, or one that does not contain fractional parts. Decimal (base 10), octal (base 8), and hexadecimal (base 16) numbers all fall under this category. Examples are 50, -20, 0755(octal)
The maximum supported integer size is platform-dependent, although it is typically positive or negative 2^31. If you attempt to surpass this limit within a PHP script, the number will be automatically converted to a float.

Float

Floating-point numbers allow you to specify numbers that contain fractional parts. Examples are:
1.2345
7.0
1.4e4
2.51E+11

String

A string is any series of characters enclosed in ‘single’ or “double quotes”. An example is:

$color = "maroon";

You could retrieve a particular character of the string by treating the string as an array, like this:

$parser = $color[2]; // Assigns 'r' to $parser since 'r' is at position 2 [m(0) a(1) r(2)]  in the array above.

Compound Data Types

Array

Arrays are one of the most powerful data types available in PHP, due to their ability to map information using key to value pairing. Example:

$state[0] = "Alabama";
$state["Alabama"] = "Montgomery";

$myState = array('name' => 'Texas',
		    'Capital' => 'Austin');
$myState['name']; // prints Texas

That was our first intro into the PHP language, but be sure to check back often as I will be continuing to dive into PHP. The next post is going to be all about variables!

Jas Singh is a Technical Lead for Fellowship Technologies. He has been with Fellowship Technologies since July 2007. He believes that every new day brings opportunities to learn something new.

Posted In: API, Tips,

Comments:
No one has commented yet. Be the first!
Commenting is not available in this channel entry.

Categories:

Previous Posts:


Subscribe to the RSS feed!