Introduction
In this tutorial you will learn how to run a PHP script.
Open a text editor application. Be sure to use a simple text editor, nothing
like MS Word because a big text programs will add extra characters and bugs
to your code.
Advertisement
In order to run PHP, you are going to need a web server with PHP installed. We recommend iPowerWeb.com for
only $7.50 with Unlimited Disk Space, Subdomains, and Emails Accounts.
Another requirement for running PHP scripts on a server is the extension file
name format. By default you must use a ".php" extension on your PHP web page
files. Other extensions that may work depending on you server are ".php3" and
".php4".
O. K. Let's start on our first script. To begin a block of PHP statements we
need to start it with a PHP open tag:
<?php
or
<?
And now we end our code with a closer tag:
?>
Lets create a new file and name it "first.php". Type the code below (or copy
and past it) in to the file named "first.php" and upload it to your server:
<?php
print "Hello World!";
?>
Now access the url page through the browser to see the out put. It should read:
Hello World!
Note that if you view the source html code from the browser application, all
php code is hidden. It was all parsed (processed) before the browser application
could view the page.
Structure
You can structure the PHP code all on one line if you prefer like this:
<?php print "Hello World!"; ?>
You can also use the short start tag " <?" with out the "<?php" like this:
<? print "Hello World!; ?>
You can also change the "print" function to "echo" or "return" functions and
achieve the same results like this:
<? echo "Hello World!"; ?>
or
<? return "Hello World!"; ?>
You can also combine HTML and PHP all in one page like this:
<HTML>
<HEAD>
<TITLE>newsourcemedia.com</title>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<BODY>
<? return "Hello World!"; ?>
</body>
</html>
You can also use
more then on line for the string of data like this:
<?
print "Hello
World!";
?>
or
<?
print "
Hello
World
";
?>
Tryout some of these above samples. You can also change the statement inside
the quotes if you like to see if all still goes well.
Understanding the Code
Let take a look at how the code works. We already know from above that "<?"
and "?>" are the start and end PHP tags. Next we used three deferent
word: "print", "echo" and "return". These word
commands are built-in PHP Functions that outputs data (you'll
learn soon how to build your own functions).
A Function is a command that performs an action and is modified
by other data usually supplied to it. The data ( a collections of characters
or a "string") is usually always placed inside single or double quotes.
Most functions must have parentheses after their name like function( ). However
the three functions we have use above are exempted from that role.
Lastly, we used a semicolon ";". The semicolon tell PHP that this is the
end of the line of code and to execute this line of code.
Most programmers forget to end there code with the semicolon. You will find
that most of the reasons for your codes not working properly is that you forgot
as well.
Summary
In this tutorial you learned how to use a text editor to create a PHP script,
how to name your PHP files, how to start and end blocks of code with start and
end tags, how to output data to the browser with functions, and how to combine
PHP and HTML codes.
This is just the tip of the iceberg so be sure to click here to check out our other tutorials on PHP.
NewSourceMedia is providing links to these listings as
a courtesy, and makes no representations regarding the content or
any information related thereto. Any questions, complaints or claims
regarding the downloaded content or details must be directed to the appropriate
publisher. We do not encourage or condone the use of any
software in violation of applicable laws.