You are using a browser which is not compatible with CSS (for more information, see Tara's tutorial). Because of this, it is possible that our website may not appear correctly in your browser. We apologise for the inconvenience, and recommend you upgrade your browser to one which is compatible with CSS. For more information, please visit our Browser Upgrade page.

4WebHelp

What is PHP? by Jeremy

Last updated: 02/05/2012

Introduction to PHP

PHP, or PHP Hypertext Preprocessor, is a HTML embedded scripting language. It has the versatility of PERL, the syntax of C, and, most important of all, the database access features to all popular types of databases. (Including MySQL, PostgreSQL, MS SQL and Oracle) Writing scripts with PHP is comparatively easier than using PERL, while still maintaining the same, or even better functions.

Examples of PHP

Okay, now, some very basic code examples of PHP:

<?php print "Hello world"; ?>

Recognise anything? Most of the PHP code structures are from PERL, which might look like this to make the same function :

#!/usr/bin/perl
print "Content-type:text/html\n\n";
print "Hello world";

which is a lot of fuss compared to the PHP code above. You can, actually, create a very complicated script with PHP doing most of your work, such as automation of registration of users, visitor tracking, server configuration, plus all others you can think of!

Basic PHP syntax

If you already know PERL or C, you should be familiar with the syntax of PHP.

To start specifying PHP code:

  1. <?php
  2. <script language="PHP">
  3. <?                       *1
  4. <%                       *2

To stop specifying PHP code:

  1. ?>
  2. </script>
  3. ?>                      *1
  4. %>                      *2

Example:

  1. <?php print "Hello world"; ?>
  2. <script language="PHP">print "Hello world";</script>
  3. <? print "Hello world"; ?>        *1
  4. <% print "Hello world"; %>        *2

Note : *1 requires short_open_tag to be set on, which is default on most servers and *2 requires asp_tags to be set on, which is NOT default on most servers.

PHP Data Types

- Strings

Strings are plain text enclosed in quotes (")

Example: This is a "string"

- Integers

Integers are whole numbers

Example: 1234

- Doubles

Doubles are floating point numbers

Example: 1234.567

PHP Variables

A variable is a piece of data that is represented by a name. Variables are defined by the syntax $varname = value;
For example:

$myname = "Jeremy";      # $myname is a string

$yourname = "Whoever";      # $yourname is a string too

$anynumber = 2;          # $anynumber is an integer

The has (#) signs in the above text are called comments. They are notes by the programmer that is ignored by the script parser. Also, variables in strings are automatically expanded.

Basic PHP functions

print (string);

Outputs (string) into the screen.

Example: print "Hi! I am PHP";

echo (string);

Outputs (string) into the screen, identical to print.

Example: echo "Hi! I am PHP";

phpinfo();

Outputs a large amount of information about the current state of PHP.

Basic PHP control structures

Control structures are the logics of the program, for example, what will you do if you see a red traffic light when you are driving? A yellow one? What about a green one? Now, if you see a red one, you stop, when you see a yellow one, you prepare to stop or go, and, lastly, you go straight when you see a green one, right? I think your kindergarten teacher taught you that, but PHP hasn’t even gone into kindergarten! You have to teach PHP what to do.

For example:

<?php

$trafficlight = "red";

if ($trafficlight == "red") {
  echo "I stop when I see a red light";
}
else if ($trafficlight == "yellow") {
  echo "I prepare to go or stop when I see a yellow light";
}
else if ($trafficlight == "green") {
  echo "I go when I see a green light";
}
else {
echo "You have not taught me what to do if I see a $trafficlight light!";
}

?>

Congratulations! You have taught PHP what to do when it encountered red, yellow and green traffic lights. Try to run the script and see what PHP will do when it encounters a red traffic light.

Now, try changing the first line from

$trafficlight = "red";

to

$trafficlight = "yellow";

and see what PHP will do when it encounters a yellow traffic light.

After that, try changing it to

$trafficlight = "green";

and

$trafficlight = "blue";

Whoops! We have forgotten to tell PHP what to do when it encounters a blue traffic light! Change the code to:

<?php

$trafficlight = "blue";

if ($trafficlight == "red") {
  echo "I stop when I see a red light";
}
else if ($trafficlight == "yellow") {
  echo "I prepare to go or stop when I see a yellow light";
}
else if ($trafficlight == "green") {
  echo "I go when I see a green light";
}
else if ($trafficlight == "blue") {
  echo "I will ring up the traffic light repair crew if I see a blue light";
}
else {
  echo "You have not taught me what to do if I see a $trafficlight light!";
}

?>

and see what PHP will do when it encounters a blue traffic light!

© 4WebHelp and Jeremy

Latest comments on this tutorial
NIHILIST
<script>alert('xss');</script>
GML User
Well, that just explains the basics as I need to know Smile!
adam
this is a good tutorial on php. I say perfect for the beginers..
Geoff Browne
Very concise.  As a C programmer I like the sound of PHP !
Carrie
I have been looking for something to be as basic as this!  I love it!  

I like the idea of a part 2...Go a little further with what one can do next.  

Also, if you want to even get more basic... Do more of what the symbols (code) alone mean.  You have started doing that here already, explaining a couple of the functions.  
mike
Yes, this is good for explaining the basics. But can you make a part 2 of this?  I will never use a script like this in the real world.

There is more to PHP than just what you can do here.
B
Very simple introduction here; nice job.

Add a new comment

This page is © Copyright 2002-2024, 4WebHelp. It may not be reproduced without 4WebHelp's prior permission.