Posted about 1 year ago, by Specific

Your first PHP Script!

I would like to show you the very basics of PHP in a short, simple tutorial.  We'll start from what you need to writing your very first PHP script.

This text only deals with creating dynamic web page with PHP, though PHP is not only capable of creating web pages.

PHP-enabled wweb pages are treated just like regular HTML pages if you're familiar with them, and you can create and edit the same.

What do I need?

In this tutorial we assume that your web host/server has activated support for PHP and that all files ending in the .php extension are handled by PHP. On most servers, this is the default extension for PHP files, but ask your server administrator to be sure.  A PHP-enabled server, the PHP file you creat will automatically be parsed by the server, and there is no need to compile or install any extra tools.

Your first PHP-enabled page

Create a file named hello.php and put in your root directory with the following content:

Use your browser to access the file with your web server's URL, ending with the "/hello.php" file reference.  When developed locally, this URL will be something like http://localhost/hello.php or http://127.0.0.1/hello.php, however, this depends on the web server's configuration.  If everything is configured correctly, the file will be parsed and the following will be sent to the browser:

This program is simple and you really did not need to use PHP to create a page like this.  All it does is display: Hello World using the PHP echo() statement.

How it works

In the above example, we use opening and closing PHP tags to parse the code inside the tags.  We then called PHP's echo() command to display the text we provided.

What can I use to write PHP?

There are many text editors and Integrated Development Evironments (IDEs) that you can use to create, edit, and manage PHP files.  It's best not to use Word Processors such as Microsoft Word as they are not optimal for editing PHP files.  If you wish to use one of these, you must make sure that you save files in plain text or PHP will not be able to read and parse the script.  I use a program called DreamWeaver created by Adobe to create my files found here on Zonehacks.com and is an IDE that I recomend using.

Windows Notepad will work just fine for writing PHP scripts, just make sure when saving that you select *all files in the drop menu or Notepad will automatically add a .txt extension to the filename you wish to create.  Windows Notepad will not handle files that are very large, as some scripts may be.

Thanks for reading this tutorial and hopefully it will jump start your PHP scripting.

Author Info Comment
Jordan
Posted about 1 year ago.
User Avatar

Also if you're printing variables, and have short tag on, you can easily do <?=$varname;?> :)