Saturday, October 12, 2024

PHP Tutorial: First Page

This very first page you make that includes some PHP code
deserves to be something really useful.  Don’t let the simplicity of this
little page fool you!  This is a very useful tool — as you will see when
you try it.   This page is going to verify the availability of PHP on
your server, and provide you with a lot of information about the PHP itself and
the server it’s running on.  Here’s the code:

<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php phpinfo(); ?>
</body>
</html>
 

That’s it!  Small, but then so are diamonds!  Let’s
break this little gem down.

Almost all of this is instantly recognizable to you as plain old
HTML code — all except that one curious line in the middle:   <?php
phpinfo(); ?>   That’s the PHP code.

A PHP code snippet is surrounded by an opening and a closing
delimiter.  The opening delimiter is <?php and the closing is ?>  the
code in between is PHP code.  In this example we are calling the PHP
function "phpinfo".  The function name is followed by opening and closing
parentheses which surround any parameters being passed to the function — in
this case there are none.  The function call is terminated with a
semicolon.

Go ahead and create a file containing this code — the file name
should end with a .php suffix — send it up to your server and pull it up in
your web browser just like you would any other web page.  you should see
lots of interesting info about the server system and PHP itself.  Take a
look at the source of the resulting page (View Source).  You’ll notice that
the HTML from your original page is still there, but the PHP code has been
replaced by all the information you see.  That’s basically how the engine
works.  The PHP code you write is replace in the resulting web page by the
results of running the PHP code.  Nobody gets to see your actual PHP code
— only the result of it running.  That’s significant, as I imagine you
already realize!

For a little further note about PHP syntax; the code snippet
above, contained between the beginning and ending delimiters is similar to HTML
tags in appearance.  It is in fact called a PHP tag.  There are four
forms of PHP tags.  The one we’ve used here is called an XML style tag and
is the one we will continue to use throughout this tutorial.  It is the
most common form. another style is called script style and will also look
somewhat familiar to you if you have written any other script code such as
JavaScript.  This example is exactly equivalent to our snippet above:

<script language=php>
phpinfo():
</script>

The other two forms are called the short style of tag and the
ASP style.  Both of these require special settings in the PHP configuration
files.  They are less commonly used and I will only provide this one
example of each in this series.  Here is the same code in the Short style
and ASP style, respectively:

<? phpinfo(); ?>

<% phpinfo(); %>

As I previously mentioned, each statement in your PHP code is
ended with a semicolon.  Leaving this semicolon off is a common syntax
error and when something isn’t working the way you expect it to should be one of
the first things you check.

Whitespace (spaces, tabs and newlines) are ignored in the syntax
of PHP.  These three examples are completely equivalent:

<?php phpinfo(); ?>

<?php     phpinfo();    
?>

<?php
phpinfo();
?>

As in all forms of programming, adding comments to you code is a
very useful idea.  Make your code self-documenting and you will love
yourself later when you come back to if after having forgotten why you did
things a certain way!  You can also add comments to you code in a variety
of ways similar to some other language syntaxes.  You can use /* multi-line
comment here */ like in C, or add comments to individual lines like in C++ or
shell scripts with // and # respectively.  Here are some examples:

<?php
phpinfo();
/*  This is a
     multi-line comment following
     the C language style of comments.
*/
?>

<?php  // this is a C++ style comment on the one code line
phpinfo();
?>

<?php  # this is a shell script style comment on the one
code line
phpinfo();
?>

That’s it!  You have now (or should have!) created your
first PHP page and learned the basic syntax requirements of PHP code.  You
are well on your way!



 


Continue to the next part of this Tutorial


Return to the Tutorial Series Index

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Popular Articles

Featured