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.

Accessing localhost Web-server on Mac and PC

Make sure that you turn on the Wamp if you are on a PC and Mamp if you are on Mac.

PC users can access their web files using the URL http://localhost/

Mac users can access their web files using the URL http://localhost/~yourUserDirectory

yourUserDirectory is the user account name of your Mac computer using which you have logged in.

On my Mac, I have a user directory by name Satish. So I can access my web files using http://localhost/~Satish

On Mac, once you enter http://localhost/~yourUserDirectory it automatically changes the URL – but don’t worry about it, its working fine and that’s how it works!



Let non of these small things make you not use the web server. I am writing this because, some times small things like this suck a lot of time and some people may even start feeling this as something very much technical and complicated and may leave the idea of learning altogether.

If you successfully accessed your web files from your localhost web server, then congratulations. Continue your exploration. We have a lot of simple PHP examples on this website that you may want to try. All the Best.

Links For Installing WAMP, MAMP, XAMPP

These are the official websites and your goto place if you want to know anything about these technologies. They have a detailed documentation and large community of users who are always willing to help.

Note: Full Form of PHP
Earlier PHP was called “Personal Home Page”. Now PHP is “PHP: Hypertext Preprocessor”.

server
Apache: httpd.apache.org
PHP: php.net
MySQL: dev.mysql.com

Links to install All-In-One package:

For Macintosh:
Starting from Mac OS 10, Apple started including Apache, PHP and Safari(web browser) as a package when you purchase the OS. But its good idea to check and upgrade to latest versions of Apache, PHP and MySQL(which doesn’t yet come along with the OS).

MAMP is a all-in-one package – it stands for Macintosh, Apache, MySQL, PHP.
http://www.mamp.info/en/index.php

XAMPP: http://www.apachefriends.org/en/xampp-macosx.html

For Windows:
WAMP: Windows, Apache, MySQL, PHP
http://www.wampserver.com/en/index.php
XAMPP: http://www.apachefriends.org/en/xampp-windows.html

Full form of XAMPPX (any of four different operating systems), Apache, MySQL, PHP and Perl.
If you visit XAMPP website you can find all-in-one package for Linux, Windows, Mac OS X, Solaris. So its a all-in-one package available for all the major operating systems.

I have just listed the links for the all-in-one installers because the installation process is easy and there is nothing but following the steps that the installer gives while installing.
If you run into trouble or if you have any question regarding installation then you can ask me in the comment section below or in the forum.

All these above packages are opensource and hence free of cost..(I am talking about WAMP, MAMP, XAMPP and not about Mac and PC!!)