Ten minutes to PHP
Written by Ian Elliot   
Thursday, 12 May 2011
Article Index
Ten minutes to PHP
User interaction
State and session information

Want to get started with PHP but never found the time? Now you can write your first program in around ten minutes and understand where to go next.

There is one snag to this idea however. It takes longer than 10 minutes to do it justice!

What we are going to try to do is to give a sketch of PHP that a programmer would understand. The emphasis isn't on covering everything PHP can do, but on the things that make PHP different or special. or the things that are very common in PHP.

Your first problem is in installing a PHP environment that you can use to test things. Most versions of Linux come with a copy of PHP. If you don't have Linux consider downloading and installing a virtual machine  and then a LAMP virtual appliance. Even if you are running Windows this can be the quickest way to get up and running. If you really want to install PHP under Windows then try WAMP. In many ways getting PHP and a web server setup is the most difficult part of getting started but there are so many ways to do it that describing each one in detail would take too long.

If you are only trying out PHP then I strongly recommend that you consider a virtual appliance. Download an install Virtual Box or VMWare Player then download a LAMP appliance and simply start work. When you have finished you can just delete the virtual machine or install a fresh copy.

Hello PHP world

Assuming that you have downloaded PHP and installed it what else could you write to test it with a  “Hello world”!

Before you do this you need to check that you can read standard HTML pages from the web directory that you are going to store the PHP pages in. Use a general-purpose text editor, Notepad for example, to create a file called Hello.php containing the following lines:

<?php
print "Hello PHP world"
?>

Save this into the web folder and then load it into a web browser using the address /server/folder/hello.php. With luck what you should see is simply the line:

Hello PHP world

Although this looks trivial, notice that the processing was done at the server’s end of the connection. The PHP command “print” was obeyed by the PHP interpreter back on the server and the result of the command “Hello PHP World” was then sent to the web browser. This means that you can use PHP to create standard HTML web pages dynamically and interactively. The big advantage of a server-side script is that the browser has to deal with nothing but HTML.

 

In most cases the PHP program within the web page would be embedded in HTML. For example,

<html>
<head>
<title>PHP Program One</title>
</head>
<body>
<?php
print "Hello PHP world"
?>
</body>
</html>

The rule is that anything you enter into a .php file is treated as html and sent to the web browser unaltered unless it is surrounded by </php and ?> in which case it is recognised as PHP code and processed accordingly. Any output the PHP code may produce is also sent to the web browser.

If you can't make this simple example work then you need to check that you have a web server set up, that PHP is installed and that you are saving the php file to a folder that the web server uses for web pages.

Moving on

PHP is a weakly typed language and any PHP variable can store integers, strings, double precision numbers and so on. You don’t have to declare PHP variable before you use them and the only oddity is the use of a “$” symbol to start every variable’s name.

Variables can be combined into expressions using the usual arithmetic operators and strings can be concatenated using the “.” operator. If you know C or any modern language you will already be familiar with the “++” increment and “- -” decrement operators and the wide range of assignment operators.

PHP’s control statements are basically the same as those in C and similar languages. If you want to repeat something you can use a C style for loop:

for(initial;test;increment) 
{instructions to repeat}

For example to print “Hello” ten times you would write:

for($count=1;$count<=10;$count++)
{print “Hello”;}

Notice also that PHP uses curly brackets to group instructions and it uses semicolons as separators.

One of the biggest problems with converting to PHP, or any server-side scripting language, is keeping in mind that your output medium is HTML.

For example, the for loop listed above gives you ten “hello”s all on the same line.

How would you put one per line?

The answer is to simply print an HTML <BR> tag after each one –

for($count=1;$count<=10;$count++)
{print "Hello <BR/>";}

As well as for loops there are also do, while and if statements that look more or less the same as those found in C, Java or C#.

You can also define your own functions to extend the language.

For example, you might want to add a printbr function which prints some text and automatically follows it with a <BR/> tag to start a new line.

You can do this as follows:

<?php
function printbr($text)
{
print $text . "<BR>";
}
for($count=1;$count<=10;$count++)
{printbr("Hello");}
?>

PHP is also object-oriented but more of this in Ten minutes to PHP Objects.

Banner

<ASIN:0596005601>

<ASIN:0672329166>

<ASIN:1590598628>



Last Updated ( Monday, 30 May 2011 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.