First PHP program & Worst Practices To Avoid


We can embed php inside html tags and html tags inside php codes, but whatever it is – your php file name must end with a .php extension, so that the parser can identify that the file contains php code and it must also look for

<?php and ?>

codes inorder to recognise the codes as PHP codes and execute them.

Note: The output of PHP code is html.

Some Worst Practices to avoid:
Some people may use

<? 
    some_php_codes(); 
?>

OR

<?= 
    some_php_codes(); 
?>

OR ASP style( Active Server Pages – from Microsoft Inc )

<% 
    some_php_codes(); 
%>

OR

<%= 
    some_php_codes(); 
%>

and whole lot of other things.

All these or atleast some of these styles may even work on your computer, and that’s because of the way your php.ini file is configured. The same file may not work on other web servers.
So the best practice is to stick with using

<?php 
    your_php_code; 
?>

Also note that, white space isn’t significant in PHP. i.e., you can introduce any number of spaces, tabs, and new line characters in between your PHP codes.
But there must be atleast a single space after

<?php



Our First PHP program:

<?php
          phpinfo();
?>

Make sure you have saved the file with ( filename.php ) .php extension. Now open the saved file in your web browser.
If your server is working, then you must see a screen that gives information about your web server and PHP.
Output Screenshot:
phpinfo

Don’t make this file available on your server for general public, as it gives a lot of information about your server. Its a handy tool to quickly scan your server configuration.

Leave a Reply

Your email address will not be published. Required fields are marked *