Basic Structure of PHP
So far we have learnt basic information about what PHP is? And how you can configure PHP on your machine, now we will start learning PHP.
PHP is a scripting language, so the scripts which you create using PHP needs not to be compiled rather it’s interpreted directly on the go at run time.
Let’s start with the character set of PHP. PHP supports following set of characters.
- Letters (a…z , A…Z )
- Numbers (0…9)
- Special Symbols ( like $ , # , & , @ , - etc)
Naming of PHP Files
All PHP scripts will be saved using the extension “.php” , like HTML all your files will be stored as plain text.
The coding of PHP can be done in the following different manners:
Traditional coding style
This is the normal style for coding. Here for this type all the PHP script should begin with and it should end with ?> , the beginning tells that whatever is written between these 2 tags is your PHP code.
<?php echo “hello world”; ?>
It is as same as traditional coding style except for the beginning of PHP code is replaced by Instead of , for using this format you need to change your php.ini file , search for short_open_tag=off and replace it with short_open_tag=on
<? echo “hello world”; ?>
HTML Script tag
For this type the first line of your code should be indicates the end of PHP script.
< script language=”php”> echo “hello world”; </script>
ASP Coding Style
For this type the first line of code should be <% and %> should be written to end your script , for using this coding style again you need to set the configuration option in your php.ini file.
My First PHP Program
Let’s start with the simple PHP program. To get started first open any editor you want to use for writing your PHP code (Like Notepad, Dreamweaver, Notepad++ etc), write the following code in your editor
<?php echo “Hello Discuss Desk ”; ?>
Save this file into your root directory for PHP.
Note that it will be www in case of WAMP (WAMP>WWW) , htdocs in case of XAMPP (XAMPP>htdocs).
Create a folder in htdocs to store all your PHP programs, save the file with a unique name and with extension .php
Executing Your saved PHP Program
- Open Web browser
- Type URL http://localhost/yourfoldername/yourfilename
- Press enter and your code will be executed.
Some important points to be remembered
- PHP is case sensitive
- Upper case and lower case letters used for variables , objects and functions will have different meaning and they will act differently ,
- White spaces are ignored in PHP
- A space, tab or new line can be considered as white space. You can put as many white spaces as you want without affecting your code, which means your white spaces will be completely ignored by PHP.
- Using comments is a good habit of keeping notes of whatever work you have completed
- Comments will also be ignored when the PHP script is executed. It’s used by programmers for documenting (writing proper information) whatever code they are writing so that it can be easily understood later.
- Single line comment The PHP statement beginning with // will be considered as single line comment, the statement written following // on same line will be ignored at the time of execution.
- Multi line comment Any number of statements written between /* and */ will be treated as multi line comment, and the statements written between /* & */ will be ignored at the time of execution.
- Each statements of PHP is terminated with ;
- Every statement in PHP like C , C++ is always terminated with ;(semicolon)
- One interesting thing to note is that PHP will allow you not to terminate the last statement with ;(semicolon) , so its optional to use ; (semicolon) for last statement
<?php echo “Hello DiscussDesk”; echo “Lets Learn PHP” ?>
In above code as you can see we have not used ;( semicolon) for the last statement and its perfectly fine.
So to summarize we have learnt the basic structure of PHP and now you have the basic knowledge of writing PHP scripts.
Share this Article on Social Media
Get Updates, Scripts & Other Tutorials to Directly to your Email
Over 20000+ Happy Readers already subscribed. (We don't send spam email). Every email subscriber can get our latest updates and download our 100+ scripts.