When I arrived at Freedom2Surf, I had little experience in web designing. I soon heard of server-side includes (SSI) and was burning to try them out.
To my dismay, I found out that Freedom2Surf did not support SSI (Note: this is no longer the case). Anwers to my protests were of the form "use PHP instead! it can do everything SSI does, and much more!".
I didn't know what PHP was, and a trip to the official PHP site gave me the impression the monster was too ferocious for me to tackle. I was wrong. But once you know PHP, the basics are so simple that nobody ever bothers to explain them, which can make PHP seem inaccessible to the non-initiated. This article will hopefully fill in that blank.
If you know nothing about PHP and are wondering what all the fuss is about, or if you want to know how to use PHP to replace SSI, you will find the anwers here.
PHP is server-side:
Your browser doesn't realise the pages it is viewing are initially written with PHP.
All it receives is an HTML page - as complex or as simple as you want.
PHP is HTML-embedded:
A PHP page can be simply an HTML page with a little PHP sprinkled here and there (we'll see how).
The PHP bits are parsed ("translated") by the server - in the examples presented here, they will be mainly used to produce more HTML code.
The HTML code on the page is sent directly to the browser.
PHP has similarities with other programming languages:
C and Perl are two of them. In my case, learning a bit of Perl really helped me get started and understand what PHP could do for me - and how it worked.
To insert bits of PHP code in an HTML page, all you have to do is insert it in a special tag, like this:
<? any amount of PHP code ?>
Note: depending on your HTML editor, or if you are using XML, you might need to use one of these alternate "PHP tags":
<?php "blah blah blah" ?>
<% "blah blah blah" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"
xml:lang="en">
<head>
<title> <? print($pagetitle); ?> </title>
</head>
<body>
HTML code... <? PHP code ?> ...HTML code... <? PHP code ?> ...HTML
code... etc...
</body>
</html> /p>
The print
statement in the "Title" line simply sends the value of the variable $pagetitle
to the browser.
Let's say we had previously assigned a value to $pagetitle
, by using the following expression (somewhere in the beginning of the same page, for example):
<? $pagetitle='This is my page!'; ?>
Then what the browser would receive is:
<html>
<head>
<title>This is my page!</title>
</head>
etc...
As you can see, as far as the browser is concerned, this is just HTML...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"
xml:lang="en">
<head>
<title>This is my page!</title>
</head>
<body>
HTML code... <? require("nav.inc"); ?> ...HTML code... <? PHP code
?> ...HTML code... etc...
</body>
</html>
The require
expression tells the server to stick in the contents of the file named "nav.inc", instead of the PHP tag.
Let's say this file contains the main navigation table for your site and looks like this:
this is the beginning of the file
<table class="nav">
<tr>
<td><a href="/" title="Home page">Home</a></td>
<td><a href="/computers/" title="Everything about computers">Computers</a></td>
...
<td><a href="/recipes/" title="My favorite recipes">Recipes</a></td>
</tr>
</table>
the file ends here
This is what the server sends to the browser:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"
xml:lang="en">
<head>
<title>This is my page!</title>
</head>
<body>
HTML code...
<table class="nav">
<tr>
<td><a href="/" title="Home page">Home</a></td>
<td><a href="/computers/" title="Everything
about computers">Computers</a></td>
...
<td><a href="/recipes/" title="My favorite
recipes">Recipes</a></td>
</tr>
</table>
...more HTML code... <? PHP code ?> ...HTML code... etc...
</body>
</html>
Again, the browser has no idea that PHP exists.
Comments on the HTML code for the navigation table:
class
attribute tells the browser to apply the previously
defined style sheet class named "nav" to the table. If the browser doesn't
support CSS, it simply ignores it. You might want to know more about CSS.Here is a little information about what matters and what doesn't when writing PHP.
<?$title="mytitle";?>
or <? $title = "mytitle" ; ?>
, both work.PHP.net can be overwhelming for the beginner. I spent hours getting lost in that site before I found out how to use it efficiently. Though the interface seems to be a little more friendly than it used to be, here are a few tips which might come in handy:
php.net/function_name
into your browser bar. Try php.net/str_replace
or php.net/explode
to get started.php.net/whatever
will open a search page for "whatever".print
and echo
Variables can have just about any size.
For example, all the text you are reading now is the value of one big variable named $content
.
Variable names start with a dollar sign ("$"), followed
by an alphabetic character or an underscore, optionally followed by alphanumeric
characters or underscores.
They are case-sensitive.
They do not have to be declared or assigned a type.
PHP also supports array
variables (e.g. $somevar[3] = "something"
) and objects,
but their discussion is beyond the scope of this article.
print
and echo
do just about the same thing: they send their argument to the browser. There
is a small difference between them, but I'm not yet sure what it is... ; )
When using print
, echo
, or when assigning a string
value to a variable, you have the choice between using double quotes or single
quotes (if the argument is a naked variable, you can also use no quotes at all).
Single quotes will reproduce the text between them with no modifications.
Line breaks, spacings, variable names and fun characters will all come out how you printed them.
But! if the text between the quotes contains single quotes, you have to replace them by the escape sequence "backslash-quote": "\'". And what if you want a backslash? Use a double backslash: "\\". That's all.
Double quotes will replace variables by their value, and ignore newlines and tabs.
Here are some escape sequences: newline = "\n", tab = "\t", double quote = "\"", backslash = "\\", dollar = "$"...
Until you know better, I recommend using single quotes unless the string contains a variable to be replaced.
If you want to define a very long variable (for example, lots of text), you might want to use the here doc syntax.
An empty variable, e.g. $potato = '';
will return false
when its boolean value is called for.
It will not create an error if you attempt to print
it.
As it should be clear from the example above, an include is a simple ASCII file, and there is no special "dressing" to put around it. Just cut out a chunk of HTML from a page and stick it in a separate file: you have an include
.
Include files can have any extension.
You can name an include "blahblah.bla
" if you wish.
It is common to name them "something.inc
", but using other extensions can sometimes make sense.
For example, a file with a ".txt" extension will be viewable directly in most browsers, whereas trying to view includes with other extensions may simply result in the browser prompting you to "Save as...".
I have often used ".des
" for some of my files to indicate that they were "design" files.
Naming includes is a perfectly personal choice.
The require
function replaces itself with the contents of the file it calls, whereas include
is more like a branch that points to it.
This can make a difference if you are using a loop to call different includes: if you use require
, the statement will be replaced by the first file during the first loop, and you will end up with (e.g.) three times the same file instead of three different files.
If you are not quite sure which to choose, follow this rule of thumb: In control statements and functions, use include
. Otherwise, use require
(which is supposed to be a little faster).
An include can call another include. The include is read just as a normal PHP file, which means that you can put in an include anything that you would put in a "normal" PHP file.
Calling an include with relative or server-relative urls sometimes
creates trouble. I usually use include("$DOCUMENT_ROOT/path/filename");
or include("http://$HTTP_HOST/path/filename")
- do a few tests
on your own server to see what works for you. You will get a listing of server
variables like $DOCUMENT_ROOT
or $REQUEST_URI
with
the function phpinfo().
Here is a method I have used to create quite simply a general design for a site.
I have included it here as an example of how you can quickly get PHP to work for you.
Of course, this is only the beginning of PHP...
PHP4 offers a built-in template feature.
I will give you here the basic principle of my method, and links to example pages, but will not discuss all the details - though this may be done in a future update of this article if it is needed.
The idea is to create a unique page with a "skeleton" of the design you want for your site, i.e. all the HTML that is constant from one page to another. The parts that change from page to page are replaced in it by variables. This page is then "required" at the end of another page which gives values to these variables. Here are the main steps to take:
© 4WebHelp and Tara
Page URL: http://www.4webhelp.net/tutorials/php/basics.php
Back to the "pretty" page!
© 2025, 4WebHelp Team.
thanks
But how can php be executed, if your browser doesnt read php and there is nowhere in the script a reference line that tells php where to look to execute the script?
It will maybe be a stupid question - that'sbecause I still don't get the real basics of the working of php.
[Spam deleted]
What I would need would be a SIMPLE example with steps listed one by one.
1) take the following code. 2) Put it into an HTML file caled name.html 3) sent to server, or whatever. Just my two cents.
$email = "you@yourdomain.com";
if (eregi("googlebot",$_SERVER['HTTP_USER_AGENT']))
{
mail($email, "The Googlebot came to call",
"Google has visited: ".$_SERVER['REQUEST_URI']);
}
?>
<?
Works
thanks !!
HELP!!!!
But I realize that I can do a lot of sh*t on the net with php code... ;p
thanks
Thanks Jacob
iam new to php programming can bring some
useful sites url
hel;p
<body>
<?php require("header.htm"); ?>
whats the best way to make a template file
and then put html into in?
seems to do it
unless there is a better way
for example
<?
$reviews = "<br><br>some html here <a href="whatever.html">agff</a>";
include ("temp.html");
?>
etc
i understand that varables are not for this
but how is it done
thanks I am very new
This is what the world has been waiting for - Thanks Tara
Here's the code if anyone is interested.
<?php
$email = "you@yourdomain.com";
if (eregi("googlebot",$_SERVER['HTTP_USER_AGENT']))
{
mail($email, "The Googlebot came to call",
"Google has visited: ".$_SERVER['REQUEST_URI']);
}
?>
<?
Thanks again
Ive been looking through a load of PHP guides all day but none of them had the basics!
this is the first page that I find which is written clearly. I made notes and all to understand the stuff. I am a veryvery newbee at php , i bought a book, but still cant figure somethings out.
Still I have a few questions, hope thats okay...
My server has php dev installed. I myself did not install anything. I wrote a testfile according to your tutorial.
test is on: http://www.showerzone.nl/test/testphp.htm
i inserted this code ][ <?php require ( "nav.inc") ; ?>][ into the html file.
But how can php be executed, if your browser doesnt read php and there is nowhere in the script a reference line that tells php where to look to execute the script?
It will maybe be a stupid question - that'sbecause I still don't get the real basics of the working of php.
hope u can help me out.
thanx - figidigi
header('Location: http://www.site.com/');
?>
I am writing a script in which it should transfer to another web page under certain condition. I know in ASP that I can use redirect statement but I would like to know about that in PHP.
Thanks
Add a new comment