Welcome Back! Today we will start with the basics on – “Writing the Code”. We will talk about how the PHP code is written, simple basic code like echoing Hello World, adding comments in the PHP file etc. We will also talk about the official site of PHP and how it can help you discover new things. I hope that you have installed the recommended programs of which I had written a couple of days back.
PHP.net – Official Website of PHP
PHP.net – The official site of PHP: HyperText Preprocessor helps you to do anything in PHP. You can view the funtion list there with examples and usage, or even the documentation or FAQ. They also have “A Hacker’s Guide to the Zend Engine“. You can read user submitted notes or can understand anything which you aren’t able to catch up with. But yeah, we are also here to help you! π Do not hesitate to ask anything in the comments section!
PHP Open & Close Tags
PHP Code is wrapped between:
<?php
?>
This code lets the server know where the PHP starts and where it ends. Note that the server processes PHP only when the file ends with .php extension. As PHP can be embedded with HTML, this is the code that makes it happen. The server accesses the web page, it knows to be ready for PHP as we have the .php extension. But it still needs some indication of where PHP starts and ends. We do that by writing . I will refer to these tags with open PHP Tag & close PHP tag respectively. You will be used to these and these will be very common for you. There are also variations of these tags of which you should be aware of. I request you not to use these variations. I have just mentioned these here so that if you come across such open and close tags, you shouldn’t be confused. You have the option of telling your web server whether to allow these tags or not in php.ini. The following is the code which was very common in PHP 3 and may still work for you:
<?
?>
<?=
?>
There is also another type of code which is used in ASP. For these tags, you have to specially edit php.ini file and tell your server to accept these tags. But you shouldn’t use these as if you give somebody your code, and that person uploads it and the server doesn’t accept these tags, then the code won’t work for the person whom you gave the code to!
<%
%>
<%=
%>
Note: If you enter any PHP code but that file doesn’t end with .php extension, then the code won’t work for you. If you have put a PHP code in a file, then you also need to end the file’s name with .php extension or else you can also configure apache/IIS to serve other extensions with php parser. For more information, you can go here or here.
phpinfo
phpinfo is the very basic function in PHP. It looks like this:
<php
phpinfo();
?>
Go and try out this code! As you may not know how to try it on XAMPP, you can see the following tutorial.
- Make a new file in the htdocs folder named phpinfo.php (You can give it any name but it should end with .php so that the server knows that it is a PHP file. Write the code given above in the file you just made. Note – Any text editor can be used to make or edit PHP files, but avoid using Word or anything like Word as it may enter some unnecessary text which may cause errors.
- Navigate to http://localhost/phpinfo.php (or the file name you entered in the above procedure). If you see something like this, then it means that your localhost server is up fine and PHP is also running.
This function returns all the configuration of PHP on your server and it can also be used to check if PHP is working on a server or not. This function is used by every single PHP developer to check if the PHP is working on a particular server or not. As this function can give out a lot of information about the web server and PHP, no PHP developer would like to put this on a web server where someone can find it. But, for development purposes, it is very useful.
Hello World
This is the most basic thing which is taught to everyone in almost every language – which is printing Hello World on the screen. We print something on a screen with PHP by the echo or print function. Here is the example:
<php
echo "Hello World";
?>
Note: In PHP, every function is followed by a semi-colon (“;”). Also note that function names in PHP are NOT case-sensitive, but the variables are.
You just have to follow the same tutorial given for phpinfo to upload files. Just make a new file named helloworld.php and put it in htdocs folder. Then you would be able to access it on http://localhost/helloworld.php
We can also use the print function or do some maths problems or even merge 2 strings. Here is an example here:
<php echo "Hello World"; ?>
<php print "Hello World"; ?>
<php echo "Hello " . "World"; ?>
<php echo 2+3; ?>
You can view the online demo of the above code here.
Adding Comments
We want to become good programmers.. right? Adding comments in PHP files is a good practise. Like, if someone else sees your code or you see your code after weeks, the comments will help you know what you actually did there. Here is how you can add comments in the files:
<?php echo "Hello World"; //echo function is used here
?>
<?php print "Hello World"; //print function is used here
?>
<?php echo "Hello " . "World"; #merging 2 strings in echo function
?>
<?php echo 2+3;
/Here is a maths problem in which 2+3 is done
it should echo back 5
*/
?>
<?php
//more comments can come like this
#or like this
/or multilines like this
and this
*/
?>
Note: Please do not put double line comments within double line comments as the PHP looks for /* and then the next */. Anything after that will be processed as PHP which may also give out errors.
What does the web server do?
I want you to know what actually happens when we enter a PHP URL in the browser:
- We enter a PHP URL in the browser.
- The browser looks for the page.
- The page is located (which is stored on the server).
- Serer already knows via its config, if a file has .php, execute PHP interpreter which will parse PHP file.
- There may be Database queries which will also be executed on the server-side.
- PHP interprter process PHP file optioanlly using other modules and send output to sever (example: Apache/IIS)
- The browser will display the output to us.
PHP can return output in any form like XML, JPG, MP3, FLV, etc. Just any format you can imagine. For more information, you can go here.
I hope that you have understood each and every thing. If you have any doubts, then please ask them here in the comments section.
That’s all for today, I hope that you try these out and come back to read the next tutorial which will be published in next one or two days. My next tutorial will contain Data Types (Strings, Variables, Arrays etc.)
14 Comments
Thanks a lot Gautam! I was looking for these easy to understand tutorials which clear up my basics! π Now I know how to make and test php files at home! As in w3schools they say that u dont need anything if your webhost supports php but I always wondered how am I supposed to test them without uploading! Thanks again π
Post revised on 11:00 am (29 March 09). Please read the post again if you have already read it else you may get confused.
Thanks to Rahul. π
Your Welcome Swashata.
Good ! But Gautam Can You Give Me Your Toolkit Codings ?
@Saad
Sorry, but I can’t give you my toolkit codings.
bhai http://localhost/phpinfo.php
kuch nahi dikha raha
in the code that we have to write in phpinfo.php
pls tell the code
I suceeded in bringing the page.
I want to ask a question.
whenever i disconnect internet after that http://localhost/phpinfo.php doesnt work. why
@Harshil
You first have to install XAMPP
then add a file in htdocs folder
Then access that link.
And please read the full posts of the series so that you can understand it. I have also given screenshots here
@Gautam
Brother I Want Your Voter Source Because My IDX Are Hacked From Other Voters Your’s One Is Safe But It Gives Advertisement :((
I’m having trouble with XAMPP and PHP.
The default index file works fine to show PHP works.
However, creating text files and saving name.php of each text file doesn’t seem to open up in a browser when entering
http://localhost/phpinfo.php
http://localhost/helloworld.php
Files saved with .php extension still show up as a Text Document
in explorer view.
@Luis Allen
You don’t have to open the files by double clicking them from the htdocs folder. You have to open that link in your browser (not by double clicking the file) when you have stored that file in the htdocs folder.
I discovered my issue, I wasn’t saving as… other files.
So even putting the .php made the files…
phpinfo.php.txt and helloworld.php.txt.
Of course, the files are read as listed in my original post, thereby making things a bit hard to see show up on a web page.