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.
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!
If you already know PERL or C, you should be familiar with the syntax of PHP.
To start specifying PHP code:
To stop specifying PHP code:
Example:
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.
- 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
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.
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.
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
Page URL: http://www.4webhelp.net/tutorials/php/what.php
Back to the "pretty" page!
© 2025, 4WebHelp Team.
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.
There is more to PHP than just what you can do here.
Add a new comment