Wednesday February 1, 2012 06:43
How To Create/Start First PHP Script? : Lesson 1
Posted by pradeep as PHP
If you are new to PHP, this is for you. Here you will learn how to create and run a PHP web page for the first time. Be sure either you are directly working on web server or you have local machine servers installed in your computer device and working. follow up my first lesson Before starting php. Important Things to know more about servers and how making of script and dynamical web works. After starting servers like Wamp, Xampp etc on your machine you can access it from url typing http://127.0.0.1 or localhost in the address bar. Always remember to put your files in the www directory of the server installed.
In this tutorial you will learn how to run a PHP script. You can code this script either in Notepad, Wordpad, Dreamweaver, Frontpage Etc. All you need to know is you should save it as .php extension in a www directory(if working with local machine server like wamp,xampp etc) or inside public_html directory (If working directly inside web server).
Let’s start on our first script. To begin a block of PHP statements we need to start it with a PHP open tag:
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:
print “Hello World!”;
Now access the url page through the browser to see the out put. It should read:
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.
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!”; ?>
<? return “Hello World!”; ?>
You can also combine HTML and PHP all in one page like this but be sure to saveit as .php extension.
<META http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
<? return “Hello World!”; ?>
You can also use more then on line for the string of data like this:
Try them out you will figure out the first step and later you can work with logical operators, more syntax’s in following up lessons.
Understanding the Code
Lets 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 Functionsthat 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.
This lesson will help a lot to start for begineers. Its requires no skills to follow up the things rather it helps you to figure out how it works. Follow up other lessons about operators, Variables, Datatypes etc, It will help you a lot to become a kick-ass beginner within short time.