Aug 2, 2011

Learn PHP | PHP Tutorials | PHP Lessons | Part 1

Learn PHP – Tutorial

(Part 1) Lesson -1

In this lesson we will focus on the following topics:-

  1.   PHP Basics

  2.   Server Side Scripting

  3.   PHP tags

  4.   The “echo ” command

  5.   Creating your first script


Let’s get started right away.

PHP Basics:

What is PHP ?

  • PHP is PHP:Hypertext  Preprocessor.

  • It is a programming language that is specially designed for creating Dynamic websites.

  • PHP language is flexible, making it easy to learn even if you have not done any programming in the past.


For example you could use PHP to show the current date and time on top of each page on your website.

[php]

Today’s date and time

The date is <!--?php  echo date(‘j F Y’); ?-->
and the time is <!--?php echo date(‘H:i:s’); ?-->

[/php]

  • Also it can easily process data submitted in HTML form and talk to various database systems( like SQL, MySQL, ..etc.) allowing generation of webpage based on SQL query.


 

Server Side Scripting:

  •         The most important concept to learn when starting out with PHP is where exactly it fits in a web environment. When you understand this, you will understand what PHP can and cannot do.



  •         A PHP module attached to your web server is telling the server that files with a particular extension (i.e. time.php) should be examined for PHP code. It processes instructions sent in a webpage by the client machine i.e. any PHP code found in the page is executed, thereby code is replaced by the output before it is sent back to the client’s web browser.  For eg. time.php file. The file contains basic HTML code along with PHP to display the current date and time. Here the PHP module in the server searches the file for the PHP code and replaces the code with the output. The following output is shown on the screen.


Output:-

The date is 1 August 2011 and the time is 14:29:54

  •          Once the page is downloaded, PHP plays no further part until another web page is requested by the client’s web browser.



  •          One thing PHP is not capable of doing is Client-Side-Validation. Now you’ll wonder what is Client-Side-Validation ?.


Consider a form(form.php) where you have to fill in all your details like Name, Address, Zip-code etc.

Client-Side-Validation checks value entered in a particular field like “Name:-..” meets certain criteria before moving to the next field i.e. in simple words you cannot enter your “Zip-code” in the area where you have to enter your “Name:-..” .

So to check the value entered is proper and valid is Client-Side-Validation. Client-Side-Validation can be achieved when PHP works with JavaScript.

  •          The beauty of PHP is that it doesn’t rely on the web browser at all. Your script will run the same no matter what browser you use. There is no need to worry about enabling JavaScript on your browser.


 

PHP tags:

Today is <?php echo date (‘j F Y’); ?>

  •          Above code is from a PHP driven web page that displays the current date

  •          The tag “<?php ”  tells that everything that follows is program code rather than HTML until the closing tag “?>”

  •          The “echo” command tells PHP to display the very next item on the screen. “echo” is used for printing output on the screen, “echo” is similar to “print” statement.

  •          The “date(‘j F Y’);” produces the formatted version of date, containing the day as “j” month as “F” and “Y” as the year.



  •           Also note that anything that appears outside the PHP tag will be displayed on the screen. As seen in the above line of code where “Today is:” which is outside the PHP tag is displayed on the output screen just before the generated date.


Note:-  In this tutorial PHP code appears inside tags that look like this “<?php ”

            and “?>”. Other tag styles can be used such as

“<%” – Asp tag style..

“<script language = “PHP”>” – the script tag.

The “echo ” command:

  • The “echo” command is used to sent output to the browser


For example:-

[php]
<?php /* php code to display date
and time */
echo “The time is ”;       / use of double quotes /
echo date(‘H:i:s’);
echo ‘and the date is ’      / use of single quotes /
echo date(‘j F Y’);
?>[/php]

  • You can either use the double quotes (“”) or single quotes (‘’) to display string.



  • You can even add “comments” to your code.


“Comment” – It is basically a piece of free text that can be shown an where in the script and is completely ignored by PHP.

Different “comment” styles supported by PHP are :-

1.      Single line comment (i.e. “/……/” or  “#” )

2.      Multi-line comment  (i.e. “/*………….*/”).

 Creating your first script:

  • In order to create and run your PHP scripts you will have to install PHP, MySQL, Apache.If you do not have PHP and other stuffs installed follow this link here which has a complete package of all the software’s you need.


http://www.wampserver.com/en/download.php

Note:- WAMP is Windows Apache MySQL PHP. Where the initial “W” determinesthe platform compatibility. Similarly there are LAMP, MAMP , and XAMPP.

“X” is compatible on all platforms i.e. LINUX, MAC, WINDOWS.I assume that most of them are using WINDOWS platform here.

The installation includes :-

- Apache 2.2.17

 - Php 5.3.3

 - Mysql 5.1.53 (version 64 bits)

 - Mysql 5.5.8 (version 32 bits)

 - PhpMyadmin 3.2.0.1

 - SQLBuddy 1.3.2

  • After installing WAMP, a new directory will be created in your “C:” drive which will contain a folder “www


Note:- when ever you create a PHP file you should save it in c:wampwww”.


  • To run any PHP file first start the “WAMP” server by double clicking on  the icon created on your desktop.



  • After the server is online i.e. “ON” next thing to do is to open your web browser and type in


http://localhost/time.php

Alright we have now completed Lesson 1 (Part 1).Hope you like this tutorial. You have now learnt how  PHP works on a web environment and what a simple PHP script looks like. We will discuss about variables in the next Lesson. You can download the files for practice that I have included in the Download.

PHP Tutorial Part 2


download

6 comments :

  1. I must say, as basic as that was, I'm impressed. I've been around the block with C#/++, Java and their mobile iterations, with anything web being my weakness, and this tutorial is a very slick way to wedge right in.

    ReplyDelete
  2. Thanks @f193bd674e5b612bb53673505be1e609:disqus  We are going to post some cool stuff more

    ReplyDelete
  3. [...] Posted on December 6, 2011 by ntechi So welcome to part 2 of the PHP tutorial. If you are new to PHP then I would recommend you to first check the Part 1 [...]

    ReplyDelete
  4. @f193bd674e5b612bb53673505be1e609:disqus  You can also check the second part of this PHP tutorial.
    http://webstutorial.com/learn-php-php-tutorials-php-part-2/programming/php

    ReplyDelete
  5. [...] To learn CI (CodeIgniter), you should know PHP, MYSQL, OOPs and MVC. If you are new to PHP and MYSQL, then I would recommend you to first learn it, you can learn from here. [...]

    ReplyDelete
  6. [...] To learn CI (CodeIgniter), you should know PHP, MYSQL, OOPs and MVC. If you are new to PHP and MYSQL, then I would recommend you to first learn it, you can learn from here. [...]

    ReplyDelete