Write first php program
Now I had installed all I need to write PHP scripts or files but how do I write the first PHP program. First of all, I created a learning folder on my desktop where I intended to keep all the 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 the first PHP page, we need to remember these few points
(1) PHP files should have .php extension. It gets executed on the server by the web server and the resulting text is returned to the browser. The browser never gets to know the PHP code, it only receives the HTML or text from the 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 settings 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 spaces (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 thing. It helps in reading and organizing the code. As with other programming languages, PHP does 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 variables are case sensitive
echo "a"; is same as Echo "a"; $col=1; is not same as $COL=1;
The above explains the basic ideas behind PHP. Let’s 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