Write first php program
Now I had installed all I need to write php scripts or files but how do I write first php program. First of all I created a learning folder on my desktop where I intended to keep all my files I made during my learning process. So now I am going to make my first ever PHP file called hello.php
So as a starting point I decided to go along with a starting ” Hello World! ” program in PHP.
- For this I simply opened my notepad and then save it as
- Now since I had created my first PHP file I am going to write my first PHP script right there in notepad.
- So I typed and hit save button.
- Now we know that PHP file can not be opened by a browser. So to see the response of my code I start running my easy php dev server by clicking its icon on mt desktop. After it gets started it looks like a black red icon under startup program as shown in this figure
- Now once it is started right click this
and select local web to start local web. Right now I am learning PHP so I had not configured anything on my easy php dev server. Once started in your browser it looks like
- Here you can click home icon and now your screen looks like this
- Now here you can see I have created alias “learn” for my folder where I will put all my learning and testing files.
- Here you can also see a code test window. You can go ahead and test your simple “hello world” script right here in code tester and it would look like
Alternatively click alias “learn” ,as in my case you can create with any name , now you will see a screen like this
- Here click file “hello.php” Now you get your interpreted result in your web browser.
- This is how I write and interpreted my first script in PHP. I am not an expert so what you see here is through an point of view of novice and how I teaches myself. Maybe one day if I continue to learn I would become a PHP guru. Who knows?
Points to be taken while writing PHP Program
When writing first php page , we need to remember these few points
(1) PHP files should have .php extension. It get executed on the server by the web server and the resulting text is returned to the browser.The browser never get to know the php code,it only receive the html or text from server.If the person views the source code of the file displayed on the browser,it will only contain the html or text
(2) php code is written between tags i.e opening and closing limiter.
Opening <?php and closing ?>
There are other shorts tags available but they require special setting in php init file
(a)Opening <? and closing ?>
(b)Opening <% and closing %>
(3) Each statement in your PHP code is ended with a semicolon. Leaving this semicolon off is a common syntax error and when something isn’t working the way you expect it to should be one of the first things you check.
Example <?php phpinfo(); ?>
Even any variable assignment also need the semicolon
(4) White space (spaces, tabs and newlines) are ignored in the syntax of PHP. These three examples are completely equivalent:
<?php echo "test"; ?> <?php echo "test"; ?> <?php echo "test"; ?>
(5) Writing comments in the code is a useful things. It helps in reading and organizing the code. As with other programming language,PHP do have comments syntax
<?php /* This is test code */ echo "test"; ?> <?php //This is test code echo "test"; ?> <?php #This is test code echo "test"; ?>
(6) In PHP, all keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are NOT case-sensitive. But all the variable are case sensitive
echo "a"; is same as Echo "a"; $col=1; is not same as $COL=1;
The above explain the basic ideas behind PHP. Lets write the first PHP based on this
<title>First PHP page</title>
// The below statement echo the statement
echo "This is php information";
// The below statement is a php function call which return the php version etc