Source of: /manual/en/tutorial.firstpage.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/tutorial.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'tutorial.firstpage.php',
1 => 'Your first PHP-enabled page',
),
'up' =>
array (
0 => 'tutorial.php',
1 => 'A simple tutorial',
),
'prev' =>
array (
0 => 'tutorial.requirements.php',
1 => 'What do I need?',
),
'next' =>
array (
0 => 'tutorial.useful.php',
1 => 'Something Useful',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="tutorial.firstpage" class="section">
<div class="info"><h1 class="title">Your first PHP-enabled page</h1></div>
<p class="para">
Create a file named <var class="filename">hello.php</var> and put it
in your web server's root directory (<var class="varname">DOCUMENT_ROOT</var>)
with the following content:
</p>
<p class="para">
</p><div class="example">
<div class="info"><p><b>Example #1 Our first PHP script: <var class="filename">hello.php</var></b></p></div>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<html><br /> <head><br /> <title>PHP Test</title><br /> </head><br /> <body><br /> <span style="color: #0000BB"><?php </span><span style="color: #007700">echo </span><span style="color: #DD0000">'<p>Hello World</p>'</span><span style="color: #007700">; </span><span style="color: #0000BB">?></span> <br /> </body><br /></html></span>
</code></div>
</div>
<div class="example-contents simpara"><p>
Use your browser to access the file with your web server's URL, ending
with the <i>/hello.php</i> file reference. When developing locally this
URL will be something like <i>http://localhost/hello.php</i>
or <i>http://127.0.0.1/hello.php</i> but this depends on the
web server's configuration. If everything is configured correctly, this
file will be parsed by PHP and the following output will be sent to
your browser:
</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<p>Hello World</p>
</body>
</html>
</pre></div>
</div>
</div><p>
</p>
<p class="para">
This program is extremely simple and you really did not need to use
PHP to create a page like this. All it does is display:
<i>Hello World</i> using the PHP <a href="function.echo.php" class="function">echo()</a>
statement. Note that the file <em class="emphasis">does not need to be executable</em>
or special in any way. The server finds out that this file needs to be interpreted
by PHP because you used the ".php" extension, which the server is configured
to pass on to PHP. Think of this as a normal HTML file which happens to have
a set of special tags available to you that do a lot of interesting things.
</p>
<p class="para">
If you tried this example and it did not output anything, it prompted
for download, or you see the whole file as text, chances are that the
server you are on does not have PHP enabled, or is not configured properly.
Ask your administrator to enable it for you using the
<a href="install.php" class="link">Installation</a> chapter
of the manual. If you are developing locally, also read the
installation chapter to make sure everything is configured
properly. Make sure that you access the file via http with the server
providing you the output. If you just call up the file from your file
system, then it will not be parsed by PHP. If the problems persist anyway,
do not hesitate to use one of the many
<a href="http://www.php.net/support.php" class="link external">» PHP support</a> options.
</p>
<p class="para">
The point of the example is to show the special PHP tag format.
In this example we used <i><?php</i> to indicate the
start of a PHP tag. Then we put the PHP statement and left PHP mode by
adding the closing tag, <i>?></i>. You may jump in
and out of PHP mode in an HTML file like this anywhere you want. For more
details, read the manual section on the <a href="language.basic-syntax.php" class="link">
basic PHP syntax</a>.
</p>
<blockquote><p><b class="note">Note</b>:
<span class="info"><b>A Note on Line Feeds</b><br /></span>
Line feeds have little meaning in HTML, however it is still a good idea
to make your HTML look nice and clean by putting line feeds in. A
linefeed that follows immediately after a closing
<i>?></i> will be removed by PHP. This can be extremely
useful when you are putting in many blocks of PHP or include files
containing PHP that aren't supposed to output anything. At the same time
it can be a bit confusing. You can put a space after the closing
<i>?></i> to force a space and a line feed to be output,
or you can put an explicit line feed in the last echo/print from within
your PHP block.
<br />
</p></blockquote>
<blockquote><p><b class="note">Note</b>:
<span class="info"><b>A Note on Text Editors</b><br /></span>
There are many text editors and Integrated Development Environments (IDEs)
that you can use to create, edit and manage PHP files. A partial list of
these tools is maintained at <a href="http://en.wikipedia.org/wiki/List_of_PHP_editors" class="link external">» PHP Editors
List</a>. If you wish to recommend an editor, please visit the above
page and ask the page maintainer to add the editor to the list. Having
an editor with syntax highlighting can be helpful.
<br />
</p></blockquote>
<blockquote><p><b class="note">Note</b>:
<span class="info"><b>A Note on Word Processors</b><br /></span>
Word processors such as StarOffice Writer, Microsoft Word and Abiword are
not optimal for editing PHP files. If you wish to use one for this
test script, you must ensure that you save the file as <em class="emphasis">plain
text</em> or PHP will not be able to read and execute the script.
<br />
</p></blockquote>
<blockquote><p><b class="note">Note</b>:
<span class="info"><b>A Note on Windows Notepad</b><br /></span>
If you are writing your PHP scripts using Windows Notepad, you will need
to ensure that your files are saved with the <var class="filename">.php</var> extension.
(Notepad adds a <var class="filename">.txt</var> extension to files automatically unless
you take one of the following steps to prevent it.) When you save the file and
are prompted to provide a name for the file, place the filename in quotes
(i.e. "<var class="filename">hello.php</var>"). Alternatively, you can click on the
'Text Documents' drop-down menu in the 'Save' dialog box and change the setting
to "All Files". You can then enter your filename without quotes.
<br />
</p></blockquote>
<p class="para">
Now that you have successfully created a working PHP script, it is
time to create the most famous PHP script! Make a call to the
<a href="function.phpinfo.php" class="function">phpinfo()</a> function and you will see a lot of useful
information about your system and setup such as available
<a href="language.variables.predefined.php" class="link">predefined variables</a>,
loaded PHP modules, and <a href="configuration.php" class="link">configuration</a>
settings. Take some time and review this important information.
</p>
<p class="para">
</p><div class="example">
<div class="info"><p><b>Example #2 Get system information from PHP</b></p></div>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php phpinfo</span><span style="color: #007700">(); </span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
</div><?php manual_footer(); ?>